使用Selenium Python从下拉框中选择每个选项 [英] Selecting every options from a drop-down box using selenium python
问题描述
我正在尝试从网站中提取一些数据.但是,该站点具有层次结构.它在顶部有一个下拉菜单,其选项值为URL.因此,我的方法是:
I am trying to extract some data from a website. However, the site has a hierarchical structure. It has a dropdown menu on the top, whose option valueare URLs. Thus, my approaches are:
- 找到下拉框
- 选择一个选项,
- 提取一些数据,然后
- 为所有可用选项重复步骤2至4.
下面是我的代码,我可以在默认选择的选项(第一个)下提取数据.但是我收到错误Message: Element not found in the cache - perhaps the page has changed since it was looked up
.好像我的浏览器没有切换到新页面.我尝试了time.sleep()
或driver.refresh()
,但是失败了……任何建议都值得赞赏!
Below is my code, I am able to extract data under the default selected option (the first one). But I got error Message: Element not found in the cache - perhaps the page has changed since it was looked up
. It seems like my browser was not switched to the new page. I tried time.sleep()
or driver.refresh()
, but failed... Any suggestions are appreciated!
###html
<select class="form-control">
<option value="/en/url1">001 Key</option>
<option value="/en/url2">002 Key</option>
</select>
### python code
# select the dropdown menu
select_box = Select(driver.find_element_by_xpath("//select[@class='form-control']"))
# get all options
options = select_box.options
for ele_index, element in enumerate(options):
# select a url
select_box.select_by_index(ele_index)
time.sleep(5)
print element.text
# extract page data
id_comp_html = driver.find_elements_by_class_name('HorDL')
for q in id_comp_html:
print q.get_attribute("innerHTML")
print "============="
更新1(基于alecxe的解决方案)
# dropdown menu
select_box = Select(driver.find_element_by_xpath("//select[@class='form-control']"))
options = select_box.options
for ele_index in range(len(options)):
# select a url
select_box = Select(driver.find_element_by_xpath("//select[@class='form-control']"))
print select_box.options[ele_index].text
select_box.select_by_index(ele_index)
# print element.text
# print "======"
driver.implicitly_wait(5)
id_comp_html = driver.find_elements_by_class_name('HorDL')
for q in id_comp_html:
print q.get_attribute("innerHTML")
print "============="
推荐答案
您的select_box
和element
引用过时,必须在循环内操作选项索引时重新查找" select元素: /p>
Your select_box
and element
references got stale, you have to "re-find" the select element while operating the option indexes inside the loop:
# select the dropdown menu
select_box = Select(driver.find_element_by_xpath("//select[@class='form-control']"))
# get all options
options = select_box.options
for ele_index in range(len(options)):
# select a url
select_box = Select(driver.find_element_by_xpath("//select[@class='form-control']"))
select_box.select_by_index(ele_index)
# ...
element = select_box.options[ele_index]
在选择一个选项并提取所需的数据之后,您可能还需要向后导航.可以通过driver.back()
完成.
You might also need to navigate back after selecting an option and extracting the desired data. This can be done via driver.back()
.
这篇关于使用Selenium Python从下拉框中选择每个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!