Skip to content
CodeKruPro

Implicit Wait

35/70

Implicit Wait

An implicit wait tells WebDriver to poll the DOM for a specified amount of time when trying to find an element that is not immediately available.

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(n));

Once set, it applies to all subsequent findElement() calls.

Task:

  1. Set an implicit wait of 5 seconds
  2. Navigate to https://testkru.com/Elements/TextFields
  3. Find the element with ID firstName
  4. Print whether the element was found
  5. Print the implicit wait value you set

Expected output:

Element found: true
Implicit wait set: 5 seconds

About this lesson

Implicit wait in Selenium Java: implicitlyWait with Duration

Learn Selenium JavaIntermediateWaits

How it works

An implicit wait is a session-wide setting that makes findElement() and findElements() keep retrying for up to a set time before they give up. In Selenium 4 you set it with driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)). The old form that took a number and a TimeUnit has been removed, so code copied from older tutorials does not compile.

The default is zero, so a missing element fails at once. With a wait set, an element that is already there is still returned immediately; the wait only costs time when the element is missing. A lookup for an id that never appears takes the full timeout and then throws NoSuchElementException, and findElements() waits the same time before returning an empty list.

It only waits for an element to exist in the DOM. It does not wait for it to be visible, enabled or clickable, which is why the explicit waits in the next lessons exist. Selenium's documentation advises against mixing the two, because the combined timeouts become hard to predict.

About the assignment

This starter opens no page for you. You set the implicit wait before the first navigation, load Text Fields, find the first-name field, and then report both the lookup result and the wait the session is using.

What you will do

  1. Set the implicit wait through driver.manage().timeouts(), passing a Duration of the length the task gives.
  2. Load the Text Fields page and find the field with id firstName.
  3. Print whether the element was found after the label Element found:.
  4. Read the wait back with getImplicitWaitTimeout(), convert the Duration to whole seconds, and print it in the format the task shows.

Common mistakes

  • Using the removed TimeUnit overload

    A call with a number and TimeUnit.SECONDS no longer compiles in Selenium 4. Pass a Duration.

  • Printing the Duration directly

    getImplicitWaitTimeout() returns a Duration, which prints as PT5S. Use toSeconds() or getSeconds() to get the number.

  • Testing the result for null

    findElement() never returns null; it throws when nothing is found. A != null check is always true when it runs, so the real proof that the element exists is that no exception was thrown.

  • Expecting it to wait for visibility

    Hidden elements are found at once. invisibleField on this page is returned straight away even though isDisplayed() is false.

In a real test suite

Many teams set no implicit wait at all and use explicit waits everywhere, because an implicit wait also slows down every check for absence: each findElements() that expects an empty list waits the full timeout first.

Lesson 34 read selection state. From lesson 36 on, WebDriverWait waits for specific conditions such as visibility instead of mere presence.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        
        // TODO: Set implicit wait to 5 seconds
        // TODO: Navigate to the page
        // TODO: Find firstName element and print if found
        // TODO: Print the wait duration
        
        driver.quit();
    }
}