如何在Selenium中选择下拉菜单项

4 投票
3 回答
13603 浏览
提问于 2025-04-18 09:21

首先,我一直在尝试从这个网页获取下拉菜单: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%的时间是这样)?是不是加载页面时出现了什么问题,导致某些东西无法显示?

3 个回答

0

试着用下面的xpath来找到元素并点击它:

Xpath: //select[@id='Component1']/option[text()='04']

如果上面的代码不管用,先用下面的代码点击下拉框:

Xpath: //select[@id='Component1']

然后再点击选项。

1

在编程中,有时候我们会遇到一些问题,像是代码运行不正常或者出现错误。这时候,我们可以去一些技术论坛,比如StackOverflow,去寻找答案或者请教别人。

在这些论坛上,很多人会分享他们的经验和解决方案。比如,有人可能会提供一段代码,告诉你如何解决某个特定的问题。你可以把这些代码复制到自己的项目中,看看能否解决你的困扰。

另外,论坛上还有很多人会提问,描述他们遇到的具体问题。其他人会根据这些描述,给出建议和解决方法。这种互动可以帮助大家更好地理解编程中的各种难题。

总之,StackOverflow是一个很好的资源,可以帮助你在编程的路上少走弯路,找到解决问题的办法。

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")
5

这是我用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

撰写回答