javaseleniumselenium-webdrivertimeoutexceptionpageloadtimeout

How to close the chrome browser right after 20 seconds handling the exception errors?


I am using this code with ChromeDriver and Chrome browser:

driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(20));
Thread.sleep(20000);
driver.quit();

Then webpage keeps loading more than 20 seconds and closing with java.util.concurrent.TimeoutException

But its not working.


Solution

  • You can try this:

    import org.openqa.selenium.TimeoutException;
    
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(20));
    try {
        driver.get("https://www.amazon.com");
    } catch (TimeoutException e) {
    //  e.printStackTrace();
        System.out.println("Quitting...");
        driver.quit();
    }