The page I am testing, it's loading forever due to the slow network(or Internet censorship, some of the resources are bound to fail loading). However most of elements of the page are present within 1 second.
Since Selenium won't have an element clicked until the page's fully loaded, I want to use pageLoadTimeout() to stop the page from loading after 5 seconds and then handle the timeout and do something on the page. And the code works for the page which I specified in get(url).
try {
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
driver.get(url);
}
catch (TimeoutException e){
}
finally {
driver.findElement(By.xpath("xpath here")).click();
}
However this won't work if you're trying to click a link and then be navigated to a new page. You're will soon get
org.openqa.selenium.TimeoutException
because the code above didn't handle this new page's timeout exception. Then I'll have to add another try-catch-finally to prevent this new page from infinitely loading. This will get trickier if there're a lot of links you have to go thru before you are actually reaching the page you wanna test.
Does anyone has a solution to this?