简体   繁体   中英

trigger python events driven by selenium controlled browser

I'm a bit new to working with Selenium. I'm trying to trigger calls to Python when I do certain things in a Selenium controlled web browser.

For example, I'm running this script in Python 3.6.3 in Spyder using the run button.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from selenium import webdriver
b = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')

from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener

class EventListeners(AbstractEventListener):
    def before_navigate_to(self, url, driver):
        print("before_navigate_to")

    def after_navigate_to(self, url, driver):
        print("after_navigate_to")

d = EventFiringWebDriver(b,EventListeners())

d.get('https://www.cnn.com')

When I navigate in the browser by clicking a link or changing the url manually I don't see the events being fired. However I can verify that the url has changed. Also, if I call the get method from Python again, the events fire. This leads me to believe that these event listeners are only listening to the Python webdriver, and not to the actual browser. How do I get the Python code to run in response to browser actions such as navigation? Eventually I'd like to get Python running in response to javascript function executions as well ...

Selenium is designed for automation testing. Since your task include manual intervention, it is difficult to (to the best of my knowledge) to implement what you need.

A workaround can (both with automated clicks and manual clicks) can be following:
Track navigation using mouse click tracking and grab the current URL information.

from selenium import webdriver
import time
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
from pynput.mouse import Listener

b = webdriver.Chrome(executable_path=r'C:\Program Files\chromewebdriver\chromedriver.exe')
b.maximize_window()

class EventListeners(AbstractEventListener):
    def before_navigate_to(self, url, driver):
        print("before_navigate_to %s" % url)

    def after_navigate_to(self, url, driver):
        print("after_navigate_to %s" % url)

    def before_click(self, element, driver):
        print("before_click %s" % element)

    def after_click(self, element, driver):
        print("after_click %s" %element)

    def after_navigate_forward(self, driver):
        print("after_navigate_forward");

    def before_navigate_forward(self, driver):
        print("before_navigate_forward")

    def after_navigate_back(self, driver):
        print("after_navigate_back")

    def before_navigate_back(self, driver):
        print("before_navigate_back")

    def before_change_value_of(self, element, driver):
        print("before_change_value_of")

d = EventFiringWebDriver(b,EventListeners())

d.get('https://www.cnn.com')
d.implicitly_wait(20)
d.get('https://www.google.de')
d.implicitly_wait(20)
d.back()

def on_click(x, y, button, pressed):
    if pressed:
        print('Mouse clicked')
        time.sleep(2)
        print("Navigation to: %s " % b.current_url)

with Listener(on_click=on_click) as listener:
    listener.join()






This will result in the following output:
在此处输入图片说明
The first 6 lines of output have resulted from event listeners and last 5 lines have resulted from mouse click listener.
Note: All mouse clicks are tracked (not just this session clicks). Please remember to stop the program once you are done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM