gpt4 book ai didi

python - 使用 Xpath 单击页面上的所有元素会导致 ElementNotVisibleException

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:18 27 4
gpt4 key购买 nike

我有一个帮助函数来浏览页面并打开它找到的每个帮助按钮。它既漂亮又简单;

def openHelp(ff):
"""
Opens all the help buttons on the screen
@type ff: instance of webdriver
@param ff: firefox instance of webdriver
"""
allHelpButtons = ff.find_elements_by_xpath('//a[@class="helpButton"]')
for helpButton in allHelpButtons:
helpButton.click()

但是,在某些页面上,帮助按钮及其相应字段可能会被 javascript 隐藏,这似乎是当 Selenium 尝试单击这些隐藏按钮时导致 ElementNotVisibileException 的原因。

每个帮助按钮在标记中显示都相同,没有应用 display:none,所以我无法这样检查。它们看起来是这样的;

<a class="helpButton" title="Help about: Field" href="#">
<img alt="Help about: Field" src="/static/images/helpIcon.png">
</a>

如果存在此异常,我假设必须有一种方法可以用来检查元素是否可见。理想情况下,我只想将所有可见元素收集到我的 allHelpButtons 列表中,但我找不到任何相关文档。我可以使用 xpath 搜索执行检查吗?还是必须在收集元素后对其进行检查?

奖励问题

我还想知道,是否可以通过匹配名称和值来选择列表项。例如,我在页面中将“是/否”单选按钮选择定义为 ul;

<ul class="compact horizontal ">
<li>
<input id="id_fieldname_0_true" type="radio" value="True" name="fieldname">
<label for="id_fieldname_0_true">Yes</label>
</li>
<li>
<input id="id_fieldname_0_false" type="radio" value="False" name="fieldname">
<label for="id_fieldname_0_false">No</label>
</li>
</ul>

显然,id 是唯一的,但我宁愿能够使用选择器来挑选“名称”和“值”,因为我认为这将使制作通用选择器变得更容易,我可以将值传递给它.

最佳答案

有一个is_displayed函数。

检查下面的源代码,第 162 行:

https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/remote/webelement.py

至于您的奖励问题,这也是由 XPath 完成的,单击"is"按钮单选:

//label[text()='Yes']/preceding-sibling::input

对于否:

//label[text()='No']/preceding-sibling::input

关于python - 使用 Xpath 单击页面上的所有元素会导致 ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15156375/

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