2016-09-24 77 views
1

我在使用webdriver選擇它後取消選擇所選選項時出現問題。我不斷收到錯誤加薪NotImplementedError(「你只能取消的多選選項」) NotImplementedError:您只能取消選項的多選取消選擇單選項下拉列表python webdriver

選擇的下拉菜單項如何才能未被選擇?我的代碼如下。

HTML代碼:

<option selected="selected" value=""></option> 
<option value="Item 1">Item 1</option> 
<option value="Item 2 (1)">Item 2</option> 
<option value="Item 3">Item 3</option> 

的Python的webdriver:

options = select.find_elements_by_tag_name("option") 
for x in range(1,len(options)): 
    option = options[x] 
    options_list.append(option.get_attribute("value")) 
    item_selection = Select(select) 
    item_selection.select_by_visible_text("Item 1") 
time.sleep() 
item_selection.deselect_by_visible_text("Item 1") 

回答

0

你可以使用.select_by_index(0)或可能.select_by_value("")。第一個應該工作......我不確定第二個。

0

的問題是,你的下拉列表中選擇類沒有多選,這意味着你只能在同一時間從下拉列表中選擇一個項目,所有取消的功能有一個檢查,如果

if not self.is_multiple: 
      raise NotImplementedError("You may only deselect options of a multi-select") 

因爲它給你錯誤,因爲取消選擇應該只適用於多選下拉菜單。

解決方法是使用另一個元素的select_by_index()或select_by_value()或select_by_visible_text()將所選元素的選擇更改爲另一個元素。

如果您正在練習取消選擇然後在支持多選的頁面上嘗試它

+0

好的謝謝。你的建議似乎有效。 – user674864

相關問題