Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop selenium webdriver from waiting for page load?

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()?

like image 204
Michał Witanowski Avatar asked Dec 14 '22 18:12

Michał Witanowski


1 Answers

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.

like image 191
JimEvans Avatar answered Dec 17 '22 09:12

JimEvans