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 ...