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.

HadiRj
  • 1,015
  • 3
  • 21
  • 41

2 Answers2

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"]})
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • I can approve that this solution works _to a certain extent_. It doesn't retrieve `React` events, for example. – HadiRj Jun 14 '20 at 12:10
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.

HadiRj
  • 1,015
  • 3
  • 21
  • 41