html源代码如下
<select id="ca_vdcs" class="pulldown small" name="vdc" style="display: none;">
<option>-- Select a vDC --</option>
<option>Platform-VDC-org</option>
</select>
我想选择“Platform-VDC-org”,但下面的代码不起作用。
select = browser.find_element_by_id('ca_vdcs')
select.find_element_by_xpath("//option[@value='Platform-VDC-org']").click()
最佳答案
您应该尝试使用 Select()
类(class)。它使处理 select elements
容易得多。
select = Select(browser.find_element_by_id("ca_vdcs"))
select.select_by_visible_text("Platform-VDC-org")
您可以在此处查看 Python 中的 WebDriver API 绑定(bind):
http://selenium-python.readthedocs.org/en/latest/api.html
Select()
类(class)在第 7.12 节。用户界面支持
关于python-2.7 - 如何在 python 中使用 webdriver 选择下拉列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648444/