Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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! 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?

like image 215
Shane Avatar asked May 25 '12 15:05

Shane


People also ask

How do I fix selenium timeout exception?

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.

How does selenium handle connection timeout?

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.

What is the default timeout in selenium WebDriver?

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.

What is script timeout in selenium?

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.


1 Answers

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

like image 196
AutomatedTester Avatar answered Nov 09 '22 18:11

AutomatedTester