Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Learn to implement Explicit Wait command in Selenium with WebDriverWait class in Java. Improve web testing by precisely waiting for elements!
Vipul Gupta
April 27, 2026
WebDriverWait in Selenium is used to apply explicit waits, allowing your test to pause until a specific condition is met, such as an element becoming clickable or visible.
Example of Selenium Webdriverwait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(
ExpectedConditions.elementToBeClickable(By.id("login"))
);
element.click();In this guide, we’ll focus on Java examples, but the same WebDriverWait concept applies across other Selenium bindings like Python and C#.
Key Takeaways
On many web pages, some elements like images, forms, or buttons load slower than others. This is a common challenge handled using Selenium waits. If a test tries to interact with these elements too early, it can fail. Selenium WebDriverWait helps solve this problem by pausing the test until a specific condition is met, such as an element becoming visible or clickable. It works by checking the element repeatedly until it is ready or the timeout is reached.
It works by polling the Document Object Model (DOM) at regular intervals to check if the condition is met for the given WebElement within the specified timeout period.
WebDriverWait is usually used with ExpectedConditions, which define what Selenium should wait for, such as an element becoming visible or clickable.
WebDriverWait is used with ExpectedConditions, which define what Selenium should wait for before proceeding with the test. These conditions help ensure that elements are in the correct state before any interaction is performed.
Below are some of the most commonly used ExpectedConditions in Selenium:
Waits until the element is visible and clickable.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.id("login")));Waits until the element is visible on the page.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));Waits until the element is present in the DOM, but not necessarily visible.
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("password")));Waits until the element is no longer visible on the page.
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loader")));Waits until a specific text is present inside an element.
wait.until(ExpectedConditions.textToBePresentInElement(
driver.findElement(By.id("message")), "Success"
));Waits until an alert is present on the screen.
wait.until(ExpectedConditions.alertIsPresent());ExpectedConditions provide a simple and effective way to use WebDriverWait by offering predefined methods for common scenarios. This makes test scripts more reliable and easier to maintain.
Having understood the Selenium WebDriverWait, let’s move forward and learn how to use WebDriverWait in Selenium Java in test scripts. We will cover some of the most commonly used explicit wait implementations using the ExpectedConditions.
In this blog on using WebDriverWait in Selenium Java, we will use RemoteWebDriver for test execution. Using RemoteWebDriver allows you to harness the capabilities of the cloud grid, including speedy and parallel test execution in a more organized way.
Let’s use TestMu AI, an AI-powered test execution platform that offers an online Selenium Grid, enabling developers and testers to perform automation testing for websites and web applications on a wide range of real browsers, devices, and operating system combinations.
Note: Run Selenium tests on real browsers and operating systems. Try TestMu AI Today!
For demonstration purposes in this blog, let’s use Eclipse IDE. You can use any other of your choice, and do the project setup using the same steps.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>WebDriverWait</groupId>
<artifactId>WebDriverWait</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.23.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
With this, our basic project setup is completed. Let’s move forward and have a look at the TestBase.java file.
To begin, create a RemoteWebDriver object and initialize it to null. You will need to include your TestMu AI Username and Access Key, which can be found in your Account Settings under the Password & Security tab. These credentials are used to connect to the TestMu AI cloud grid for test execution.
For added security, you can configure these values as environment variables instead of hardcoding them in the test script. Additionally, define a String variable called status and initialize it with the value failed. This status is updated to passed if the test case runs without any assertion failures or exceptions, ensuring the test’s final status is accurately reflected on the TestMu AI Web Automation dashboard.
public class TestBase {
public RemoteWebDriver driver = null;
String username = System.getenv("LT_USERNAME") == null ? "<lambdatest_username>" : System.getenv("LT_USERNAME");
String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "<lambdatest_accesskey>" : System.getenv("LT_ACCESS_KEY");
String status = "failed";
@BeforeMethod
public void setUp()
{
try
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPlatformName("Windows 10");
chromeOptions.setBrowserVersion("127.0");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("build", "WebDriverWaits in Selenium Java");
ltOptions.put("name", "WebDriverWaits in Selenium Java");
chromeOptions.setCapability("LT:Options", ltOptions);
driver = new RemoteWebDriver(
new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

Next, create a setup() method, annotated with @BeforeMethod annotation from TestNG, to handle setting up browser properties and TestMu AI configurations before each test case. Use the TestMu AI Automation Capabilities Generator to fetch the required browser capabilities.
Then, connect to TestMu AI using RemoteWebDriver and a ChromeOptions object containing all the specified browser settings. After each test execution, a tearDown() method, annotated with @AfterMethod, will update the test status on TestMu AI and close the WebDriver. The main test class, TestExplicitWait, extends TestBase to inherit properties and methods required for the tests.
The TestExplicitWait class contains 4 different test methods to demonstrate different ExpectedConditions. Let’s have a look at these one by one.
Test Scenario 1:
Test Implementation:
This test script uses explicit waits with WebDriverWait in Selenium Java. It interacts with a TestMu AI eCommerce website and performs a different set of actions:
@Test(description="WebDriverWait demonstration 1")
public void testExplicitWait_1()
{
// Initialize WebDriverWait with a timeout of 10 seconds
WebDriverWait wait =new WebDriverWait(driver, Duration.ofSeconds(10));
// LambdaTest e-commerce website to check for dynamic loading elements
driver.get("https://ecommerce-playground.lambdatest.io/");
try {
// ExpectedCondition to wait for element to be present : presenceOfElementLocated
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("entry_217966")));
// ExpectedCondition to check if element is clickable and then click on it: elementToBeClickable
element = wait.until(ExpectedConditions.elementToBeClickable(By.id("entry_217966")));
element.click();
// ExpectedCondition to wait for element to be visible after the click: visibilityOfElementLocated
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@title='iPod Touch']")));
// ExpectedCondition to wait for all input type elements : visibilityOfAllElementsLocatedBy
// assert if the expected length of elements located is 15
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@class='carousel-item active']")));
Assert.assertEquals(elements.size(), 15);
status = "passed";
} catch (TimeoutException ex)
{
ex.printStackTrace();
}
}
Code Walkthrough:
Start by initializing a WebDriverWait object, which will be used in the test case flow to implement ExpectedConditions as required. This will be a common step in all cases.
The maximum wait time for this is 10 seconds, which means the driver waits for a maximum of 10 seconds for the condition to be fulfilled before throwing an exception.
Navigate to the TestMu AI eCommerce Playground website. Start the try block for better exception handling and implement the ExpectedCondition to wait for the dynamic loading image WebElement to be present using the presenceOfElementLocated() method. Then, implement the ExpectedCondition to wait for the same element to be clickable after its presence is confirmed using the elementToBeClickable() method and then click on it.

After clicking the same, wait for an element to be visible on the newly loaded page after redirection. For this, we have used the visibilityOfElementLocated() method to wait for the image to be visible before proceeding. Next, we use the visibilityOfAllElementsLocatedBy() method to check if all the product images are loaded on the page. For this, we assert the size to verify it.

Finally, update the status as passed for better visibility on the TestMu AI Web Automation dashboard and add a catch block for graceful exception handling in case there is any exception.
Test Scenario 2:
Test Implementation:
This test script uses explicit waits using WebDriverWait in Selenium Java for handling dynamic content on a Simple iframe page.
@Test(description="WebDriverWait demonstration 2")
public void testExplicitWait_2()
{
// Initialize WebDriverWait with a timeout of 10 seconds
WebDriverWait wait =new WebDriverWait(driver, Duration.ofSeconds(10));
// Navigate to iframe demo page
driver.get("https://www.lambdatest.com/selenium-playground/iframe-demo/");
try {
// ExpectedCondition to wait for title to contain given text: titleContains
wait.until(ExpectedConditions.titleContains("Selenium Playground"));
System.out.println("Page title is : " + driver.getTitle());
// ExpectedCondition to wait for element using the text : textToBe
wait.until(ExpectedConditions.textToBe(By.xpath("//*[contains(text(),'containing webpage')]"), "Simple iFrame containing webpage"));
// ExpectedCondition to wait for iFrame and switch to it : frameToBeAvailableAndSwitchToIt
driver = (RemoteWebDriver) wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iFrame2")));
// Verify switch to iframe is successful using expected condition by comparing text
// ExpectedCondition to wait/verify for presence of element by text : textToBePresentInElementLocated
Boolean iFrame = wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[contains(@href,'api-doc')]"), "API"));
System.out.println("Switched to iframe : " + iFrame);
status = "passed";
} catch (TimeoutException ex)
{
ex.printStackTrace();
}
}
Code Walkthrough:
Create and initialize the WebDriverWait object with a max wait time of 10 seconds and then navigate to the Simple iframe demo website. Now, wait for the title of the page to contain Selenium Playground words using the titleContains() method before logging the title.

Next, we check if the required iFrame is present or not using the textToBe() method for its header. Switch to the iFrame once it is available using the frameToBeAvailableAndSwitchToIt() method and store its reference in the RemoteWebDriver object by casting the WebDriver object to it. After that, check for an element inside the iFrame using the textToBePresentInElementLocated() method to verify that the switch to iFrame was successful.

Test Scenario 3:
Test Implementation:
This test script uses explicit waits using WebDriverWait in Selenium Java for handling JavaScript alerts on a Javascript Alert Box Demo page.
@Test(description="WebDriverWait demonstration 3")
public void testExplicitWait_3()
{
// Initialize WebDriverWait with a timeout of 10 seconds
WebDriverWait wait =new WebDriverWait(driver, Duration.ofSeconds(10));
// Navigate to iframe demo page
driver.get("https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo");
try {
// Click on the button to get alert
driver.findElement(By.xpath("(//*[@type='button'])[1]")).click();
// ExpectedCondition to wait for alert : alertIsPresent
wait.until(ExpectedConditions.alertIsPresent());
System.out.println("Alert text is : " + driver.switchTo().alert().getText());
status = "passed";
} catch (TimeoutException ex)
{
ex.printStackTrace();
}
}
Code Walkthrough:
Create and initialize the WebDriverWait object with a max wait time of 10 seconds and then navigate to the Javascript Alert Box Demo page. Now, click on the button to get an alert.
Next, use the alertIsPresent() method to wait for the alert. Once the condition is fulfilled, switch to the alert to get the alert text.

Then, update the test case status and add a catch block to handle the exception in case ExpectedCondition fails to find the alert in the given 10 seconds.
Test Scenario 4:
Test Implementation:
This test script uses explicit waits using WebDriverWait in Selenium Java for handling checkbox interactions on a Checkbox Demo page.
@Test(description="WebDriverWait demonstration 4")
public void testExplicitWait_4()
{
// Initialize WebDriverWait with a timeout of 10 seconds
WebDriverWait wait =new WebDriverWait(driver, Duration.ofSeconds(10));
// Navigate to iframe demo page
driver.get("https://www.lambdatest.com/selenium-playground/checkbox-demo");
try {
// check the checkbox to display message
driver.findElement(By.id("isAgeSelected")).click();
// ExpectedCondition to wait for checkbox to be selected: elementToBeSelected
wait.until(ExpectedConditions.elementToBeSelected(By.id("isAgeSelected")));
// uncheck the checkbox to hide the message
driver.findElement(By.id("isAgeSelected")).click();
// ExpectedCondition to wait for element to be invisible : invisibilityOf
// Verify that the message is invisible now.
Boolean isInvisible = wait.until(ExpectedConditions.invisibilityOf(driver.findElement(By.id("txtAge"))));
System.out.println("Message is invisible : " + isInvisible);
status = "passed";
} catch (TimeoutException ex)
{
ex.printStackTrace();
}
}
Code Walkthrough:
Create and initialize the WebDriverWait object with a max wait time of 10 seconds, and then navigate to the Checkbox Demo page. Now, check the checkbox and then use the elementToBeSelected() method to ensure that the checkbox is selected.

Then, uncheck the checkbox and use the invisibilityOf() method to wait and verify that the message displayed earlier is no longer visible after the checkbox is unchecked.
To view the execution results, navigate to the TestMu AI Web Automation dashboard.

A few best practices using which we can make the best use of WebDriverWait in our Selenium automation script are as follows:
Implicit waits should always be a reasonable time, not too short and not too long and explicit waits only in conditions where more precise and granular control is required over any WebElement before interacting.
Subscribe to the TestMu AI YouTube Channel and stay updated with more such tutorials.
With this, we came to an end to this blog on WebDriverWait in Selenium Java, where we learned about different wait types and methods that can be used as per the requirement to make test scripts robust and reliable. We also saw how using WebDriverWait in Selenium Java helps in synchronizing test execution for the dynamic behavior of web pages.
By understanding and applying these different waits with the help of ExpectedConditions, you can handle various waiting scenarios effectively in your automation scripts and make the test results more precise and efficient.
You can also use WebDriverWait in Selenium with various programming languages such as C#. To know more, refer to this blog on WebDriverWait in Selenium C#.
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance