gpt4 book ai didi

javascript - 确定 HTML div 是否可点击

转载 作者:行者123 更新时间:2023-11-28 08:53:45 26 4
gpt4 key购买 nike

我正在从事一些 Selenium/Robot Framework 自动化测试,我需要一种自动定位 div 的方法可点击的标签。

如果我给它一个非常具体的 CSS 选择器,Click Element 关键字就可以解决问题,但是我如何通过算法找到可以单击的元素呢?只要有办法获取 DOM 节点,我就希望返回一个唯一的选择器,我可以用它来调用 Click Element 关键字。

据我所知,旧版本的 jQuery 有 $(element).data('events')可以用来查找点击监听器,但这似乎是一种不可靠的方法。此外,Chrome Web 检查器似乎无法识别任何 click div 的听众这就是我正在看的。

为了完整起见,以下是我正在使用的 HTML 示例:

<div id="contentarea" class="">
<div id="contentwrapper" style="opacity: 1;">
<div class="rap">
<div id="twocol" class="clearfix margintop">
<div class="leftcol scrollable addprogram">
<a href="..." class="newgroup">
<span class="addicon"></span>New Group
</a>
</div>
<div class="rightcol clearfix">
<ul class="subtabs rounded clearfix">
<li data-tab="pending" class="selected">
<a href="..." class="selected">Released</a>
</li>
</ul>
<div class="clear"></div>
<div id="program-grid" class="clearfix" style="">
<div class="program groups" tabindex="0">
<div class="programframe"></div>
<div class="programinfo">
<h1 class="prizeheight">Development</h1>
</div>
</div><div class="program groups" tabindex="0">
<div class="programframe"></div>
<div class="programinfo">
<h1 class="prizeheight">Loss Prevention/Safety Group</h1>
</div>
...
...

<强> <div class='program groups'>是一个可点击的元素,我希望以编程方式识别它。

郑重声明,HTML 有效等等。缩小的 Javascript 以某种方式呈现此源,并可能附加监听器。

谢谢!

最佳答案

根据this website,这可能是不可能的.

Which event handlers are registered?

One problem of the current implementation of W3C’s event registration model is that you can’t find out if any event handlers are already registered to an element. In the traditional model you could do:

alert(element.onclick) and you see the function that’s registered to it, or undefined if nothing is registered. Only in its very recent DOM Level 3 Events W3C adds an eventListenerList to store a list of event handlers that are currently registered on an element. This functionality is not yet supported by any browser, it’s too new. However, the problem has been addressed.

Fortunately removeEventListener() doesn’t give any errors if the event listener you want to remove has not been added to the element, so when in doubt you can always use removeEventListener().

关于javascript - 确定 HTML div 是否可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18789753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com