-
-
Save rafajafar/6e4c5610d8da74978cf0b1cdf372f13a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClickRecorder(): | |
data = {} | |
current_selector = None | |
current_coord = None | |
current_url = None | |
def addClick(self, sel, coordpair): | |
self.current_selector = sel | |
self.current_coord = coordpair | |
self.current_url = None | |
def addURL(self, url): | |
self.current_url = url | |
if url not in self.data: | |
self.data[url] = {} | |
if self.current_selector not in self.data[url]: | |
self.data[url][self.current_selector] = [] | |
self.data[url][self.current_selector].append(self.current_coord) | |
self.current_selector = None | |
self.current_coord = None | |
class NavListener(AbstractEventListener): | |
prevent_navigation = True | |
click_recorder = None | |
def __init__(self): | |
self.click_recorder = ClickRecorder() | |
def before_navigate_to(self, url, driver): | |
print "BEFORE" | |
self.nav_attempted = False | |
if self.prevent_navigation: | |
driver.execute_script("window.stop();") | |
self.click_recorder.addURL(url) | |
print "DONE" | |
####Check the code here for the calls, the above are for reference | |
def detect_advertisements(pg_url): | |
####cut a large bit of code out here, assume variables exist | |
chrome_options = ChromeOptions() | |
selenium_logger = logging.getLogger('selenium.webdriver.remote.remote_connection') | |
selenium_logger.setLevel(logging.ERROR) | |
ub_driver_base = webdriver.Chrome(executable_path=chromedrvr,chrome_options=chrome_options, service_args=["--log-path=./chrome.log"]) | |
nav_listener = NavListener() | |
ub_driver = EventFiringWebDriver(ub_driver_base, nav_listener) | |
######another large cut | |
actions = ActionChains(ub_driver) | |
ub_driver.set_page_load_timeout(0.01) | |
for dompath in clicks_relevant: | |
print "#",dompath | |
el = ub_driver.find_element_by_id(dompath) | |
for cr in clicks_relevant[dompath]: | |
nav_listener.click_recorder.addClick(dompath, cr) | |
try: | |
#this is the line that should throw an error | |
#I'm trying to capture all page-change events without changing the page | |
#this is for monitoring webpage advertisements | |
actions.move_to_element_with_offset(el, cr["clickX"], cr["clickY"]).click().perform() | |
except: | |
pass | |
try: | |
alert = ub_driver.switch_to_alert() | |
alert.dismiss() | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment