Tell me why this does not end up with a timeout error (selenium 2 webdriver)?
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?!"
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!
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?
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.
This will probably be in Selenium 2.22 for Python and then your test case will likely fail once that is set
The Until method on webdriver wait ignores the element not found exception and other exceptions which occures in the condition you specify, for the time period you specify. After the given time, you would initially be getting a no such element exception if you dont have the element present and then a timeout exception if you handle the 'no such element' exception(preferably in a try catch).
For your need you could try a work around this way-
-> Bring the focus to the button after which there is a page load -> Fire the click with java code(not webdriver. since clicks will wait for the next page to load.) -> Put a thread.sleep for a second or two -> check for the element's prescense.
链接地址: http://www.djcxy.com/p/59828.html