0
0
Selenium Javatesting~5 mins

Implicit wait in Selenium Java

Choose your learning style9 modes available
Introduction

Implicit wait tells the browser to wait a little before giving up on finding something. It helps tests run smoothly when pages load slowly.

When a web page takes time to load elements after navigation.
When elements appear after some delay due to animations or scripts.
When you want a simple way to wait for elements without writing extra code.
When you expect small delays but don't want to pause the whole test.
When you want to avoid test failures caused by temporary slow loading.
Syntax
Selenium Java
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

This sets the wait time to 10 seconds for all element searches.

Use Duration.ofSeconds() to specify the wait time in seconds.

Examples
Waits up to 5 seconds for elements before failing.
Selenium Java
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
Waits up to 0.5 seconds (500 milliseconds) for elements.
Selenium Java
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
Waits up to 1 minute for elements, useful for very slow pages.
Selenium Java
driver.manage().timeouts().implicitlyWait(Duration.ofMinutes(1));
Sample Program

This test opens a page where a button appears after some delay. The implicit wait tells Selenium to wait up to 10 seconds for the button before failing. If the button appears in time, the test prints success.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;

public class ImplicitWaitExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        // Set implicit wait of 10 seconds
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        driver.get("https://example.com/delayed_element");

        // Try to find element that appears after delay
        WebElement delayedElement = driver.findElement(By.id("delayedButton"));

        if (delayedElement.isDisplayed()) {
            System.out.println("Element found and visible.");
        } else {
            System.out.println("Element not visible.");
        }

        driver.quit();
    }
}
OutputSuccess
Important Notes

Implicit wait applies to all element searches after it is set.

Setting implicit wait once is enough; it stays until changed or driver quits.

Implicit wait can slow tests if set too high; use reasonable times.

Summary

Implicit wait helps tests wait for elements to appear without extra code.

It sets a global wait time for all element searches.

Use it to handle small delays in page loading smoothly.