使用Selenium检查所有复选框
问题描述:
我正在检查类似于https://sqa.stackexchange.com/questions/3292/how-to-select-or-check-multiple-checkboxes-in-selenium的页面上的所有复选框。使用Selenium检查所有复选框
我试着然而,随着
checkboxes = driver.find_elements_by_class_name('gwt-CheckBox')
for checkbox in checkboxes:
if not checkbox.is_selected():
checkbox.click()
要做到这一点,所有的复选框给假为is_selected()
,使已经选定拿不到的关闭,未选择拿不到的开启。下面我有一个选中的CheckBox类(库名)和一个未经检查的复选框类(配对名义长度)的例子。
如何检查这些是否已被选中?
<div style="position: relative; display: inline-block; vertical-align: top; float: left;">
<span class="gwt-CheckBox">
<input id="gwt-uid-27" tabindex="0" type="checkbox" value="on"/>
<label for="gwt-uid-27">
Library name
</label>
</span>
</div>
</div>
<div class="holderFp" style="width: 100%; position: relative; overflow: hidden; display: block;">
<div style="position: relative; display: inline-block; vertical-align: top; float: left; width: 25%;">
<span class="gwt-CheckBox">
<input id="gwt-uid-28" tabindex="0" type="checkbox" value="on"/>
<label for="gwt-uid-28">
Paired nominal length
</label>
</span>
</div>
答
看起来复选框元素实际上是<input>
子标签。尝试使用它
checkboxes = driver.find_elements_by_css_selector('.gwt-CheckBox > input')
你可以尝试属性“价值”?猜测它的值=“开”选定的复选框! –