Selenium Wait Commands help control the execution speed of automation scripts when web elements do not load immediately. They ensure that Selenium interacts with elements only after they become available on the webpage.
- Waits prevent NoSuchElementException and timing-related errors.
- They improve test execution efficiency on dynamic websites.
- Different wait types are used based on application behavior and loading time.
Types of Wait Commands in Selenium
1. Implicit Wait
Implicit Wait in Selenium tells WebDriver to wait for a specified time while searching for web elements. If the element is not found immediately, Selenium keeps checking until the timeout is completed. Once set, the implicit wait is applied globally to all element searches in the WebDriver session.
- Pros: Implicit Wait is easy to implement and automatically applies to all element searches. It helps handle synchronization delays in dynamic web applications.
- Cons: Implicit Wait cannot handle specific conditions like element visibility or clickability. Using high wait times or mixing it with Explicit Wait may slow execution and cause unpredictable behavior.
Example: Implicit Wait on saucedemo.com
package Tests;
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;
public class ImplicitWaitTest {
public static void main(String[] args) {
// Set up ChromeDriver
WebDriver driver = new ChromeDriver();
try {
// Set Implicit Wait for 10 seconds
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Navigate to Saucedemo
driver.get("https://www.saucedemo.com/");
// Find the username field (Implicit wait will apply here)
WebElement usernameField = driver.findElement(By.id("user-name"));
// Enter username
usernameField.sendKeys("standard_user");
System.out.println("Username entered successfully.");
} finally {
// Close browser
driver.quit();
}
}
}
Output:

2. Explicit Wait
Explicit Wait in Selenium allows WebDriver to wait for a specific condition before performing an action on a web element. It waits only for the targeted element and is mainly used for conditions like visibility, clickability, or presence of elements.
- Pros: Explicit Wait provides better control over dynamic elements and waits only when required. It improves test efficiency by handling specific synchronization conditions.
- Cons: Explicit Wait requires more code compared to Implicit Wait. Using multiple explicit waits in large test scripts can make the code lengthy and harder to maintain.
Example: Explicit Wait on saucedemo.com
package Tests;
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.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
public class ExplicitWaitTest {
public static void main(String[] args) {
// Set up ChromeDriver
WebDriver driver = new ChromeDriver();
try {
// Navigate to Saucedemo
driver.get("https://www.saucedemo.com/");
// Wait up to 10 seconds for username field to be clickable
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement usernameField = wait.until(
ExpectedConditions.elementToBeClickable(By.id("user-name"))
);
// Enter username
usernameField.sendKeys("standard_user");
System.out.println("Username entered successfully.");
} finally {
// Close browser
driver.quit();
}
}
}
Output:

3. Fluent Wait
Fluent Wait in Selenium is an advanced wait mechanism that allows defining the maximum wait time, polling frequency, and exception handling while searching for elements. It repeatedly checks for the element at regular intervals until the condition is met.
- Pros: Fluent Wait offers better flexibility with custom polling intervals and exception handling. It is useful for handling elements that load at irregular time intervals.
- Cons: Fluent Wait is more complex to implement and may increase code complexity. Incorrect polling or timeout settings can affect test performance.
Example: Fluent Wait on saucedemo.com
package Tests;
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.NoSuchElementException;
import java.time.Duration;
public class FluentWaitTest {
public static void main(String[] args) {
// Set up ChromeDriver
WebDriver driver = new ChromeDriver();
try {
// Navigate to Saucedemo
driver.get("https://www.saucedemo.com/");
// Enter invalid credentials and click login
driver.findElement(By.id("user-name")).sendKeys("invalid_user");
driver.findElement(By.id("password")).sendKeys("wrong_password");
driver.findElement(By.id("login-button")).click();
// Fluent wait for error message
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(10)) // Max wait time
.pollingEvery(Duration.ofSeconds(1)) // Check every 1 second
.ignoring(NoSuchElementException.class); // Ignore this exception
// Use a different name for the parameter in the lambda expression
WebElement errorMessage = wait.until(
wd -> wd.findElement(By.cssSelector("h3[data-test='error']"))
);
System.out.println("Error message: " + errorMessage.getText());
} finally {
// Close browser
driver.quit();
}
}
}
Output:

Implicit Wait vs Explicit Wait vs Fluent Wait
| Feature | Implicit Wait | Explicit Wait | Fluent Wait |
|---|---|---|---|
| Definition | Waits globally for all elements. | Waits for a specific condition on an element. | Advanced wait with polling and exception handling. |
| Scope | Global | Specific element | Specific element |
| Condition Based | No | Yes | Yes |
| Polling Frequency | Default | Default | Customizable |
| Exception Handling | No | Limited | Yes |
| Flexibility | Low | Medium | High |
| Performance | May slow execution | More efficient | Highly flexible |
| Complexity | Simple | Moderate | Advanced |
| Best Use Case | Simple synchronization | Dynamic elements | Frequently changing elements |