取消选择单选项下拉python webdriver

时间:2016-09-24 05:54:20

标签: python selenium

在使用webdriver选择选项后,我有一个问题未选中。我一直收到错误引发NotImplementedError("你可能只取消选择多选的选项") NotImplementedError:您只能取消选择多选

的选项

如何取消选择下拉菜单项?我的代码如下。

HTML code:

<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")

2 个答案:

答案 0 :(得分:0)

您可以使用.select_by_index(0).select_by_value("")。第一个应该工作......我不确定第二个。

答案 1 :(得分: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()来将选择的选择更改为另一个元素。

如果您正在练习取消选择,请在支持多选

的页面上进行尝试