如何在 Selenium 的下拉列表中选择项目

Posted

技术标签:

【中文标题】如何在 Selenium 的下拉列表中选择项目【英文标题】:How to Select Items in Dropdown in Selenium 【发布时间】:2014-07-31 19:19:15 【问题描述】:

首先,我一直在尝试从该网页获取下拉列表:http://solutions.3m.com/wps/portal/3M/en_US/Interconnect/Home/Products/ProductCatalog/Catalog/?PC_Z7_RJH9U5230O73D0ISNF9B3C3SI1000000_nid=RFCNF5FK7WitWK7G49LP38glNZJXPCDXLDbl

这是我的代码:

    import urllib2
    from bs4 import BeautifulSoup
    import re
    from pprint import pprint

    from selenium import webdriver

    url = 'http://solutions.3m.com/wps/portal/3M/en_US/Interconnect/Home/Products/ProductCatalog/Catalog/?PC_Z7_RJH9U5230O73D0ISNF9B3C3SI1000000_nid=RFCNF5FK7WitWK7G49LP38glNZJXPCDXLDbl'

    element_xpath = '//*[@id="Component1"]'
    driver = webdriver.PhantomJS()
    driver.get(url)
    element = driver.find_element_by_xpath(element_xpath)
    element_xpath = '/option[@value="02"]'
    all_options = element.find_elements_by_tag_name("option")
    for option in all_options:
        print("Value is: %s" % option.get_attribute("value"))
        option.click()
    source = driver.page_source.encode('utf-8', 'ignore')
    driver.quit()

    source = str(source)

    soup = BeautifulSoup(source, 'html.parser')

    print soup

打印出来的是这样的:

Traceback (most recent call last):
  File "../../../../test.py", line 58, in <module>
Value is: XX
    main()
  File "../../../../test.py", line 46, in main
    option.click()
  File "/home/eric/dev/octocrawler-env/local/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 54, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/home/eric/dev/octocrawler-env/local/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 228, in _execute
    return self._parent.execute(command, params)
  File "/home/eric/dev/octocrawler-env/local/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/home/eric/dev/octocrawler-env/local/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: u'"errorMessage":"Element is not currently visible and may not be manipulated","request":"headers":"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:51413","User-Agent":"Python-urllib/2.7","httpVersion":"1.1","method":"POST","post":"\\"sessionId\\": \\"30e4fd50-f0e4-11e3-8685-6983e831d856\\", \\"id\\": \\":wdc:1402434863875\\"","url":"/click","urlParsed":"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":,"chunks":["click"],"urlOriginal":"/session/30e4fd50-f0e4-11e3-8685-6983e831d856/element/%3Awdc%3A1402434863875/click"' ; Screenshot: available via screen

其中最奇怪最令人愤怒的一点是,有时它实际上都可以解决。我不知道这里发生了什么。


更新

似乎我对其他网站上的下拉表单的可见性没有任何问题,只有这个。有什么东西可以使表单不可见(如果是,为什么只有 95% 的时间)?加载页面时是否存在问题,可能导致内容不可见?

【问题讨论】:

我注意到您正在使用 PhantomJS。我会尝试的第一件事:这在真正的浏览器中有效吗? 它在 Firefox 中做了同样的事情,但没有真正成功(它仍然有 90% 的时间在不变的代码上失败)。 Selenium - Python - drop-down menu option value的可能重复 【参考方案1】:

在 Python3 中使用 Selenium Webdriver 我是这样做的:

    all_options = self.driver.find_element_by_id("Component1")
    options = all_options.find_elements_by_tag_name("option")
    for each_option in all_options:
        print(each_option.get_attribute("value"))

如果您尝试从文本框中选择某些内容,请按以下步骤操作:

    select = Select(self.driver.find_element_by_id("Component1"))
    select.select_by_visible_text("02")

参考:http://selenium-python.readthedocs.org/api.html

【讨论】:

打印每个选项都没有问题。它总是有效的。另一方面,如果我尝试选择,那么在 5-10% 的时间里,它毫无例外地有效(如果代码也没有改变的话)。【参考方案2】:

尝试像这样使用 xpath: 使用下面的 xpath 查找元素并单击它。 Xpath://select[@id='Component1']/option[text()='04']

如果上面的代码不起作用,首先点击下拉菜单:

xpath://select[@id='Component1'] 然后点击选项。

【讨论】:

在下拉菜单中有@value not text()【参考方案3】:
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.common.by import By

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID "Component1")))
select = WebDriverWait(driver, 10).until(lambda driver:Select(driver.find_element_by_id("Component1")))
select.select_by_visible_text("Text to look for")

【讨论】:

以上是关于如何在 Selenium 的下拉列表中选择项目的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Selenium 和 Java 从非选择下拉列表中单击并选择一个选项

RobotFramework Selenium:如何从多选下拉列表中选择多个选项?

如何使用 Selenium WebDriver C# 从下拉列表中选择一个选项?

如何使用 Selenium WebDriver C# 从下拉列表中选择一个选项?

如何使用来自具有特殊设置的网站的Selenium从下拉列表中选择值 - Python

selenium python 针对js生成的下拉列表,如何选择隐藏的选项