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!
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?
Solution. You can manually increase the wait time by hit-and-trial. If the problem persists for a longer period of time, there may be some other issue and you should continue onto the next solution. You can explicitly add wait by using JavaScript Executor.
You should try to increase the timeout to ensure that the objects are available to selenium to work. If you want to handle the error, better include this code in @BeforeTest or @BeforeSuite annotation which ensure the entire test suite will not run if this fails.
Defaults to 30 seconds (or 30,000 ms). When the object is used as input for the Set Timeouts command or as part of the timeouts capability when creating a new session, all fields are optional.
The script timeout error is a WebDriver error that occurs when a script the user has provided did not complete before the session's script timeout duration expired. The script timeout duration is a configurable capability, which means you can change how long it will take before the driver interrupts an injected script.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With