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
- Set the implicit wait through
driver.manage().timeouts(), passing aDurationof the length the task gives. - Load the Text Fields page and find the field with id
firstName. - Print whether the element was found after the label
Element found:. - Read the wait back with
getImplicitWaitTimeout(), convert theDurationto 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.SECONDSno longer compiles in Selenium 4. Pass aDuration.Printing the Duration directly
getImplicitWaitTimeout()returns aDuration, which prints asPT5S. UsetoSeconds()orgetSeconds()to get the number.Testing the result for null
findElement()never returnsnull; it throws when nothing is found. A!= nullcheck 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.
invisibleFieldon this page is returned straight away even thoughisDisplayed()isfalse.
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.
Related lessons
- Explicit Wait - Element VisibleExplicit waits for one condition on one element.
- Find Multiple Elements with findElements()
findElements(), which also waits the full implicit timeout when nothing matches. - Handle Dynamic Elements (Appearing Content)Content that appears after page load, handled with explicit waits.
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.
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();
}
}