【问题标题】:How to scrape drop-down menu options using Selenium Webdriver with Python?如何使用 Selenium Webdriver 和 Python 抓取下拉菜单选项?
【发布时间】:2017-09-18 14:37:43
【问题描述】:

我使用 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>

【问题讨论】:

  • 您需要获取值还是选择特定的值?
  • 我需要获取一个包含值的列表。选项是特定于用户的,但选项卡 ngcontent-pxo-26 似乎对每个用户都是相同的。

标签: python selenium selenium-webdriver beautifulsoup


【解决方案1】:

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 代码在 cmets 块内 &lt;!--template bindings={}--,您可能无法检索其值。

【讨论】:

    【解决方案2】:

    应该很简单。

    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)
    

    【讨论】:

      【解决方案3】:

      您可以使用 BeautifulSoup 做到这一点。

      既然你提到了 selenium,这段代码就开始使用它,以防你需要它来通过登录或其他需要 selenium 的东西。如果您不需要 selenium,那么您可以跳到使用 BeautifulSoup 生成 soup 的行。上面的代码只是展示了如何使用 selenium 获取源代码,以便BeautifulSoup 可以访问。

      首先找到包含所有 HTML 代码的 select 标记,包括注释的内容。然后获取此列表中的每个项目,将其转换为字符串并将其连接成一个大字符串,并在前面加上&lt;select&gt;。把这个大字符串变成汤和findAll 里面的option 标签。从这些标签中显示您想要的任何内容。

      >>> 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'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-26
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 2016-07-28
        • 1970-01-01
        • 2014-02-26
        • 2018-02-17
        相关资源
        最近更新 更多