我需要使用Python中的selenium webdriver从下拉列表中选择一个元素。为此,我检查了有用的帖子,如Selecting a value from a drop-down option using selenium python和 https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver?lq=1。无法使用selenium webdriver从下拉列表中选择元素
我说的是在接下来的块中显示的元素:
<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)
:发生第二线以下错误ppens但我也试图用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 name元素。
我在上面的块之后,但有另一个块之前引用,而不是div id="dayTab"...
是div id="monthTab"...
显然是不可见的。我如何参考我想要的标签,没有标识。
您是否确切知道页面上是否只有一个元素使用css'select.input-small.input-thin',并且该元素是您的元素?您可以点击另一个具有相同css且不可见的选择。 – LINGS
@LINGS你说得对,这是问题,谢谢。我将编辑我的OP,因为我不知道如何选择正确的。 –