Event listeners are a set of functions in a selenium python bindings that waits for an event to occur; that event may be a user clicking or finding, changing the value, or an Exception. The listeners are programmed to react to an input or signal.
There is a function to react when an event about to happen; also, there is a function to react after the event.
In a Simple way : If person A tries to slap perform B, person B tries to escape the hit (before the event); unfortunately, if A slaps person B. Then B reacts to it either by slapping or by keeping mouth shut (after the event).
Even though selenium python provides listeners, it doesn't offer details on what to do when an event occurs. The user must provide all the details of what should happen when an event occurs.
For Example, the User should implement a method for what should happen before and after a click.
EventFiringWebDriver is a wrapper on the selenium python, and it provides all the methods which the selenium provides.
EventFiringWebDriver provides two more functions to register and unregister the Listeners implementing class.
AbstractEventListener provides pre and post even listeners, so to use these listeners, we have to implement those listeners by inheriting AbstractEventListener.
before_change_value_of :
before_change_value_of method will be invoked when we try to change the value of the element; this method accepts the target element, driver, and the text
after_change_value_of :
after_change_value_of method will be invoked when the target element's values change; this method accepts the target element, driver, and text.
before_click :
before_click method will be invoked before clicking and element with click() method in selenium, this method accepts web element and driver as parameters
after_click :
This method will be invoked after the click() method's operation.
before_find :
before_find method will be invoked before finding the element with the findElement method, this method accepts By class parameter and two web elements
after_find :
after_find method will be invoked after the operation of findElement
before_navigate_back :
before_navigate_back this method will be invoked before executing back() method from Navigation class; this method accepts driver as a parameter
after_navigate_back :
after_navigate_back method will be executed after executing back() method
before_navigate_forward
before_navigate_forward will be invoked before executing forward() method from the Navigation class, this method accepts the driver as a parameter
after_navigate_forward :
after_navigate_forward method will be invoked after executing the forward() method from the Navigation class.
beforeNavigateRefresh :
beforeNavigateRefresh method will be executed before executing refresh() method from the Navigation class, this method accepts driver as a parameter
after_navigate_refresh :
after_navigate_refresh method will be executed after refreshing the page with the refresh() method from the Navigation class
before_navigate_to :
before_navigate_to method will be executed before navigating to nay webpage using to() method from Navigation class, this method accepts String web address and driver as parameters
after_navigate_to : after_navigate_to method will be executed after executing to() method
before_execute_script:
before_execute_script method will be executed before executing any javascript code with JavaScriptExecutor
after_execute_script :
after_execute_script method will be executed after executing javascript code.
on_exception :
on_exception method will be executed whenever there is an exception occurs in selenium, irrespective of whether the user handles the exception or not, this method will be executed
Let's create an example program for Listeners in python.
import unittest
import logging
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
class MyListener(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)
class Test(unittest.TestCase):
def test_logging_file(self):
driver_plain = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe');
edriver = EventFiringWebDriver(driver_plain, MyListener())
edriver.get("https://google.com")
if __name__ == "__main__":
unittest.main()
import unittest
import logging
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
class MyListener(AbstractEventListener):
def before_navigate_to(self, url, driver):
print("Before navigating to ", url)
def after_navigate_to(self, url, driver):
print("After navigating to ", url)
def before_navigate_back(self, driver):
print("before navigating back ", driver.current_url)
def after_navigate_back(self, driver):
print("After navigating back ", driver.current_url)
def before_navigate_forward(self, driver):
print("before navigating forward ", driver.current_url)
def after_navigate_forward(self, driver):
print("After navigating forward ", driver.current_url)
def before_find(self, by, value, driver):
print("before find")
def after_find(self, by, value, driver):
print("after_find")
def before_click(self, element, driver):
print("before_click")
def after_click(self, element, driver):
print("after_click")
def before_change_value_of(self, element, driver):
print("before_change_value_of")
def after_change_value_of(self, element, driver):
print("after_change_value_of")
def before_execute_script(self, script, driver):
print("before_execute_script")
def after_execute_script(self, script, driver):
print("after_execute_script")
def before_close(self, driver):
print("tttt")
def after_close(self, driver):
print("before_close")
def before_quit(self, driver):
print("before_quit")
def after_quit(self, driver):
print("after_quit")
def on_exception(self, exception, driver):
print("on_exception")
class Test(unittest.TestCase):
def test_logging_file(self):
driver_plain = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe');
edriver = EventFiringWebDriver(driver_plain, MyListener())
edriver.get("https://google.com")
edriver.find_element_by_name("q").sendKeys("Sendkeys with listener");
edriver.find_element_by_xpath("//input[contains(@value,'Search')]").click();
edriver.close()
if __name__ == "__main__":
unittest.main()
A table in python selenium
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.