4

Sometimes when i run a test, website never stops loading and my test stucks on it. How can i set driver.get() method to not wait for page load? If its impossible are there any work arounds or other methods that could replace driver.get()?

4
  • 2
    The first question you should ask yourself is why your website doesn't stop loading? Commented Nov 14, 2014 at 21:09
  • How are you going to test a site that never stops loading? Commented Nov 14, 2014 at 22:20
  • It's not my website and i dont need it fully loaded, i just need several controls/buttons to load. Commented Nov 14, 2014 at 22:29
  • If site loading slow it self then no control / method of selenium can help to solve issue..have to wait till site loads. Commented Nov 18, 2014 at 10:20

2 Answers 2

12

The easiest way to exit the page load wait early is to set the page load timeout. When the timeout expires, you can catch the TimeoutException and proceed with the next step of your test. The code to invoke this would look something like the following:

// Set the page load timeout to 10 seconds. driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); try { driver.get("http://url/to/my/slow/loading/page"); } catch (TimeoutException e) { // Ignore the exception. } // Proceed with your next step here. 

Note that you may have to use a WebDriverWait or similar to ensure the element you're interested in is present on the page.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work for me. It keeps waiting for the page complete loading to execute the next command.
0

We can set pageLoadTimeout, once set it wil be there throught the webdriver session, and if the exception is thrown because of timeout then we can't restore same session so need to create new instance.

 WebDriver driver = new FirefoxDriver(); //waits 10 seconds for page to load driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); try { driver.get("https://yourdomain.com"); } catch (TimeoutException e) { driver.close(); driver.quit(); //create new instance of webdriver driver = new FirefoxDriver(); //waits 5 minutes for page to load driver.manage().timeouts().pageLoadTimeout(300, TimeUnit.SECONDS); driver.get("https://yourdomain.com"); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.