admin管理员组

文章数量:1392007

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

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

Share Improve this question asked Dec 10, 2015 at 14:30 abrunetabrunet 1,12218 silver badges32 bronze badges 2
  • 1 I was having the same issue and believe this is a bug. I reported the issue here: bugs.chromium/p/chromedriver/issues/detail?id=1320 – Martin Commented Feb 6, 2016 at 6:08
  • According to this page developers.google./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 Commented Nov 3, 2016 at 8:50
Add a ment  | 

2 Answers 2

Reset to default 3

Sadly the answer is: Accessing getEventListeners via ChromeDriver is not possible. This is -- as mentioned in the ments -- 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.

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

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

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

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

本文标签: javascriptAccess getEventListeners from chrome webdriver (python)Stack Overflow