5
votes

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

2
I was having the same issue and believe this is a bug. I reported the issue here: bugs.chromium.org/p/chromedriver/issues/detail?id=1320Martin
According to this page developers.google.com/web/tools/chrome-devtools/console/… as getEventListeners is part of Command Line API, "This API is only available from within the console itself. You cannot access the Command Line API from scripts on the page".Vasilen Donchev

2 Answers

3
votes

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.

1
votes

Update for 2021:

There is a beta release of Selenium that offers a Chrome dev-tools API. It's hardly documented but does seem to work for simplistic dev-tools-only JS commands.

e.g. pip install -Iv selenium==4.0.0.b3

browser = webdriver.Chrome('path/to/chromedriver')
js_return = browser.execute_cdp_cmd(command, options)

where the command and options are defined here in the left-hand scroll pane (particularly the Runtime section).