forked from bannerclick/bannerclick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_command.py
51 lines (40 loc) · 1.63 KB
/
custom_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""This file aims to demonstrate how to write custom commands in OpenWPM
Steps to have a custom command run as part of a CommandSequence
1. Create a class that derives from BaseCommand
2. Implement the execute method
3. Append it to the CommandSequence
4. Execute the CommandSequence
"""
import json
import logging
import os
import time
from typing import List
import uuid
from selenium.webdriver import Firefox
from selenium.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
from openwpm.commands.types import BaseCommand
from openwpm.config import BrowserParams, ManagerParams
from openwpm.socket_interface import ClientSocket
class LinkCountingCommand(BaseCommand):
"""This command logs how many links it found on any given page"""
def __init__(self) -> None:
self.logger = logging.getLogger("openwpm")
# While this is not strictly necessary, we use the repr of a command for logging
# So not having a proper repr will make your logs a lot less useful
def __repr__(self) -> str:
return "LinkCountingCommand"
# Have a look at openwpm.commands.types.BaseCommand.execute to see
# an explanation of each parameter
def execute(
self,
webdriver: Firefox,
browser_params: BrowserParams,
manager_params: ManagerParams,
extension_socket: ClientSocket,
) -> None:
current_url = webdriver.current_url
link_count = len(webdriver.find_elements(By.TAG_NAME, "a"))
self.logger.info("There are %d links on %s", link_count, current_url)