如何在Python中使用SeleniumWebDriver刮取下拉菜单选项?

2024-10-04 01:23:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用SeleniumWebDriver测试一个带有下拉菜单的网站,该下拉菜单为不同的用户提供了不同的选项。选项的数量及其值总是不同的。当我查看源代码时,我看到了下面的代码。您能否提供一个示例,说明我如何在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>

Tags: divformvalue选项filterngselectlabel
3条回答

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,所以这段代码首先使用它,以防您需要它通过登录或其他需要selenium的东西。如果您不需要selenium,那么可以跳到使用BeautifulSoup生成soup的行。前面的代码只是展示了如何使用selenium获取源代码,以便BeautifulSoup可以访问它

首先找到包含所有HTML代码的select标记,包括注释的内容。然后获取列表中的每个项目,将其转换为字符串,并将其连接为一个大字符串,并在<select>前加前缀。将这个大字符串转换为soup和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'

相关问题 更多 >