Question

I'm using capybara-webkit and would like to get the event handler(s) bound to the click event of an element in the DOM. An answer even using native QtWebKit calls would probably be enough for me to figure out how to do it using the webkit driver in Ruby. The challenge I am having is that the event handlers are being bound programmatically in JavaScript, not in HTML, and my searches so far on how to do this all seem to end with how to click or otherwise trigger events in a QWebView. I need to inspect the event handler (i.e. the actual function definition), in particular anonymous functions bound to the event, without generating the event itself. Any help is appreciated.

Was it helpful?

Solution

You can execute arbitrary Javascript and return the result using page.execute_script. If the event handlers in question are bound using jQuery, this will list them:

page.execute_script(<<-JAVASCRIPT)
  var handlers = $('.some-selector').data("events").click;
  jQuery.each(handlers, function(key, handler) {
    console.log(handler);
  });
JAVASCRIPT

There's a complete answer on introspecting on event handlers here: How to find event listeners on a DOM node when debugging or from the JavaScript code?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top