2016-12-27 96 views
1

我正在檢查類似於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> 
+1

你可以嘗試屬性「價值」?猜測它的值=「開」選定的複選框! –

回答

2

看起來複選框元素實際上是<input>子標籤。嘗試使用它

checkboxes = driver.find_elements_by_css_selector('.gwt-CheckBox > input')