简体   繁体   English

无法使用Selenium WebDriver从下拉列表中选择元素

[英]Unable to select an element from a dropdown list with selenium webdriver

I need to select an element from a dropdown list using selenium webdriver in Python. 我需要在Python中使用Selenium Webdriver从下拉列表中选择一个元素。 For that I have checked helpful posts such as Selecting a value from a drop-down option using selenium python and https://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver?lq=1 . 为此,我检查了有用的帖子,例如使用Selenium pythonhttps://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in- 从下拉选项中选择值 selenium-webdriver?lq = 1

The element i am talking about is shown in the next block: 我正在谈论的元素显示在下一个块中:

<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>

I tried Select() : 我尝试了Select()

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

Although it locates the element (which it does), i then get the following error which occurs for the second line: 尽管它找到了元素(确实如此),但随后我在第二行得到了以下错误:

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)

I am not sure why this happens but i have also tried to use Click() instead in order to "open" the drop down list 我不确定为什么会发生这种情况,但我也尝试使用Click()来“打开”下拉列表

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

and that the elements are visible but then I get this: 并且元素可见,但是然后我得到了:

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)

Why is element still not visible if I am able to locate the drop-down list and select it? 如果我能够找到下拉列表并选择它,为什么元素仍然不可见?

EDIT : 编辑

After LINGS comment I realised that there is not only one element with the css name that I 've used. LINGS评论之后,我意识到我使用的不仅只有一个具有CSS名称的元素。

I am after the above block but there is another block referenced before that one where instead of div id="dayTab"... is div id="monthTab"... which obviously is invisible. 我在上述代码块之后,但是在此之前引用了另一个代码块,其中不是div id="dayTab"...而是div id="monthTab"...这显然是不可见的。 How can I refer to the tab that I want, there's no ID. 我该如何参考我想要的标签,没有ID。

It was quite simple after all, I replaced the initial: 毕竟这很简单,我替换了最初的字母:

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

with this: 有了这个:

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

It is only a matter of finding the correct CSS (or XPath). 只是找到正确的CSS(或XPath)而已。 Chrome add-ons such as XPath Helper may help in this. Chrome插件(例如XPath Helper)可能会对此有所帮助。 Other tips on CSS selectors you may find in here . 有关CSS选择器的其他技巧,您可以在这里找到。 Glad in case I've helped other users avoid such annoying mistakes. 很高兴我帮助其他用户避免了此类令人讨厌的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium webdriver 无法在下拉列表中定位元素 - Selenium webdriver unable to locate element in a dropdown list 无法在Python中使用Selenium从下拉菜单中通过xpath选择元素 - Unable to select element by xpath from dropdown menu with Selenium in Python 无法从隐藏的下拉菜单中找到 select 元素 - Python Selenium - Unable to select element from Dropdown that hides - Python Selenium 使用 div class - python Z8E00596AD8DE22136FF8F8D8478 - Unable to select from 3 dropdown list using div class - python selenium Python selenium 从下拉列表中选择第一个元素 - Python selenium to select first element from the dropdown list 我正在尝试使用 selenium webdriver python 隐藏下拉菜单选项 select 但它抛出“消息:没有这样的元素:无法找到元素” - i am trying to select hidden dropdown menu option using selenium webdriver python but it throw "Message: no such element: Unable to locate element" 无法从带有 Selenium 的下拉菜单中找到或 select select 元素 - 元素不可见 - Unable to locate or select a select element from dropdown menu with Selenium - element not visible 无法使用 Selenium 选择已选择的下拉列表 - Unable to select Chosen dropdown list with Selenium Selenium Webdriver - Python - leboncoin (classified) - pb 选择下拉列表和框 - Selenium Webdriver - Python - leboncoin (classified) - pb to select a dropdown list & box 无法通过Selenium Webdriver python中的XPath选择元素 - Unable to select an element via XPath in selenium Webdriver python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM