Browser Automation with Python and Selenium — 9: Event Firing WebDriver

In the previous post, we explored how to pass options to the Selenium WebDriver instance to set the various preferences for the browser.

EventFiringWebDriver class is a wrapper for a WebDriver instance that supports firing events.

You can use this to take some actions before or after certain events like finding an element, navigating to a url, clicking an element, or quitting the browser.

It takes driver, and event_listener as arguments. event_listener is an instance of a class that subclasses the AbstractEventListener class.

Following methods of the AbstractEventListener class should be implemented fully or partially.

* before_navigate_to(self, url, driver)
* after_navigate_to(self, url, driver)
* before_navigate_back(self, driver)
* after_navigate_back(self, driver)
* before_navigate_forward(self, driver)
* after_navigate_forward(self, driver)
* before_find(self, by, value, driver)
* after_find(self, by, value, driver)
* before_click(self, element, driver)
* after_click(self, element, driver)
* before_change_value_of(self, element, driver)
* after_change_value_of(self, element, driver)
* before_execute_script(self, script, driver)
* after_execute_script(self, script, driver)
* before_close(self, driver)
* after_close(self, driver)
* before_quit(self, driver)
* after_quit(self, driver)
* on_exception(self, exception, driver)

The following example implements before_navigate_to, after_navigate_to, before_find, after_find, before_quit, and after_quit methods to print custom messages with these events.

# output
Before navigating to https://www.pythondoctor.com/
Current url before navigating to https://www.pythondoctor.com/ is 'about:blank'
Page title before navigating to https://www.pythondoctor.com/ is ''
After navigating to https://www.pythondoctor.com/
Current url after navigating to https://www.pythondoctor.com/ is 'https://www.pythondoctor.com/'
Page title after navigating to https://www.pythondoctor.com/ is 'Python Doctor'
Searching for element with 'id=id_questioner' on https://www.pythondoctor.com/Found element with 'id=id_questioner' on https://www.pythondoctor.com/Quitting the browser with url: https://www.pythondoctor.com/
Bye ...
Quit the browser. Have a nice day :)

Things to Remember

  • EventFiringWebDriver class is a wrapper for a WebDriver instance that supports firing events to take some actions before or after certain events.
  • It takes driver, and event_listener as arguments. event_listener is an instance of a class that subclasses the AbstractEventListener class.

In the next post, we will look at how to execute JavaScript code on the page with Selenium.

Thank you for your time.

Software Engineer, Python Enthusiast, Darbuka Player, https://coskundeniz.github.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store