Open In App

How do I set the Selenium webdriver get timeout using Java?

Last Updated : 13 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Setting timeouts is important for good automated testing in Selenium WebDriver. One important timeout is page load timeout. This controls how long Selenium waits to load a page when visiting a given URL. If there is no timeout, tests may be stuck when pages load slowly.

This results in failed tests. You can have better test execution with correct timeout settings.

How to Set Page Load Timeout in Selenium WebDriver

In Selenium WebDriver, manage().timeouts().pageLoadTimeout() method is used to set maximum time limit for page loading. This timeout make sure that Selenium waits only for some period for page to load after going to a URL. If the page takes longer than set time, Selenium throws a TimeoutException, so test proceed or fail nicely without hanging indefinitely.

Purpose of pageLoadTimeout()

The pageLoadTimeout() method helps to manage time in which Selenium waits for a webpage to load fully. By controlling this, you can handle case when page load slower due to network delay or large resources.

Parameters of pageLoadTimeout()

  • Duration of Timeout: This tells how long Selenium will wait for a page to load. It is represented in numeric form. It tells the unit of time for the timeout like seconds, milliseconds, minutes, etc. The most commonly used unit is seconds.

Example

SeleniumTest.java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.TimeoutException;
import java.util.concurrent.TimeUnit;

public class SeleniumTest{
    public static void main(String[] args) {
        // Set path for the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\hp\\Desktop\\Shalini\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // Setting page load timeout
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

        try {
            driver.get("http://example.com");
            System.out.println("Page loaded successfully.");
        } catch (TimeoutException e) {
            System.out.println("Page load timeout exceeded. Retrying...");
            try {
                // Retry logic, reload page or handle as necessary
                driver.navigate().refresh();
                driver.get("http://example.com");
                System.out.println("Page loaded on retry.");
            } catch (TimeoutException retryException) {
                System.out.println("Retry failed: " + retryException.getMessage());
            }
        } finally {
            driver.quit();
    }
    }
}

Output

output Selenium webdriver get timeout
Selenium webdriver get timeout

Conclusion

Setting page load timeouts in Selenium WebDriver is an important part of automated testing. It improves the performance of tests. By setting an appropriate timeout period, testers can prevent unnecessary delays due to slow network loads. This ensures that automated tests run smoothly. Since each web application has different performance characteristics, testers should experiment with different timeouts to find the best settings that meet their specific needs.


Next Article
Article Tags :

Similar Reads