Implicit and Explicit Wait in Selenium with Syntax
โก Smart Summary
Selenium Wait commands synchronize automation scripts with dynamic page behavior, preventing test failures caused by elements that load asynchronously. This reference explains Implicit, Explicit, and Fluent Wait strategies with WebDriver syntax, working Java code samples, and practical guidance on selecting the appropriate wait for stable, reliable test execution.

In Selenium, “Waits” play an important role in executing reliable tests. In this tutorial, you will learn the key aspects and differences between Implicit and Explicit Wait commands in Selenium, along with practical Fluent Wait usage.
Why Do We Need Waits In Selenium?
Most modern web applications are developed using Ajax and JavaScript. When a page is loaded by the browser, the elements that we want to interact with may load at different time intervals.
Not only does this make element identification difficult, but if the element is not located in time, Selenium will throw an ElementNotVisibleException. Selenium Waits resolve this synchronization problem by giving the WebDriver a defined window in which to find an element before failing.
Consider a scenario in which both implicit and explicit waits are used in the same test. Assume the implicit wait is set to 20 seconds and the explicit wait is set to 10 seconds.
If we are looking for an element governed by certain ExpectedConditions (Explicit Wait) and the element is not located within the explicit window of 10 seconds, the WebDriver falls back to the implicit window of 20 seconds before throwing an ElementNotVisibleException.
Selenium WebDriver Waits
- Implicit Wait
- Explicit Wait
- Fluent Wait
Implicit Wait in Selenium
The Implicit Wait in Selenium instructs the WebDriver to wait for a defined duration before throwing a NoSuchElementException. The default value is 0. Once configured, the WebDriver polls the DOM during that duration before failing. Selenium WebDriver inherited the concept of implicit waits from Watir.
In the example below, we declare an implicit wait with a duration of 10 seconds. If the element is not located within that window, an exception is raised.
Implicit Wait Syntax (Selenium 4):
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
package guru.test99;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class AppTest {
protected WebDriver driver;
@Test
public void guru99tutorials() throws InterruptedException {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
String eTitle = "Demo Guru99 Page";
String aTitle = "";
// launch Chrome and redirect it to the Base URL
driver.get("https://demo.guru99.com/test/guru99home/");
// Maximize the browser window
driver.manage().window().maximize();
// get the actual value of the title
aTitle = driver.getTitle();
// compare the actual title with the expected title
if (aTitle.equals(eTitle)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
// close browser
driver.close();
}
}
Explanation of Code
In the above example, consider the following statement:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Selenium 4 replaced the older two-argument signature (integer plus TimeUnit) with a single Duration argument. The Duration class supports seconds, minutes, milliseconds, and other time units through static helpers such as Duration.ofSeconds, Duration.ofMillis, and Duration.ofMinutes.
Explicit Wait in Selenium
The Explicit Wait in Selenium instructs the WebDriver to wait for specific conditions (Expected Conditions) or a maximum duration before throwing an ElementNotVisibleException. It is a smarter wait because it applies only to the specified element. Explicit Wait is preferred over Implicit Wait whenever the test needs to handle dynamically loaded Ajax content.
Once an explicit wait is declared, you must use ExpectedConditions, or you can configure polling frequency using Fluent Wait. Avoid using Thread.sleep(), as it pauses execution unconditionally and is generally considered bad practice.
In the example below, we create a reference of the WebDriverWait class, instantiate it with the WebDriver reference, and configure a maximum window of 20 seconds.
Explicit Wait Syntax (Selenium 4):
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
package guru.test99;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
public class AppTest2 {
protected WebDriver driver;
@Test
public void guru99tutorials() throws InterruptedException {
driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
String eTitle = "Demo Guru99 Page";
String aTitle = "";
// launch Chrome and redirect it to the Base URL
driver.get("https://demo.guru99.com/test/guru99home/");
// Maximize the browser window
driver.manage().window().maximize();
// get the actual value of the title
aTitle = driver.getTitle();
// compare the actual title with the expected title
if (aTitle.contentEquals(eTitle)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
WebElement guru99seleniumlink;
guru99seleniumlink = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")));
guru99seleniumlink.click();
}
}
Explanation of Code
Consider the following code:
WebElement guru99seleniumlink;
guru99seleniumlink = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")));
guru99seleniumlink.click();
In this WebDriverWait example, the script waits up to 20 seconds, as defined by the WebDriverWait class, until the ExpectedConditions are satisfied โ whichever occurs first.
The above Java code waits up to 20 seconds for the condition visibilityOfElementLocated to be true.
The following Expected Conditions can be used in Selenium Explicit Wait:
- alertIsPresent()
- elementSelectionStateToBe()
- elementToBeClickable()
- elementToBeSelected()
- frameToBeAvailableAndSwitchToIt()
- invisibilityOfTheElementLocated()
- invisibilityOfElementWithText()
- presenceOfAllElementsLocatedBy()
- presenceOfElementLocated()
- textToBePresentInElement()
- textToBePresentInElementLocated()
- textToBePresentInElementValue()
- titleIs()
- titleContains()
- visibilityOf()
- visibilityOfAllElements()
- visibilityOfAllElementsLocatedBy()
- visibilityOfElementLocated()
Fluent Wait in Selenium
The Fluent Wait in Selenium defines the maximum duration for the WebDriver to wait for a condition, along with the polling frequency for verifying that condition before throwing an ElementNotVisibleException. Fluent Wait checks for the element at regular intervals until the object is found or the timeout expires.
Frequency: The repeat cycle for evaluating the condition at regular intervals.
Consider a scenario where an element loads at unpredictable intervals. The element may load in 10 seconds, 20 seconds, or longer. If we declare an explicit wait of 20 seconds, the script will wait for the full duration before failing. In such cases, Fluent Wait is the ideal choice because it polls at the specified frequency until the element is located or the final timer expires.
Fluent Wait Syntax (Selenium 4):
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
The older signature that accepted integer values plus TimeUnit was deprecated in Selenium 3.11 and removed in Selenium 4. The Duration-based API shown above is the current recommended approach.
package guru.test99;
import java.time.Duration;
import java.util.NoSuchElementException;
import java.util.function.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.testng.annotations.Test;
public class AppTest3 {
protected WebDriver driver;
@Test
public void guru99tutorials() throws InterruptedException {
String eTitle = "Demo Guru99 Page";
String aTitle = "";
driver = new ChromeDriver();
// launch Chrome and redirect it to the Base URL
driver.get("https://demo.guru99.com/test/guru99home/");
// Maximize the browser window
driver.manage().window().maximize();
// get the actual value of the title
aTitle = driver.getTitle();
// compare the actual title with the expected title
if (aTitle.contentEquals(eTitle)) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
}
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
WebElement clickseleniumlink = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"));
}
});
// click on the selenium link
clickseleniumlink.click();
// close browser
driver.close();
}
}
Explanation of Code
Consider the following code:
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
In the above example, we declare a Fluent Wait with a timeout of 30 seconds, a polling frequency of 5 seconds, and ignore NoSuchElementException.
Consider the following code:
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"));
}
This function identifies the WebElement on the page (here, the Selenium link on the homepage). With a polling interval of 5 seconds and a timeout of 30 seconds, the wait checks for the element every 5 seconds for a maximum of 30 seconds. If the element is located within that window, the script proceeds. Otherwise, an ElementNotVisibleException is thrown.
Also Check: Selenium IDE Tutorial for Beginners
Difference Between Implicit Wait Vs Explicit Wait
The table below highlights the key differences between Implicit Wait and Explicit Wait in Selenium.
| Implicit Wait | Explicit Wait |
|---|---|
| Implicit Wait time is applied to all elements in the script. | Explicit Wait time is applied only to the elements specifically targeted. |
| In Implicit Wait, ExpectedConditions are not specified for the element. | In Explicit Wait, ExpectedConditions must be defined for the element. |
| Recommended when elements load within the configured implicit wait window. | Recommended when elements take longer to load and when validating element states such as visibilityOfElementLocated, elementToBeClickable, or elementToBeSelected. |
| Set once and applied for the WebDriver instance lifecycle. | Set per element or per check, providing finer control. |
How to Choose the Right Wait Strategy
Choosing the correct wait command directly affects script stability and execution time. Use this decision framework to select the appropriate option for each scenario.
- Identify the load pattern: Check whether the element appears at a fixed time, after Ajax response, or at unpredictable intervals. Fixed loads suit Implicit Wait, whereas unpredictable loads call for Fluent Wait.
- Avoid mixing Implicit and Explicit Waits: Combining the two can cause unexpected delays because the longer wait may dominate. The Selenium documentation recommends using only one strategy per WebDriver session.
- Default to Explicit Wait for dynamic content: When pages depend on Ajax, conditional rendering, or animations, Explicit Wait with ExpectedConditions targets exactly the element that matters.
- Use Fluent Wait for variable timing: When element load timing varies between runs, configure Fluent Wait with a polling interval. Polling reduces total wait time compared with a single long timeout.
- Replace Thread.sleep: Hardcoded sleeps slow down test suites and hide real timing issues. Replace them with conditional waits to keep execution fast and deterministic.
- Tune timeout values: Start with short timeouts (5โ10 seconds) and increase only when failures show genuine slow load. Long default timeouts mask performance regressions.
- Keep waits in helper utilities: Centralize wait logic in a helper class so that timeout policy changes apply across the entire test suite.
By following this framework, automation engineers can build resilient scripts that adapt to real application timing without becoming slow or brittle.
Common Mistakes to Avoid With Selenium Waits
Even experienced testers fall into a small set of recurring traps when working with Selenium waits. Avoiding these issues keeps scripts fast, deterministic, and easy to maintain across teams and environments.
The first common mistake is overusing Thread.sleep. A hardcoded pause forces the script to wait for the full duration regardless of actual element availability, which inflates total run time. Replace such pauses with Explicit or Fluent Wait so the script proceeds the moment the condition is met.
The second mistake is mixing Implicit and Explicit Waits in the same test. The two mechanisms can compound, creating unpredictable timeouts that are hard to debug. Pick one strategy per WebDriver session and apply it consistently.
A third mistake is setting timeouts too high “just in case.” Long default windows hide genuine performance problems and slow down feedback in continuous integration pipelines. Begin with short timeouts and extend them only when failure logs prove a real timing issue exists.
Also Check: Selenium Tutorial for Beginners: Learn WebDriver in 7 Days
