我正在为 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 驱动程序等待元素可访问,而不仅仅是存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!