get list of events associated with a DOM elements
Asked Answered
D

2

7

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.

Desmonddesmoulins answered 10/6, 2020 at 11:5 Comment(7)
Out of interest, is this a test? What are you testing?Tango
@Tango well, it is not really a test. As a part of a project, I need to track DOM changes after each event executed.Desmonddesmoulins
Could you show us the url of the page.Snowberry
@Snowberry it could be any url.Desmonddesmoulins
Why not use puppeteer?Kristikristian
@Kristikristian would you please show me some sample code that does what I asked for using puppeteer.Desmonddesmoulins
github.com/puppeteer/puppeteer/issues/…Kristikristian
T
6

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"]})
Thankyou answered 14/6, 2020 at 9:12 Comment(1)
I can approve that this solution works to a certain extent. It doesn't retrieve React events, for example.Desmonddesmoulins
D
0

To whom it may concern, in the future!

The solution offered by @pguardiario collects most of the events, but not React and jQuery, as far as I tested this solution.

To overcome this problem and collect React and jQuery events I dug deep into firefox source code and found out that firefox uses sort of a parser, to show React and jQuery events, same as Visual Events 2 solution.

Desmonddesmoulins answered 28/6, 2020 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.