[英]Selenium/Python: Find <label for=“”> element with no other attributes
[英]Python & Selenium - Find element by label text
我正在尝试使用 python 和 selenium 网络驱动程序从 html 网站上的大量复选框中找到并单击一个元素(复选框)。 HTML 代码如下所示:
<div class="checkbox-inline col-md-5 col-lg-3 col-sm-6 m-l-sm rightCheckBox">
<input type="checkbox" checked="checked" class="i-checks" name="PanelsContainer:tabsContentView:5:listTabs:rights-group-container:right-type-view:2:right-view:2:affected-right" disabled="disabled" id="id199"> <label>Delete group</label>
</div>
我的问题是唯一的唯一标识符是:
<label>Delete group</label>
所有其他元素/ID 的/名称由其他复选框或从页面到页面的更改使用。 我尝试了以下代码:
driver.find_element_by_xpath("//label[contains(text(), 'Delete group')]").click()
但是我只在使用它时出错。 错误:元素不可交互
任何人都可以帮忙吗?
试试下面的 xpath
//label[contains(text(), 'Delete group')]//ancestor::div//input
尝试使用 Javascript。
checkBox = driver.find_element_by_xpath("//label[text()='Delete group']//ancestor::div//input")
# Scroll to checkbox if its not in screen
driver.execute_script("arguments[0].scrollIntoView();", checkBox)
driver.execute_script("arguments[0].click();", checkBox)
注意:根据您分享的 HTML,复选框处于禁用状态 state,因此我不确定点击是否会触发任何操作。 但是上面的代码将单击您的复选框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.