Explicit Wait in Selenium
JustAcademy Selenium Training: JustAcademy Selenium Training Course
JustAcademy Demo Registration: Register for Selenium Course Demo
Explicit Wait in Selenium is a synchronization mechanism that allows WebDriver to wait for a specific condition to become true before continuing the test execution. Unlike Implicit Wait, which provides a general timeout for element searches, Explicit Wait is condition-specific and is especially useful for dynamic web applications where elements may appear, become visible, become enabled, become clickable, or change state after some time.
Explicit Wait is an important part of Selenium synchronization and dynamic-element handling. JustAcademy's Selenium curriculum includes Explicit Wait along with Implicit Wait, Fluent Wait, page load synchronization, dynamic element handling, and stable test execution strategies.
For structured Selenium learning covering WebDriver, WebElements, locators, waits, synchronization, dynamic elements, TestNG, Page Object Model, automation frameworks, and practical projects, visit Selenium Training Course.
For a practical course demonstration, visit Selenium Course Demo.
1. What is Explicit Wait?
Explicit Wait is a Selenium synchronization technique used to wait for a particular condition before continuing execution. It allows the tester to define both the maximum waiting time and the condition that Selenium should monitor.
For example, a test may need to wait until a login button becomes clickable, a message becomes visible, a loader disappears, an alert appears, or a particular text is displayed.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
2. Why is Explicit Wait Required?
Modern web applications are often dynamic. Elements may not be immediately ready when Selenium reaches the corresponding test step.
- JavaScript may dynamically create elements.
- AJAX or API requests may update the page asynchronously.
- Buttons may become enabled after validation.
- Loading indicators may remain visible while data is retrieved.
- Messages may appear after form submission.
- Menus may appear after user interaction.
- Search results may load after an API response.
- Animations may temporarily affect interaction.
- SPA frameworks may update the DOM without a complete page reload.
3. Basic Syntax of Explicit Wait
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
Here, driver is the WebDriver instance and Duration.ofSeconds(10) specifies the maximum time that the explicit wait can wait for the condition.
4. Understanding WebDriverWait
WebDriverWait is a Selenium Java wait utility used to wait until a specified condition is satisfied or the timeout is reached.
| Component | Meaning |
| WebDriverWait | Wait utility used for condition-based synchronization. |
| driver | The WebDriver instance controlling the browser. |
| Duration | Defines the maximum waiting period. |
| until() | Waits until the supplied condition is satisfied. |
5. Explicit Wait with until()
The until() method is used to wait for a condition.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("message")
)
);
Selenium repeatedly evaluates the condition until it succeeds or the configured timeout is reached.
6. Explicit Wait Execution Flow
Test Starts
↓
Create WebDriver
↓
Create WebDriverWait
↓
Define Condition
↓
Selenium Checks Condition
↓
Condition Satisfied?
↙ ↘
Yes No
↓ ↓
Continue Keep Polling
↓
Timeout Reached?
↙ ↘
No Yes
↓ ↓
Check Again TimeoutException
7. Explicit Wait is Condition-Based
The main advantage of Explicit Wait is that it waits for a meaningful condition rather than simply delaying execution for a fixed amount of time.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
The test can continue as soon as the required condition is satisfied instead of always waiting for the entire ten seconds.
8. Explicit Wait vs Thread.sleep()
| Feature | Explicit Wait | Thread.sleep() |
| Type | Condition-based synchronization | Fixed delay |
| Condition | Yes | No |
| Efficiency | Can continue when condition is satisfied | Always waits the specified duration |
| Dynamic Applications | Suitable | Less suitable |
| Maintainability | Higher | Lower when used repeatedly |
9. Explicit Wait vs Implicit Wait
| Feature | Implicit Wait | Explicit Wait |
| Scope | General element searches | Specific condition |
| Condition-Specific | No | Yes |
| Visibility | No direct condition | Supported |
| Clickability | No direct condition | Supported |
| Text | No direct condition | Supported |
| Alert | No direct condition | Supported |
| Frame | No direct condition | Supported |
| Custom Condition | Not intended for this | Supported |
10. Explicit Wait and Dynamic Elements
Dynamic elements may appear only after JavaScript execution, API responses, AJAX calls, or other asynchronous operations.
Page Loaded
↓
JavaScript Executes
↓
API Request
↓
Response Received
↓
DOM Updated
↓
Element Becomes Visible
↓
Explicit Wait Detects Condition
↓
Test Continues
11. Explicit Wait for Element Presence
presenceOfElementLocated() waits until an element is present in the DOM.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.presenceOfElementLocated(
By.id("username")
)
);
Presence means the element has been located in the DOM. It does not necessarily mean that the element is visible or ready for interaction.
12. Explicit Wait for Element Visibility
visibilityOfElementLocated() waits until the element is present and visible.
WebElement message = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("message")
)
);
This is useful when the test needs to interact with or validate a visible element.
13. Explicit Wait for Existing WebElement Visibility
If a WebElement has already been located, visibilityOf() can be used.
WebElement message =
driver.findElement(By.id("message"));
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.visibilityOf(message)
);
14. Explicit Wait for Clickability
elementToBeClickable() is commonly used when the test needs an element that is visible and enabled for interaction.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
button.click();
15. Explicit Wait for Element to be Selected
elementToBeSelected() can be used when the test needs to wait until an element becomes selected.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeSelected(
By.id("terms")
)
);
16. Explicit Wait for Element to Become Selected or Unselected
elementSelectionStateToBe() can be used when the expected selected state needs to be explicitly defined.
wait.until(
ExpectedConditions.elementSelectionStateToBe(
By.id("terms"),
true
)
);
17. Explicit Wait for Text
textToBePresentInElementLocated() waits until expected text is present inside a located element.
wait.until(
ExpectedConditions.textToBePresentInElementLocated(
By.id("status"),
"Completed"
)
);
18. Explicit Wait for Text in an Existing WebElement
When a WebElement is already available, Selenium provides conditions for checking expected text in that element.
WebElement status =
driver.findElement(By.id("status"));
wait.until(
ExpectedConditions.textToBePresentInElement(
status,
"Completed"
)
);
19. Explicit Wait for Attribute Value
Dynamic applications may update attributes such as class, value, aria attributes, or custom attributes.
wait.until(
ExpectedConditions.attributeToBe(
By.id("status"),
"class",
"completed"
)
);
20. Explicit Wait for Attribute to Contain a Value
When only part of an attribute value needs to be matched, an attribute-based condition can be useful.
wait.until(
ExpectedConditions.attributeContains(
By.id("status"),
"class",
"active"
)
);
21. Explicit Wait for Invisibility
invisibilityOfElementLocated() is useful when a loader, spinner, overlay, or temporary element must disappear before the next operation.
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("loader")
)
);
22. Explicit Wait for Loading Spinner
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(15)
);
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.cssSelector(".loading-spinner")
)
);
This is often more meaningful than using a fixed sleep because the test can continue as soon as the loading indicator disappears.
23. Explicit Wait for Alert
When an alert is expected to appear asynchronously, alertIsPresent() can be used.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
Alert alert = wait.until(
ExpectedConditions.alertIsPresent()
);
alert.accept();
24. Explicit Wait for Frame
When an iframe may not immediately be available, an explicit wait can wait until the frame is available and switch to it.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.id("paymentFrame")
)
);
25. Explicit Wait for Multiple Windows
When a user action opens a new tab or window, a custom wait can be used to wait until the expected number of window handles exists.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
d -> d.getWindowHandles().size() > 1
);
26. Explicit Wait with Lambda Expression
Explicit Wait can also use a lambda expression when the required condition is not directly represented by a built-in ExpectedConditions method.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
d -> d.findElement(By.id("status"))
.getText()
.equals("Completed")
);
27. Explicit Wait for Custom Condition
Custom conditions are useful when an application has a specific state that cannot be handled directly by a standard expected condition.
wait.until(
driver -> {
String status =
driver.findElement(By.id("status"))
.getText();
return status.equals("Success");
}
);
28. Explicit Wait for Button Enablement
A button may initially be disabled and become enabled after the user completes required fields.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement submit = wait.until(
driver -> {
WebElement button =
driver.findElement(By.id("submit"));
return button.isEnabled() ? button : null;
}
);
submit.click();
29. Explicit Wait for Login Button
driver.findElement(By.id("username"))
.sendKeys("admin");
driver.findElement(By.id("password"))
.sendKeys("password");
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement loginButton = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);
loginButton.click();
30. Explicit Wait for Search Results
driver.findElement(By.id("search"))
.sendKeys("Selenium");
driver.findElement(By.id("searchButton"))
.click();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement result = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".search-result")
)
);
System.out.println(result.getText());
31. Explicit Wait for AJAX Content
AJAX can update only part of a web page. The browser may remain on the same page while the required content is being loaded.
User Action
↓
AJAX Request
↓
Server Processing
↓
Response
↓
DOM Update
↓
Explicit Wait
↓
Condition Satisfied
↓
Test Continues
32. Explicit Wait for Dynamic Table Rows
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
List<WebElement> rows = wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.cssSelector("table tbody tr")
)
);
for(WebElement row : rows){
System.out.println(row.getText());
}
33. Explicit Wait for Product Cards
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
List<WebElement> products = wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.cssSelector(".product-card")
)
);
for(WebElement product : products){
System.out.println(product.getText());
}
34. Explicit Wait with XPath
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement login = wait.until(
ExpectedConditions.elementToBeClickable(
By.xpath("//button[@id='login']")
)
);
login.click();
35. Explicit Wait with CSS Selector
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("#username")
)
);
username.sendKeys("admin");
36. Explicit Wait with ID Locator
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.sendKeys("admin");
37. Explicit Wait with Name Locator
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.name("username")
)
);
username.sendKeys("admin");
38. Explicit Wait with Class Name
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(
By.className("login-button")
)
);
button.click();
39. Explicit Wait and Presence vs Visibility
| Condition | Meaning |
| presenceOfElementLocated() | Element exists in the DOM. |
| visibilityOfElementLocated() | Element is present and visible. |
| elementToBeClickable() | Element is visible and enabled for interaction. |
40. Explicit Wait and Presence
Presence is appropriate when the test only needs the element to exist in the DOM.
WebElement element = wait.until(
ExpectedConditions.presenceOfElementLocated(
By.id("data")
)
);
Presence does not guarantee that the element is visible.
41. Explicit Wait and Visibility
Visibility is appropriate when the element needs to be displayed before the test continues.
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("data")
)
);
42. Explicit Wait and Clickability
Clickability is useful when a test needs an element that is visible and enabled before attempting a click.
WebElement element = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
element.click();
43. Explicit Wait and isDisplayed()
Explicit Wait can be combined with visibility conditions when the test requires a dynamically displayed element.
WebElement message = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("message")
)
);
System.out.println(
message.isDisplayed()
);
44. Explicit Wait and isEnabled()
When a control becomes enabled dynamically, a custom condition or clickability condition can be used.
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
System.out.println(
button.isEnabled()
);
45. Explicit Wait and isSelected()
When a checkbox or radio button becomes selected after a user action or application update, an explicit selection condition can be used.
wait.until(
ExpectedConditions.elementToBeSelected(
By.id("terms")
)
);
46. Explicit Wait for Checkbox
WebElement checkbox = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("terms")
)
);
if(!checkbox.isSelected()){
checkbox.click();
}
wait.until(
ExpectedConditions.elementToBeSelected(
By.id("terms")
)
);
47. Explicit Wait for Radio Button
WebElement male = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("male")
)
);
male.click();
wait.until(
ExpectedConditions.elementToBeSelected(
By.id("male")
)
);
48. Explicit Wait for Toast Message
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement toast = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".toast")
)
);
System.out.println(toast.getText());
49. Explicit Wait for Error Message
driver.findElement(By.id("login"))
.click();
WebElement error = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("errorMessage")
)
);
System.out.println(error.getText());
50. Explicit Wait for Success Message
WebElement success = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".success-message")
)
);
String message = success.getText();
System.out.println(message);
51. Explicit Wait for Modal
WebElement modal = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("confirmationModal")
)
);
System.out.println(
modal.getText()
);
52. Explicit Wait for Modal to Disappear
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("confirmationModal")
)
);
53. Explicit Wait for Dropdown
WebElement dropdown = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("country")
)
);
dropdown.click();
54. Explicit Wait for Autocomplete Suggestions
driver.findElement(By.id("search"))
.sendKeys("Selenium");
List<WebElement> suggestions = wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.cssSelector(".suggestion")
)
);
for(WebElement suggestion : suggestions){
System.out.println(suggestion.getText());
}
55. Explicit Wait for E-Commerce Product
driver.findElement(By.id("search"))
.sendKeys("Laptop");
driver.findElement(By.id("searchButton"))
.click();
WebElement product = wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(".product-card")
)
);
product.click();
56. Explicit Wait for Add to Cart Button
WebElement addToCart = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("addToCart")
)
);
addToCart.click();
57. Explicit Wait for Checkout Button
WebElement checkout = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("checkout")
)
);
checkout.click();
58. Explicit Wait for Dynamic Login Form
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
WebElement password = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
);
username.sendKeys("admin");
password.sendKeys("password");
WebElement login = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);
login.click();
59. Explicit Wait with Page Navigation
driver.get("https://example.com/login");
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.sendKeys("admin");
60. Explicit Wait for New Page Content
driver.findElement(By.id("login"))
.click();
WebElement dashboard = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dashboard")
)
);
System.out.println(
dashboard.getText()
);
61. Explicit Wait and StaleElementReferenceException
Dynamic applications may replace elements in the DOM. A previously stored WebElement can become stale after a DOM refresh or re-render.
WebElement element =
driver.findElement(By.id("dynamic"));
driver.navigate().refresh();
element.click();
When the DOM changes, locating the element again inside the wait condition can be safer than relying on an old reference.
62. Re-Locating an Element During an Explicit Wait
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dynamic")
)
);
element.click();
Using a locator-based condition allows Selenium to perform the lookup as part of the wait operation.
63. Explicit Wait and TimeoutException
If the required condition does not become true within the configured timeout, Selenium can throw TimeoutException.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("element")
)
);
64. Handling TimeoutException
try{
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("element")
)
);
System.out.println(element.getText());
}
catch(TimeoutException e){
System.out.println(
"Element did not become visible in time."
);
}
65. Explicit Wait and NoSuchElementException
When a locator-based expected condition is used, the wait repeatedly performs the required lookup. If the condition cannot be satisfied before timeout, the wait ultimately fails.
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dynamicElement")
)
);
66. Explicit Wait and AJAX Search
driver.findElement(By.id("search"))
.sendKeys("Selenium");
driver.findElement(By.id("searchButton"))
.click();
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".search-results")
)
);
67. Explicit Wait for Loader and Content
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("loader")
)
);
WebElement content = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("content")
)
);
System.out.println(content.getText());
68. Explicit Wait with FluentWait
WebDriverWait is built on Selenium's fluent waiting mechanism. For advanced synchronization, a FluentWait can provide control over timeout, polling interval, and ignored exceptions.
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(
d -> d.findElement(By.id("dynamic"))
);
69. Explicit Wait Polling
Explicit Wait does not normally execute one single check and immediately fail. It repeatedly evaluates the condition until it succeeds or the timeout is reached.
Start
↓
Check Condition
↓
Condition True?
↙ ↘
Yes No
↓ ↓
Continue Poll Again
↓
Timeout Reached?
70. Explicit Wait and Polling Interval
For more advanced waiting requirements, polling frequency can be customized through FluentWait.
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(2));
71. Explicit Wait with Ignored Exceptions
FluentWait can be configured to ignore selected exceptions while polling for a condition.
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
72. Explicit Wait and Page Object Model
In a Page Object Model framework, explicit waits can be encapsulated inside page classes or reusable wait utilities.
public class LoginPage {
private WebDriver driver;
private WebDriverWait wait;
private By username =
By.id("username");
private By password =
By.id("password");
private By loginButton =
By.id("login");
public LoginPage(WebDriver driver){
this.driver = driver;
this.wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
}
public void login(
String user,
String pass
){
wait.until(
ExpectedConditions.visibilityOfElementLocated(
username
)
).sendKeys(user);
wait.until(
ExpectedConditions.visibilityOfElementLocated(
password
)
).sendKeys(pass);
wait.until(
ExpectedConditions.elementToBeClickable(
loginButton
)
).click();
}
}
73. Explicit Wait Utility Class
A reusable wait utility can reduce duplicate synchronization code in larger frameworks.
public class WaitUtils {
private WebDriver driver;
private WebDriverWait wait;
public WaitUtils(WebDriver driver){
this.driver = driver;
this.wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
}
public WebElement waitForVisible(By locator){
return wait.until(
ExpectedConditions.visibilityOfElementLocated(
locator
)
);
}
public WebElement waitForClickable(By locator){
return wait.until(
ExpectedConditions.elementToBeClickable(
locator
)
);
}
}
74. Explicit Wait in TestNG
@BeforeMethod
public void setup(){
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void loginTest(){
driver.get("https://example.com/login");
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
).sendKeys("admin");
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
).sendKeys("password");
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
).click();
}
75. Explicit Wait with Assertions
WebElement dashboard = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dashboard")
)
);
Assert.assertTrue(
dashboard.isDisplayed()
);
76. Explicit Wait in Data-Driven Testing
Explicit Wait can be reused while executing the same workflow with multiple test data sets.
for(String username : usernames){
WebElement input = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
input.clear();
input.sendKeys(username);
}
77. Explicit Wait in Cross-Browser Testing
Explicit Wait works with Selenium WebDriver regardless of whether the test is executed against Chrome, Firefox, Edge, or another supported browser.
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
78. Explicit Wait in CI/CD
CI/CD environments can have different execution characteristics from local systems. Condition-based waits can help tests synchronize with actual application state instead of relying on fixed delays.
Local Machine
↓
Application Response
↓
Condition Check
↓
Test Continues
CI Server
↓
Application Response
↓
Condition Check
↓
Test Continues
79. Choosing Explicit Wait Duration
The timeout should be realistic for the application and execution environment.
| Timeout | Possible Effect |
| Very Short | May cause failures when the application is temporarily slow. |
| Reasonable | Allows normal application response variation. |
| Very Long | Can delay failure detection and increase test execution time. |
80. Explicit Wait Does Not Fix Incorrect Locators
Waiting cannot correct a locator that does not identify the required element.
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("incorrect-id")
)
);
If the actual element uses a different ID, increasing the timeout will not make the locator correct.
81. Explicit Wait Does Not Fix Application Bugs
If an application has a genuine defect and the expected element or state never occurs, an explicit wait will eventually fail with a timeout. A wait should synchronize with expected application behavior, not hide application defects.
82. Explicit Wait Does Not Guarantee Successful Click
elementToBeClickable() checks whether an element is visible and enabled, but a click can still fail because of overlays, DOM changes, interception, browser behavior, animations, or other application conditions.
WebElement button = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
button.click();
83. Explicit Wait and Overlays
A transparent or visible overlay can block interaction with an element. In such cases, waiting for the target element alone may not be enough.
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("overlay")
)
);
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
).click();
84. Explicit Wait for Animation Completion
Animations can temporarily affect interaction. A framework may wait for a relevant state or for the animation-related element to disappear or reach the expected state.
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.cssSelector(".animation-loader")
)
);
85. Explicit Wait for Frame Switching
wait.until(
ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.cssSelector("iframe.payment")
)
);
driver.findElement(By.id("cardNumber"))
.sendKeys("123456789");
86. Explicit Wait for Alert and Handling
driver.findElement(By.id("delete"))
.click();
Alert alert = wait.until(
ExpectedConditions.alertIsPresent()
);
System.out.println(alert.getText());
alert.accept();
87. Explicit Wait for Window or Tab
String originalWindow =
driver.getWindowHandle();
driver.findElement(By.id("open"))
.click();
wait.until(
d -> d.getWindowHandles().size() > 1
);
for(String window : driver.getWindowHandles()){
if(!window.equals(originalWindow)){
driver.switchTo().window(window);
break;
}
}
88. Explicit Wait for Dynamic URL
A custom condition can be used when navigation is expected to result in a particular URL.
wait.until(
ExpectedConditions.urlContains("dashboard")
);
89. Explicit Wait for Title
Selenium provides conditions for waiting until the page title contains or matches expected text.
wait.until(
ExpectedConditions.titleContains("Dashboard")
);
90. Explicit Wait for URL
wait.until(
ExpectedConditions.urlToBe(
"https://example.com/dashboard"
)
);
91. Explicit Wait for DOM State
When a specific DOM state represents application readiness, a custom lambda can be used.
wait.until(
driver -> driver.findElement(
By.id("status")
).getAttribute("data-state")
.equals("ready")
);
92. Explicit Wait for Application Status
wait.until(
driver -> {
WebElement status =
driver.findElement(By.id("status"));
return status.getText().equals("Ready");
}
);
93. Explicit Wait for Dynamic Form Validation
driver.findElement(By.id("email"))
.sendKeys("[email protected]");
wait.until(
ExpectedConditions.attributeContains(
By.id("email"),
"class",
"valid"
)
);
94. Explicit Wait for Search Suggestion
driver.findElement(By.id("search"))
.sendKeys("Selenium");
List<WebElement> suggestions = wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.cssSelector(".suggestion-item")
)
);
suggestions.get(0).click();
95. Explicit Wait for Notification
WebElement notification = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".notification")
)
);
Assert.assertTrue(
notification.getText()
.contains("Success")
);
96. Explicit Wait for File Upload Result
When a file upload causes a status element to appear, Selenium can wait for that status instead of relying on a fixed delay.
driver.findElement(By.id("file"))
.sendKeys("/path/to/file.pdf");
WebElement status = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("uploadStatus")
)
);
System.out.println(status.getText());
97. Explicit Wait for Download Completion
For downloads, the synchronization requirement is often outside the DOM. The test may need to verify the filesystem or application download status rather than simply waiting for a web element.
driver.findElement(By.id("download"))
.click();
wait.until(
driver -> {
return new File(
"/downloads/report.pdf"
).exists();
}
);
98. Explicit Wait and Stable Locators
A good synchronization strategy should be combined with stable locators. Explicit Wait cannot compensate for locators that frequently change or identify the wrong element.
- Prefer stable IDs when available.
- Use meaningful CSS selectors.
- Use reliable XPath expressions when necessary.
- Avoid unnecessary absolute XPath.
- Avoid locating elements using unstable dynamic values when better options exist.
99. Explicit Wait Best Practices
- Use Explicit Wait for specific application conditions.
- Choose realistic timeout values.
- Use condition-based synchronization instead of arbitrary delays.
- Use stable locators.
- Wait for the actual state required by the next test action.
- Use visibility conditions when visibility is required.
- Use clickability conditions when clicking is required.
- Use invisibility conditions for loaders and overlays.
- Use custom conditions for application-specific states.
- Keep reusable waits in framework utilities where appropriate.
- Avoid unnecessary Thread.sleep() calls.
- Handle TimeoutException meaningfully.
- Do not use excessive timeout values.
- Keep synchronization logic maintainable.
100. Common Mistakes with Explicit Wait
- Using Thread.sleep() instead of condition-based waiting.
- Waiting for presence when visibility is actually required.
- Waiting for visibility when clickability is required.
- Using incorrect locators.
- Using extremely large timeout values.
- Ignoring TimeoutException.
- Waiting for the wrong application condition.
- Using stale WebElement references.
- Adding waits everywhere without understanding the actual synchronization requirement.
- Using multiple wait mechanisms without a clear framework strategy.
101. Explicit Wait and Synchronization Strategy
Application Action
↓
Application Processing
↓
Identify Required State
↓
Choose Correct Expected Condition
↓
Create Explicit Wait
↓
Wait Until Condition
↓
Condition Satisfied
↓
Perform Next Action
↓
Validate Result
102. Explicit Wait Decision Table
| Requirement | Recommended Condition |
| Element exists in DOM | presenceOfElementLocated() |
| Element is visible | visibilityOfElementLocated() |
| Element can be clicked | elementToBeClickable() |
| Element is selected | elementToBeSelected() |
| Text appears | textToBePresentInElementLocated() |
| Loader disappears | invisibilityOfElementLocated() |
| Alert appears | alertIsPresent() |
| Frame becomes available | frameToBeAvailableAndSwitchToIt() |
| URL changes | urlContains() or urlToBe() |
| Title changes | titleContains() or titleIs() |
| Custom application state | Lambda or custom condition |
103. Practical Login Automation Example
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;
public class ExplicitWaitLoginTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com/login");
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.sendKeys("admin");
WebElement password = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
);
password.sendKeys("password");
WebElement loginButton = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);
loginButton.click();
WebElement dashboard = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dashboard")
)
);
System.out.println(
"Dashboard: " + dashboard.getText()
);
driver.quit();
}
}
104. Practical E-Commerce Example
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(15)
);
driver.get("https://example.com");
WebElement search = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("search")
)
);
search.sendKeys("Laptop");
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("searchButton")
)
).click();
WebElement product = wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(".product-card")
)
);
product.click();
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("addToCart")
)
).click();
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("checkout")
)
).click();
driver.quit();
105. Practical Registration Example
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("name")
)
).sendKeys("John");
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("email")
)
).sendKeys("[email protected]");
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
).sendKeys("Password123");
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("register")
)
).click();
WebElement success = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("success")
)
);
System.out.println(success.getText());
106. Practical Dynamic Dashboard Example
driver.get("https://example.com/dashboard");
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(15)
);
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("loader")
)
);
WebElement dashboard = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dashboard")
)
);
WebElement reports = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("reports")
)
);
reports.click();
107. Explicit Wait Interview Question: What is Explicit Wait?
Answer: Explicit Wait is a Selenium synchronization mechanism that waits for a specific condition to become true before allowing test execution to continue or until the configured timeout is reached.
108. Interview Question: How do you create an Explicit Wait?
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
109. Interview Question: What is the purpose of until()?
Answer: The until() method waits until the supplied condition is satisfied or the configured timeout is reached.
110. Interview Question: What is ExpectedConditions?
Answer: ExpectedConditions is a Selenium utility that provides commonly used conditions for Explicit Wait, such as visibility, presence, clickability, text, alerts, frames, selection, URLs, titles, and other states.
111. Interview Question: What is the difference between presence and visibility?
Answer: Presence means the element exists in the DOM. Visibility means the element is present and displayed according to WebDriver's visibility rules.
112. Interview Question: What is elementToBeClickable()?
Answer: elementToBeClickable() is an ExpectedCondition used to wait until an element is visible and enabled for interaction.
113. Interview Question: What happens when Explicit Wait times out?
Answer: If the required condition does not become true within the configured timeout, Selenium generally throws a TimeoutException.
114. Interview Question: Can Explicit Wait handle dynamic elements?
Answer: Yes. Explicit Wait is particularly useful for dynamic elements because it can wait for specific states such as presence, visibility, clickability, text, selection, or custom application conditions.
115. Interview Question: Can Explicit Wait replace Thread.sleep()?
Answer: For normal synchronization requirements, condition-based Explicit Wait is generally more appropriate than fixed Thread.sleep() delays because it waits for the required state rather than an arbitrary amount of time.
116. Interview Question: Can Explicit Wait wait for an alert?
Answer: Yes. Selenium provides alertIsPresent() for waiting until a JavaScript alert is available.
Alert alert = wait.until(
ExpectedConditions.alertIsPresent()
);
117. Interview Question: Can Explicit Wait wait for an iframe?
Answer: Yes. Selenium provides frameToBeAvailableAndSwitchToIt() for waiting until a frame is available and switching into it.
118. Interview Question: Can Explicit Wait wait for text?
Answer: Yes. Conditions such as textToBePresentInElementLocated() can be used when test execution depends on dynamic text.
119. Interview Question: Can Explicit Wait wait for a loader to disappear?
Answer: Yes. invisibilityOfElementLocated() can be used to wait until a loading indicator or other temporary element becomes invisible.
120. Interview Question: What is the difference between Explicit Wait and Fluent Wait?
Answer: WebDriverWait provides a convenient condition-based waiting mechanism, while FluentWait provides more configurable control over timeout, polling interval, and ignored exceptions. WebDriverWait is built on Selenium's fluent waiting mechanism.
121. Interview Question: Should Implicit and Explicit Wait be mixed?
Answer: They can be configured together, but their interaction should be understood because mixing them without a clear strategy can make timeout behavior harder to reason about. A consistent synchronization strategy is generally easier to maintain.
122. Interview Question: Does Explicit Wait fix incorrect locators?
Answer: No. Explicit Wait only waits for the condition associated with the locator. If the locator is incorrect, the expected condition will not become true.
123. Interview Question: Does Explicit Wait guarantee click success?
Answer: No. elementToBeClickable() waits for visibility and enabled state, but actual interaction can still be affected by overlays, DOM changes, interception, animations, or other browser/application conditions.
124. Explicit Wait Quick Reference
| Concept | Details |
| Primary Class | WebDriverWait |
| Main Method | until() |
| Purpose | Condition-specific synchronization. |
| Common Condition | visibilityOfElementLocated() |
| Clickable Condition | elementToBeClickable() |
| Presence Condition | presenceOfElementLocated() |
| Text Condition | textToBePresentInElementLocated() |
| Alert Condition | alertIsPresent() |
| Frame Condition | frameToBeAvailableAndSwitchToIt() |
| Selection Condition | elementToBeSelected() |
| Invisible Condition | invisibilityOfElementLocated() |
| Failure | TimeoutException when condition is not satisfied within timeout. |
125. Explicit Wait Revision Notes
- Explicit Wait: Condition-based Selenium synchronization.
- Class: WebDriverWait.
- Main Method: until().
- Purpose: Wait for a specific application state.
- Presence: Wait until an element exists in the DOM.
- Visibility: Wait until an element is visible.
- Clickability: Wait until an element is visible and enabled.
- Invisibility: Wait until an element disappears.
- Text: Wait until expected text appears.
- Alert: Wait until an alert is present.
- Frame: Wait until an iframe is available.
- Custom Condition: Use lambda or custom logic.
- Timeout: Use realistic values.
- Best Practice: Prefer condition-based synchronization over arbitrary fixed delays.
126. Final Summary
Explicit Wait is one of the most useful synchronization mechanisms in Selenium automation. It allows a test to wait for a specific condition before continuing execution. This makes it particularly useful for modern dynamic web applications where elements can appear, disappear, become visible, become enabled, change text, or update their state asynchronously.
The basic Selenium Java syntax is WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));. The until() method is then used with a suitable expected condition.
Common conditions include presenceOfElementLocated(), visibilityOfElementLocated(), elementToBeClickable(), textToBePresentInElementLocated(), invisibilityOfElementLocated(), alertIsPresent(), frameToBeAvailableAndSwitchToIt(), and elementToBeSelected().
Explicit Wait is different from Implicit Wait because it is condition-specific, while Implicit Wait provides a general timeout for applicable element searches. Explicit Wait is also different from Thread.sleep(), because it can stop waiting as soon as the required condition is satisfied rather than always pausing for a fixed duration.
A reliable Selenium framework should use meaningful conditions, stable locators, realistic timeout values, reusable wait utilities, and synchronization based on actual application state. Explicit Wait should be used to synchronize with expected application behavior rather than to hide incorrect locators or application defects.
For detailed Selenium automation training covering WebDriver, locators, waits, synchronization, dynamic elements, TestNG, Page Object Model, frameworks, and practical automation projects, visit Selenium Training Course.
To explore a practical Selenium course demonstration, visit Register for Selenium Course Demo.