Search code examples
pythonseleniumgoogle-chrome-devtoolspuppeteerchrome-devtools-protocol

get list of events associated with a DOM elements


In Firefox it is possible to see events associated with each element in Inspect Element of developers tools. enter image description here

I want to have a list of elements and events associated with it, programmatically. preferably using selenium+python.

I know that there is getEventListeners function that can be used in developer tools of Chrome, but it is not accessible in Selenium.

I went through most of the solutions offered in this question, but found nothing to resolve my problem.

My ultimate goal is to iterate throw html elements of a given page and execute each element's events.


Solution

  • You can do this to a certain extent with python + selenium (chrome only):

    body = driver.execute_cdp_cmd("Runtime.evaluate", {"expression": "document.body"})
    listeners = driver.execute_cdp_cmd("DOMDebugger.getEventListeners", {"objectId": body["result"]["objectId"]})