Skip to content

Access getEventListeners from chrome webdriver (python)

Aim:

Get all events attached to a node from selenium webdriver


I’m using selenium-python and I’d like to execute a javascript script (through driver.execute_script('my js script').

This script uses getEventListeners which is only available on Chrome. I used successfully

driver = webdriver.Chrome('path/to/chromedriver')

to launch a chrome browser. Executing my script with getEventListeners(myNode) I get something like:

File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, line 403, in execute_script {‘script’: script, ‘args’:converted_args})[‘value’] File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, line 175, in execute self.error_handler.check_response(response) File “/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py”, line 166, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: getEventListeners is not defined

getEventListeners is available through the Command Line API but I can’t make it work from selenium. Is there a solution for this? Is there any other way to get all events binded to an element ? (especially Click event)

Cheers

Answer

Sadly the answer is: Accessing getEventListeners via ChromeDriver is not possible. This is — as mentioned in the comments — by design:

Warning: These functions only work when you call them from the Chrome DevTools Console. They won’t work if you try to call them in your scripts.

The according issue report was set to WontFix.

BUT:

Check out this answer for finding all events attached to a node without using getEventListeners.