Python Selenium/xpath从下拉菜单获取文本
在Selenium中,有没有一种方法可以从下拉菜单中获取文本,然后在Python中放入列表 假设:Python Selenium/xpath从下拉菜单获取文本,python,html,selenium,xpath,Python,Html,Selenium,Xpath,在Selenium中,有没有一种方法可以从下拉菜单中获取文本,然后在Python中放入列表 假设: <select id="zoo"> <option value="30"> Lion </option> <option value="10"> Elephant </option> <option value="5"> Zebra </opti
<select id="zoo">
<option value="30">
Lion
</option>
<option value="10">
Elephant
</option>
<option value="5">
Zebra
</option>
</select>
狮子
大象
斑马
如何将狮子、大象和斑马放入列表中?使用并获取:
选项
返回属于此选择标记的所有选项的列表
其中driver
是webdriver
实例
演示(使用(是的,对不起W3学校:):
请注意,此页面上的select
元素没有id
,因此我只是使用find\u element\u by\u tag\u name()
来查找它。还要注意,这里有iframe
,因此我必须切换到适当的iframe
才能找到元素
希望有帮助 以下是对我有效的方法:
select = Select(driver.find_element_by_tag_name('select'))
text_list = []
for option in select.find_elements_by_tag_name('option'):
text_list.append(option.text)
谢谢你的帮助 非常感谢你的帮助。我确实不断收到相同的错误,但是:TypeError:interaction over non sequence也许我没有成功找到Select元素?@user2631667不客气,你能展示你正在使用的代码和完整的错误回溯吗?我会帮忙的。谢谢
>>> from selenium import webdriver
>>> from selenium.webdriver.support.select import Select
>>> driver = webdriver.Firefox()
>>> driver.get('http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select')
>>> driver.switch_to.frame('view')
>>> select = Select(driver.find_element_by_tag_name('select'))
>>> [option.text for option in select.options]
[u'Volvo', u'Saab', u'Opel', u'Audi']
select = Select(driver.find_element_by_tag_name('select'))
text_list = []
for option in select.find_elements_by_tag_name('option'):
text_list.append(option.text)