问题描述
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Firefox()
browser.get("http://testsite.com")
element = WebDriverWait(browser, 10).until(lambda browser : browser.find_element_by_id("element"))
element.click() # it actually goes to page http://testsite.com/test-page.html
print "Just clicked! And I'm expecting timeout error!"
new_element = WebDriverWait(browser, 0.1).until(lambda browser : browser.find_element_by_id("element"))
print "Too bad there's no timeout error, why?!"
好的,您可以看到,即使我将等待时间设置为0.1秒,也不会抛出超时异常.执行element.click()
时,它不会阻塞,直到整个页面加载完毕,这就是Just clicked! And I'm expecting timeout error!
出现的原因,令我惊讶的是,new_element = WebDriverWait(browser, 0.1).until(lambda browser : browser.find_element_by_id("element"))
等到整个页面加载完毕.而且,如果您使用implicit waits
,则会得到相同的结果.
OK, as you can see even I set wait time to 0.1 sec there's still no timeout exception thrown. When element.click()
executed it does not block till the whole page loads up and that's why Just clicked! And I'm expecting timeout error!
showed up, and to my surprise new_element = WebDriverWait(browser, 0.1).until(lambda browser : browser.find_element_by_id("element"))
wait till the whole page loads up. And if you use implicit waits
, you get the same result.
我的意思是,有时单击一个元素后,由于代理不正确,页面加载可能甚至需要几个小时,而且您显然不要要等待那么长时间,您想要的是超时异常.在这种情况下,您将如何使其工作?
My point is, sometimes after you click an element it might take up to even hours for a page to load up because of a bad proxy, and you obviously DO NOT want to wait that long, what you want is a timeout exception. In this case how would you make it work?
推荐答案
点击具有内置的隐式等待,以等待页面加载.有一项工作(当前仅在FirefoxDriver中完成)可以让您设置Selenium等待页面加载的时间.
Clicks have an implicit wait built into them to wait for when the page is loaded. There is work, currently complete in FirefoxDriver only, that allows you to set how long Selenium should wait for a page to load.
这可能在Python的Selenium 2.22中,一旦设置,您的测试用例就可能失败
This will probably be in Selenium 2.22 for Python and then your test case will likely fail once that is set
这篇关于告诉我为什么这不会导致超时错误(Selenium 2 WebDriver)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!