How can I get Selenium Web Driver to wait for an element to be accessible, not just present?(如何让 Selenium Web 驱动程序等待元素可访问,而不仅仅是存在?)
问题描述
我正在为 Web 应用程序编写测试.一些命令会弹出对话框,这些对话框具有可见的控件,但有一段时间不可用.(它们是灰色的,但 webdriver 仍然认为它们是可见的).
I am writing tests for a web application. Some commands pull up dialog boxes that have controls that are visible, but not available for a few moments. (They are greyed out, but webdriver still sees them as visible).
我如何告诉 Selenium 等待元素实际可访问,而不仅仅是可见?
How can I tell Selenium to wait for the element to be actually accessible, and not just visible?
try:
print "about to look for element"
element = WebDriverWait(driver, 10).until(lambda driver : driver.find_element_by_id("createFolderCreateBtn"))
print "still looking?"
finally: print 'yowp'
这是我尝试过的代码,但它在按钮可用之前看到"按钮,并且基本上在假定的等待"之后收费.
Here is the code that I have tried, but it "sees" the button before it is usable and basically charges right past the supposed "wait".
请注意,我可以在代码中填充 10 秒的睡眠时间,而不是这样,代码会正常工作,但这是丑陋、不可靠且效率低下的.但它确实证明了问题只是点击"命令在控件的可用性之前竞争.
Note that I can stuff a ten second sleep into the code instead of this and the code will work properly, but that is ugly, unreliable, and inefficient. But it does prove that the problem is just that "click" command is racing ahead of the availability of the controls.
推荐答案
print time.time()
try:
print "about to look for element"
def find(driver):
e = driver.find_element_by_id("createFolderCreateBtn")
if (e.get_attribute("disabled")=='true'):
return False
return e
element = WebDriverWait(driver, 10).until(find)
print "still looking?"
finally: print 'yowp'
print "ok, left the loop"
print time.time()
这就是我们最终的结果.(感谢 lukeis 和 RossPatterson.)请注意,我们必须按 id 查找所有项目,然后按禁用"过滤.我更喜欢单一的搜索模式,但你能做什么?
Here is what we ended up with. (Thanks to lukeis and RossPatterson.) Note that we had to find all the items by id and then filter by "disabled". I would have preferred a single search pattern, but what can you do?
这篇关于如何让 Selenium Web 驱动程序等待元素可访问,而不仅仅是存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何让 Selenium Web 驱动程序等待元素可访问,而不仅仅是存在?
基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01