Selenium Webdriver Python如何在所有复选框中打勾
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Selenium Webdriver Python如何在所有复选框中打勾,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2273字,纯文字阅读大概需要4分钟。
内容图文
![Selenium Webdriver Python如何在所有复选框中打勾](/upload/InfoBanner/zyjiaocheng/663/8d8c0063a7104a658fd7e1da821f9c3f.jpg)
我在网页上有复选框列表.其中一些被打勾.我正在尝试取消勾选的项目.如果选中我的代码,则它们似乎不会取消选中该复选框.
如果我注释掉IF语句,它将选中所有复选框.
问题出在我的IF语句中.它不会检查复选框是否被勾选.
我的代码段是:
# uncheck all of the checkboxes if they are ticked
def remove_ticks_from_all_checkboxes(self):
checkboxes = self.driver.find_elements(By.XPATH, '//div[@id="match_configuration_add_match_tab_data_objects_fp_flags"]//input')
for checkbox in checkboxes:
if checkbox.is_selected == "True": # If checkbox is ticked
checkbox.click() # to untick it
HTML是:
<div id="match_configuration_add_match_tab_data_objects_fp_flags">
<div class="gwt-Label matchruleheader">Gender and title flags</div>
<span class="gwt-CheckBox" style="display: block;">
<input id="gwt-uid-239" type="checkbox" value="on" tabindex="0"/>
<label for="gwt-uid-239">Gender must be present in both names</label>
</span>
<span class="gwt-CheckBox" style="display: block;">
<input id="gwt-uid-240" type="checkbox" value="on" tabindex="0"/>
<label for="gwt-uid-240">Gender must be consistent in both names</label>
</span>
<span class="gwt-CheckBox" style="display: block;">
<input id="gwt-uid-241" type="checkbox" value="on" tabindex="0"/>
<label for="gwt-uid-241">Allow gender mismatch</label>
</span>
<span class="gwt-CheckBox" style="display: block;">
--- some more checkboxes
<span class="gwt-CheckBox" style="display: block;">
--- some more checkboxes
<span class="gwt-CheckBox" style="display: block;">
<span class="gwt-CheckBox" style="display: block;">
<div class="gwt-Label matchruleheader">Name flags</div>
<span class="gwt-CheckBox" style="display: block;">
<span class="gwt-CheckBox" style="display: block;">
<span class="gwt-CheckBox" style="display: block;">
<span class="gwt-CheckBox" style="display: block;">
<span class="gwt-CheckBox" style="display: block;">
<div class="gwt-Label matchruleheader">Other flags</div>
<span class="gwt-CheckBox" style="display: block;">
</div>
如何检查for循环中的复选框是否已选中?
如果选中复选框,那么我想单击它,以取消选中它.
我的XPATH是正确的,因为如果我删除IF语句. for循环将遍历所有复选框并将其选中.
谢谢,
里亚兹
解决方法:
is_selected是一个返回布尔值的方法,将其调用:
for checkbox in checkboxes:
if checkbox.is_selected(): # If checkbox is ticked
checkbox.click() # to untick it
内容总结
以上是互联网集市为您收集整理的Selenium Webdriver Python如何在所有复选框中打勾全部内容,希望文章能够帮你解决Selenium Webdriver Python如何在所有复选框中打勾所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。