如何使用python从硒的下拉框中选择项目 [英] How do I select items from a drop down box in selenium using python

查看:223
本文介绍了如何使用python从硒的下拉框中选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试遍历下拉列表进行网络抓取,但我发现我的代码无法正常工作

I'm attempting to iterate through a dropdown list for webscraping and I've noticed that my code isn't working out

dropdown = browser.find_element_by_XPATH('//*[@id="department-dropdown"]')
select = Select(dropdown)

select.select_by_value("Accounting")

我收到错误消息

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\Web_Scrap\setup.py", line 31, in <module>
    dropdown = browser.find_element_by_XPATH('//*[@id="mainContent"]/div[1]/div/div[3]/div/div/span')
AttributeError: 'WebDriver' object has no attribute 'find_element_by_XPATH'

目前,我试图选择至少第一个值,但是它没有成功

For now I was attempting to select atleast the first value, but it just isn't working out

提供的图片包含我要循环显示的下拉框的检查元素"通过

这似乎有点令人困惑,因为下拉框元素不是实际列表的一部分,有人可以让我知道这里的实际情况吗?如果我看错了.

This seems a bit confusing that the dropdown box element isn't a part of the actual list, can someone give me an idea of what is actually going on here? and if I'm looking at this incorrectly.

如果有人对我可以实现自己的目标有何建议

And if anyone has any recommendations on what I can do to achieve my goal

推荐答案

您的下拉列表框是一个css下拉列表,而不是仅由<select><option>标记实现的本机下拉列表.

Your drop down box is a css drop down, not a native drop down which implement purely by <select> and <option> tag.

下拉菜单的选项来自<ul class="typeahead typeahead-long dropdown-menu"内的li,只有单击右侧的向下箭头后,这些选项才会附加到页面上.

the options of the drop down comes from li inside the <ul class="typeahead typeahead-long dropdown-menu", and they are attached to the page only after you click the down arrow at right side.

<select>包含许多<option>的原因是上述li的属性:在这些<option>上创建的data-value.您可以认为这些<option>li的数据源.因此<select>在页面上不可见,就像前端后面的数据库一样提供数据,因此<select>样式设置为display: none,这意味着在页面上不可见.

The reason there is a <select> with many <option> is the above li's attribute: data-value created upon these <option>. You can think of these <option> are the data source for li. So the <select> not visible on page, act like a data base behind frontend to supply data, thus the <select> style set to display: none which means not visible on page.

要作为用户行为,请单击并展开所有li,然后在ul中的li中找到并选择选项.而不是从不可见的<select>中选择选项或更改select的显示CSS值使其可见,然后从中选择选项.

To act as a user behavior, you should find and select option from li inside ul after click it to expand all li. Rather than select option from invisible <select> or change select display css value to make it visible then select option from it.

// click down arrow to expand all options
driver.find_element_by_css_selector(
    ".department-combobox .input-group > span").click();

// search all options
options = driver.find_elements_by_css_selector(
    ".department-combobox .input-group > ul > li")

// print all option text
for(opt in options):
    println opt.text

// select specific option by text
target = 'Anthropology'
driver.find_element_by_css_selector(
    ".department-combobox .input-group > ul")
    .find_element_by_xpath("./li[.="+target+"]")
    .click();

这篇关于如何使用python从硒的下拉框中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆