繁体   English   中英

如何使用带有 Python 的 Selenium Webdriver 抓取下拉菜单选项?

[英]How to scrape drop-down menu options using Selenium Webdriver with Python?

我使用 Selenium Webdriver 来测试一个带有下拉菜单的网站,该菜单为不同的用户提供不同的选项。 选项的数量和它们的值总是不同的。 当我查看源代码时,我看到了下面的代码。 您能否提供一个示例,说明如何在 Python 中抓取它并列出所有可用的选项值?

<div _ngcontent-pxo-26="" class="col-md-6">
  <div _ngcontent-pxo-26="" class="form-group">
    <label _ngcontent-pxo-26="" for="Filter_ClientRegion">Region</label>
    <select _ngcontent-pxo-26="" class="form-control ng-pristine ng-valid ng-touched" id="Filter_ClientRegion">
      <option _ngcontent-pxo-26="" value="">All</option>
      <!--template bindings={}--
      <option _ngcontent-pxo-26="" value="A">A</option>
      <option _ngcontent-pxo-26="" value="B">B</option>
      <option _ngcontent-pxo-26="" value="C">C</option>
      <option _ngcontent-pxo-26="" value="D">D</option>
      <option _ngcontent-pxo-26="" value="E">E</option>
      <option _ngcontent-pxo-26="" value="F">F</option>
      <option _ngcontent-pxo-26="" value="G">G</option>
    </select>
  </div>
</div>

select特定option ,您可以使用以下内容:

from selenium import webdriver
driver = webdriver.Firefox()

driver.get("some.site")
el = driver.find_element_by_id('Filter_ClientRegion')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'A': # or  B or C...
        option.click() # select() for older versions
        break

要获取optionvalues ,您可以使用:

options = []
driver.get("some.site")
el = driver.find_element_by_id('Filter_ClientRegion')
for option in el.find_elements_by_tag_name('option'):
    options.append(option.get_attribute("value"))
# print(options)
# A B C ...

笔记:
1.我无法完全测试上面的代码,因为我没有完整的源代码
2. 请注意, options代码位于注释块<!--template bindings={}-- ,您可能无法检索其值。

这应该很容易。

array_options =  []
element = WebDriverWait(self.driver, timeout=wait_time).until(
          EC.visibility_of_element_located("id","Filter_ClientRegion")))
if element.tag_name == 'select':
    select = Select(element)
    dropdown_options = select.options
    for option in dropdown_options:
        array_options.append(option.text)

你可以用 BeautifulSoup 做到这一点。

由于您提到了硒,因此该代码首先使用它,以防您需要它来通过登录或其他需要硒的东西。 如果您不需要 selenium,那么您可以跳到使用BeautifulSoup制作soup的那一行。 前面的代码只是展示了如何使用 selenium 获取源代码,以便它可以被BeautifulSoup访问。

首先找到包含所有 HTML 代码的select标记,包括注释的内容。 然后获取此列表中的每个项目,将其转换为字符串并将其连接为一个大字符串,并添加<select> 打开这个大串入汤findAlloption之内它的标签。 从这些标签中的每一个中显示您想要的任何内容。

>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> content = driver.page_source
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(content, 'lxml')
>>> select = soup.find('select', attrs={'id': 'Filter_ClientRegion'})
>>> items = []
>>> for item in select.contents:
...     items.append(str(item).strip())
...     
>>> items
['', '<option _ngcontent-pxo-26="" value="">All</option>', '', 'template bindings={}--\n      <option _ngcontent-pxo-26="" value="A">A</option>\n      <option _ngcontent-pxo-26="" value="B">B</option>\n      <option _ngcontent-pxo-26="" value="C">C</option>\n      <option _ngcontent-pxo-26="" value="D">D</option>\n      <option _ngcontent-pxo-26="" value="E">E</option>\n      <option _ngcontent-pxo-26="" value="F">F</option>\n      <option _ngcontent-pxo-26="" value="G">G</option>\n    </select>\n  </div>\n</div>']
>>> newContents = '<select>' + ''.join(items).replace('--','')
>>> newSelectSoup = BeautifulSoup(newContents)
>>> options = newSelectSoup.findAll('option')
>>> len(options)
8
>>> for option in options:
...     option.attrs['value']
...     
''
'A'
'B'
'C'
'D'
'E'
'F'
'G'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM