Tell me why it did not end 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 no timeout error, why?!" 

OK, as you can see that even I set the wait time to 0.1 s, but still no timeout exception was thrown. When element.click() is executed, it is not blocked until the whole page loads, and why Just clicked! And I'm expecting timeout error! Just clicked! And I'm expecting timeout error! appeared, and to my surprise new_element = WebDriverWait(browser, 0.1).until(lambda browser : browser.find_element_by_id("element")) wait for the entire page to load. And if you use implicit waits , you will get the same result.

My point is, sometimes when you click on an item, it may even take an hour to load the page due to a bad proxy server, and you obviously DO NOT want , want to wait a long time, what you want is an exception. In this case, how would you work it?

+7
source share
2 answers

Clicks have an implicit wait built into them to wait for the page to load. Only work works in FirefoxDriver that allows you to set how long Selenium should wait for the page to load.

This will probably be in Selenium 2.22 for Python, and then your test script will most likely work if installed

+4
source

The Until on webdriver wait method ignores the exception of an element that is not found, and other exceptions that occur in the condition you specify during the period you specify. After the set time has elapsed, you first get an exception for such an element if you don’t have an element present, and then a timeout exception if you are handling the "no such element" exception (preferably in try catch).

For your need, you can try working around this path -

-> Lift focus on the button after which the page loads -> Fire click using java code (not webdriver, since clicks will wait for the next page to load). -> Put thread.sleep for a second or two -> check that the element is present.

0
source

All Articles