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 驱动程序等待元素可访问,而不仅仅是存在?
基础教程推荐
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
