如何使用 capybara-webkit 或本机 QtWebKit 获取 DOM 元素单击处理程序?

发布于 2024-12-28 18:09:59 字数 272 浏览 4 评论 0原文

我正在使用 capybara-webkit 并希望将事件处理程序绑定到 DOM 中元素的单击事件。即使使用本机 QtWebKit 调用的答案也可能足以让我弄清楚如何使用 Ruby 中的 webkit 驱动程序来做到这一点。我面临的挑战是事件处理程序是在 JavaScript 中以编程方式绑定的,而不是在 HTML 中,到目前为止我对如何执行此操作的搜索似乎都以如何单击或以其他方式触发 QWebView 中的事件而结束。我需要检查事件处理程序(即实际的函数定义),特别是绑定到事件的匿名函数,而不生成事件本身。任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独行侠 2025-01-04 18:09:59

您可以使用page.execute_script执行任意Javascript并返回结果。如果有问题的事件处理程序是使用 jQuery 绑定的,这将列出它们:

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

这里有一个关于事件处理程序内省的完整答案:如何在调试时或从 JavaScript 代码中查找 DOM 节点上的事件侦听器?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文