Rick
Rick

Reputation: 826

Best way to deal with slow pageloads after GET using Selenium

What is the best way to deal with driver->get(URL) and slow page load?

I basically have a for loop that runs 3 times at most. Within that loop, I have a try/catch block. In the try block, it loads the page with get OR

wait()->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::xPath("//body")));

If the get or the wait doesn't work, the page refreshes or the same get command is executed.

Is that truly the best way? I wonder how you guys deal with that? A page that doesn't load properly seems to be one of the main reasons my script regularly doesn't execute fully.

Upvotes: 0

Views: 1519

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193308

presenceOfElementLocated() for BODY won't ensure if the page have completely loaded.

An ideal solution to slow pageloads would be to set pageLoadTimeout() through a try-catch{} block as follows:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
try {
    driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl");
} catch (TimeoutException e) {
    System.out.println("TimeoutException occurred. Quiting the program.");
}
driver.quit();

References

You can find a couple of relevant detailed discussions on pageLoadTimeout() in:

Upvotes: 0

Related Questions