【问题标题】:Unable to select an element from a dropdown list with selenium webdriver无法使用 selenium webdriver 从下拉列表中选择元素
【发布时间】:2015-10-15 17:44:01
【问题描述】:

我需要在 Python 中使用 selenium webdriver 从下拉列表中选择一个元素。为此,我查看了有用的帖子,例如 Selecting a value from a drop-down option using selenium pythonhttps://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver?lq=1

我正在谈论的元素显示在下一个块中:

<div id="dayTab" style="height:20px" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
 <select class="input-small input-thin">
   <option value="2010">2010</option>
   <option value="2011">2011</option>
   <option value="2012">2012</option>
   <option value="2013">2013</option>
   <option value="2014">2014</option>
   <option value="2015">2015</option>
 </select>
</div>

我试过Select()

yearselect = Select(browser.find_element_by_css_selector("select.input-small.input-thin"))
yearselect.select_by_value("2010")

虽然它找到了元素(它确实),但我得到了第二行发生的以下错误:

Traceback (most recent call last):
File "C:\Users\elek2\workspace\webdriving\src\gotonch.py", line 119, in <module>
yearselect.select_by_value("2010")
File "C:\Python34\lib\site-packages\selenium\webdriver\support\select.py", line 79, in select_by_value
self._setSelected(opt)
File "C:\Python34\lib\site-packages\selenium\webdriver\support\select.py", line 195, in _setSelected
option.click()
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 74, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 453, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible: Element is not currently visible and may not be manipulated
(Session info: chrome=45.0.2454.101)
(Driver info: chromedriver=2.19.346078 (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.1 SP1 x86_64)

我不确定为什么会发生这种情况,但我也尝试使用Click() 来“打开”下拉列表

yearselect =browser.find_element_by_css_selector("select.input-small.input-thin").click()
yearselect.select_by_value("2010")

并且元素是可见的,但后来我明白了:

Traceback (most recent call last):
File "C:\Users\elek2\workspace\webdriving\src\gotonch.py", line 118, in <module>
yearselect = browser.find_element_by_css_selector("select.input-small.input-thin").click()
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 74, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 453, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=45.0.2454.101)
(Driver info: chromedriver=2.19.346078   (6f1f0cde889532d48ce8242342d0b84f94b114a1),platform=Windows NT 6.1 SP1 x86_64)

如果我能够找到下拉列表并选择它,为什么元素仍然不可见?

编辑

LINGS 发表评论后,我意识到不仅有一个 元素具有我使用过的css 名称。

我在上面的块之后,但在该块之前引用了另一个块,而不是 div id="dayTab"...div id="monthTab"...,这显然是不可见的。我怎样才能引用我想要的选项卡,没有 ID。

【问题讨论】:

  • 您确定页面上是否只有一个带有 css select.input-small.input-thin 的元素并且该元素是您的元素吗?您可能正在单击具有相同 css 且不可见的另一个选择。
  • @LINGS 你是对的,这就是问题所在,谢谢。我将编辑我的 OP,因为我不确定如何选择正确的。

标签: python select drop-down-menu selenium-chromedriver


【解决方案1】:

毕竟还是挺简单的,我把首字母换了:

yearselect = Select(browser.find_element_by_css_selector("select.input-small.input-thin"))
yearselect.select_by_value("2010")

用这个:

yearselect = Select(browser.find_element_by_css_selector("#dayTab > select.input-small.input-thin"))
yearselect.select_by_value("2010")

只需找到正确的 CSS(或 XPath)即可。诸如 XPath Helper 之类的 Chrome 插件可能会对此有所帮助。您可以在here 中找到有关 CSS 选择器的其他提示。很高兴我帮助其他用户避免了这些烦人的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多