我尝试从具有以下 html 代码的下拉列表中选择“本地主机”:
<select id="server_select">
<option></option>
<option>
Local Host
</option>
<option>
ah005
</option>
</select>
这是我的 python 代码,使用 splinter 模块从下拉列表中选择“本地主机”但失败了。
server_list = browser.find_by_xpath('//select[@id="server_select"]//option[@selected="Local Host"]').first
server_list.click()
我得到了错误:
Traceback (most recent call last):
File "splinter_test.py", line 22, in <module>
server_list = browser.find_by_xpath('//select[@id="server_select"]//option[@selected="Local Host"]').first
File "/Users/Bibo_MBPR/anaconda/lib/python2.7/site-packages/splinter/element_list.py", line 53, in first
return self[0]
File "/Users/Bibo_MBPR/anaconda/lib/python2.7/site-packages/splinter/element_list.py", line 44, in __getitem__
self.find_by, self.query))
splinter.exceptions.ElementDoesNotExist: no elements could be found with xpath "//select[@id="server_select"]//option[@selected="Local Host"]"
有人可以给我一些建议吗?谢谢
请您参考如下方法:
您可以使用 find_by_id 获取用于下拉的浏览器元素,因为它具有 id="server_select" 选项。然后您可以通过文本从中选择您的选项。尝试以下 -
server_list = browser.find_by_id('server_select')
server_list.select_by_text("Local Host")
或者,
browser.find_option_by_text("Local Host").first.click()
