【问题标题】:Python 3 Selenium Select from drop down menu with the same ID but different valuePython 3 Selenium 从具有相同 ID 但不同值的下拉菜单中选择
【发布时间】:2021-01-21 08:39:11
【问题描述】:
我正在使用 Firefox,并希望从下拉菜单中单击所选文本。
于是我打开了optgroup,想选这个:
<option value="WSS" id="A5">[PREMIUM] WSS (wss://)</option>
有不同的值,但 ID 相同。
我试过这个但没有成功:
select = Select(driver.find_element_by_id('A5'))
select.select_by_value("WSS")
select.select_by_visible_text("visible text/the shown text on the drop down menu")
select.select_by_value('WSS')
【问题讨论】:
标签:
python
python-3.x
selenium
selenium-webdriver
【解决方案1】:
您可以尝试获取在 HTML 代码中必须是“选择”的父对象(使用“./..”)并列出所有可能的选项。避免了多id的问题。
试试这个:
yourValue = "WSS"
listOptions = driver.find_elements_by_xpath("//option[@id='A5']/./../option")
for option in listOptions:
if option.text == yourValue: # if wrong test "if option.get_attribute("innerText") == yourValue:"
indexed = listOptions.index(option)
break
Select(driver.find_elements_by_xpath("//option[@id='A5']/./..]")).select_by_index(indexed)
我直接在这里写的,可能有问题,不要犹豫,用更多的 HTML 代码给你的反馈,它会有所帮助;)