Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python从Selenium中的下拉列表中选择未显示的选项_Python_Selenium_Html Select - Fatal编程技术网

如何使用Python从Selenium中的下拉列表中选择未显示的选项

如何使用Python从Selenium中的下拉列表中选择未显示的选项,python,selenium,html-select,Python,Selenium,Html Select,我正在尝试编写一个Selenium驱动程序来测试使用下拉(组合)检查列表的网页。下面的代码显示了该问题 #!/usr/bin/python from selenium import webdriver from selenium.webdriver.support.ui import Select driver = webdriver.Firefox() driver.get("http://dropdown-check-list.googlecode.com/svn/trunk/doc/

我正在尝试编写一个Selenium驱动程序来测试使用下拉(组合)检查列表的网页。下面的代码显示了该问题

#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()

driver.get("http://dropdown-check-list.googlecode.com/svn/trunk/doc/ddcl-tests.html")

selector = driver.find_element_by_id("s1")

allOptions = selector.find_elements_by_tag_name("option")

for option in allOptions:
    print "Value is", option.get_attribute("value")
    option.click()
当我运行它时,我得到以下输出:

Value is Low
Traceback (most recent call last):
  File "./ddcl-test.py", line 24, in <module>
option.click()
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 51, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 225, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 160, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 149, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace: Method fxdriver.preconditions.visible threw an error in file:///var/folders/d4/qbgb29wx7z7fpr15t___x24h0000gn/T/tmpBzUUcu/extensions/fxdriver@googlecode.com/components/command_processor.js 
值较低
回溯(最近一次呼叫最后一次):
文件“/ddcl test.py”,第24行,在
选项。单击()
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webelement.py”,第51行,单击
self.\u执行(命令。单击\u元素)
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webelement.py”,第225行,在
返回self.\u parent.execute(命令,参数)
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/webdriver.py”,第160行,在execute中
self.error\u handler.check\u响应(响应)
文件“/Library/Python/2.7/site packages/selenium/webdriver/remote/errorhandler.py”,第149行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.ElementNotVisibleException:消息:u'元素当前不可见,因此可能无法与';Stacktrace:方法fxdriver.Premissions.visible在中引发错误file:///var/folders/d4/qbgb29wx7z7fpr15t___x24h0000gn/T/tmpBzUUcu/extensions/fxdriver@googlecode.com/components/command_processor.js
它无法单击元素,因为它未显示


我如何解决这个问题?或者这在Selenium中是一个不稳定的例子吗?

我已经尝试了无数次去点击那些不可见的元素,但这在Selenium中是不可行的,或者至少我还没有找到一种方法。看起来你并不是想先点击下拉按钮,所以我建议你先点击下拉菜单按钮来显示菜单,我认为这会让你克服你的问题

问题在于jQuery故意隐藏了您试图访问的

<select id="s1" class="s1" tabindex="8" multiple="" style="display: none;">
    <option>Low</option>
    <option>Medium</option>
    <option>High</option>
</select>

低
中等
高
WebDriver不会单击隐藏的元素。时期这是故意的,因为最终用户也不能点击它。WebDriver不想让你做人类做不到的事情

相反,您必须像人类一样与浏览器交互:单击jQuery向人类公开的任何元素。对于本例,用户界面为:

<span id="ddcl-s1" class="ui-dropdownchecklist ui-dropdownchecklist-selector-wrapper ui-widget" style="display: inline-block; cursor: default; overflow: hidden;">
    <span class="ui-dropdownchecklist-selector ui-state-default" style="display: inline-block; overflow: hidden; white-space: nowrap; width: 85px;" tabindex="8">
        <span class="ui-dropdownchecklist-text" style="display: inline-block; white-space: nowrap; overflow: hidden; width: 81px;" title=" "> 
        </span>
    </span>
</span>
<div id="ddcl-s1-ddw" class="ui-dropdownchecklist ui-dropdownchecklist-dropcontainer-wrapper ui-widget" style="position: absolute; left: -33000px; top: -33000px; height: 74px; width: 91px;">
    <div class="ui-dropdownchecklist-dropcontainer ui-widget-content" style="overflow-y: auto; height: 74px;">
        <div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
            <input id="ddcl-s1-i0" class="active" type="checkbox" tabindex="8" disabled="" index="0" value="Low">
            <label class="ui-dropdownchecklist-text" for="ddcl-s1-i0" style="cursor: default;">Low</label>
        </div>
        <div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
            <input id="ddcl-s1-i1" class="active" type="checkbox" tabindex="8" disabled="" index="1" value="Medium">
            <label class="ui-dropdownchecklist-text" for="ddcl-s1-i1" style="cursor: default;">Medium</label>
        </div>
        <div class="ui-dropdownchecklist-item ui-state-default" style="white-space: nowrap;">
            <input id="ddcl-s1-i2" class="active" type="checkbox" tabindex="8" disabled="" index="2" value="High">
            <label class="ui-dropdownchecklist-text" for="ddcl-s1-i2" style="cursor: default;">High</label>
        </div>
    </div>
</div>

低
中等
高
因此,与之交互的东西似乎是
元素之一,但确实不容易确定


这就是为什么我们中的一些人认为从span和div重构现有HTML功能的JavaScript框架是一个非常糟糕的主意。

单击下拉菜单本身,以便在单击选项之前显示菜单。我试图通过调用
selector.Click()来单击选择器
但我在这次通话中仍然遇到同样的异常。整个选择器怎么可能不可见?这可能是时间问题吗?我遇到了需要等待下拉菜单出现并填充的问题。尝试使用WebDriverWait调用,看看是否有帮助