<div class="selenium-notes">
<h1>Waits with Java in Selenium</h1>
<p><strong>Waits</strong> are one of the most important concepts in Selenium automation. Modern web applications often load elements asynchronously, update content dynamically, display animations, open popups, or make elements available only after a particular condition is satisfied. Selenium waits help synchronize the automation script with the actual state of the web application.</p>
<p>Without proper synchronization, Selenium may try to interact with an element before it is available, visible, enabled, or clickable. This can result in exceptions such as <strong>NoSuchElementException</strong>, <strong>ElementNotInteractableException</strong>, <strong>ElementClickInterceptedException</strong>, and <strong>TimeoutException</strong>.</p>
<p>In Selenium Java, the main waiting approaches are <strong>Implicit Wait</strong>, <strong>Explicit Wait</strong>, and <strong>Fluent Wait</strong>. Selenium's Java API provides <strong>WebDriverWait</strong> as a specialization of <strong>FluentWait</strong>, while <strong>ExpectedConditions</strong> provides commonly used conditions for explicit waits. :contentReference[oaicite:0]{index=0}</p>
<p><strong>Course Resource:</strong> <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training</a> | <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a></p>
<hr>
<h2>1. What is a Wait in Selenium?</h2>
<p>A <strong>Wait</strong> tells Selenium to pause or repeatedly check for a specific condition before continuing with the next operation.</p>
<p>For example, suppose a login button appears only after a page finishes loading. If Selenium immediately executes <strong>click()</strong>, the element may not yet be ready. Instead, Selenium can wait until the button becomes clickable and then perform the click.</p>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement loginButton =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);
loginButton.click();</code></pre>
<p>Explicit waits repeatedly evaluate a condition until it succeeds or the timeout is reached. Selenium's Java API represents these conditions through <strong>ExpectedCondition</strong> and provides ready-to-use conditions through <strong>ExpectedConditions</strong>. :contentReference[oaicite:1]{index=1}</p>
<h2>2. Why are Waits Important?</h2>
<p>Real-world web applications rarely behave like static HTML pages. Elements may appear after JavaScript execution, AJAX requests, API responses, animations, page transitions, or other asynchronous operations.</p>
<p>Waits help Selenium synchronize the test with these changing application states.</p>
<ul>
<li>Wait for elements to appear.</li>
<li>Wait for elements to become visible.</li>
<li>Wait for elements to become clickable.</li>
<li>Wait for elements to become enabled.</li>
<li>Wait for text to appear.</li>
<li>Wait for a page title.</li>
<li>Wait for a URL to change.</li>
<li>Wait for an alert.</li>
<li>Wait for an iframe to become available.</li>
<li>Wait for an element to disappear.</li>
<li>Wait for a DOM element to become stale.</li>
<li>Synchronize automation with AJAX and JavaScript operations.</li>
</ul>
<h2>3. Basic Wait Flow</h2>
<pre><code>Start Test
|
v
Open Browser
|
v
Open Web Application
|
v
Locate Element
|
v
Is Required Condition Satisfied?
|
+---- No ----> Keep Waiting
|
Yes
|
v
Perform Operation
|
v
Validate Result
|
v
Continue Test</code></pre>
<h2>4. Types of Waits in Selenium</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Wait Type</th><th>Description</th><th>Typical Usage</th></tr>
<tr><td>Implicit Wait</td><td>Applies a default wait when locating elements</td><td>General element lookup synchronization</td></tr>
<tr><td>Explicit Wait</td><td>Waits for a specific condition</td><td>Dynamic elements and specific states</td></tr>
<tr><td>Fluent Wait</td><td>Configurable wait with timeout, polling interval, and ignored exceptions</td><td>Advanced synchronization requirements</td></tr>
<tr><td>Thread.sleep()</td><td>Pauses execution for a fixed amount of time</td><td>Generally discouraged for normal synchronization</td></tr>
</table>
<h2>5. Implicit Wait</h2>
<p>An <strong>Implicit Wait</strong> tells WebDriver to wait for a specified amount of time when trying to find elements if they are not immediately available.</p>
<pre><code>import java.time.Duration;
driver.manage().timeouts().implicitlyWait(
Duration.ofSeconds(10)
);</code></pre>
<p>After this setting, element lookup operations can wait up to the configured duration before failing to find an element.</p>
<h3>Example</h3>
<pre><code>driver.manage().timeouts().implicitlyWait(
Duration.ofSeconds(10)
);
WebElement username =
driver.findElement(By.id("username"));
username.sendKeys("admin");</code></pre>
<h2>6. Characteristics of Implicit Wait</h2>
<ul>
<li>It is configured on the WebDriver.</li>
<li>It applies broadly to element lookup operations.</li>
<li>It does not describe a specific application condition.</li>
<li>It can make simple scripts more tolerant of delayed element availability.</li>
<li>It should be configured deliberately as part of the synchronization strategy.</li>
</ul>
<h2>7. Advantages of Implicit Wait</h2>
<ul>
<li>Simple to configure.</li>
<li>Applies globally to WebDriver element lookup.</li>
<li>Reduces failures caused by very small delays in locating elements.</li>
<li>Requires less repeated wait code for basic lookup scenarios.</li>
</ul>
<h2>8. Disadvantages of Implicit Wait</h2>
<ul>
<li>It is not designed for detailed condition-based synchronization.</li>
<li>It does not directly express conditions such as clickable, visible, title contains, or alert present.</li>
<li>Large timeout values can make failures slower to diagnose.</li>
<li>Combining implicit and explicit waits can make timing behavior harder to reason about.</li>
</ul>
<h2>9. Explicit Wait</h2>
<p>An <strong>Explicit Wait</strong> waits for a particular condition before continuing. It is especially useful for dynamic web applications.</p>
<p>Selenium's Java <strong>WebDriverWait</strong> is commonly used for explicit waits. It extends <strong>FluentWait<WebDriver></strong> and uses a timeout specified with <strong>Duration</strong>. :contentReference[oaicite:2]{index=2}</p>
<pre><code>import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement username =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.sendKeys("admin");</code></pre>
<h2>10. Basic Explicit Wait Syntax</h2>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.condition(...)
);</code></pre>
<p>The general process is:</p>
<ol>
<li>Create a WebDriverWait object.</li>
<li>Specify the maximum timeout.</li>
<li>Define the expected condition.</li>
<li>Call <strong>until()</strong>.</li>
<li>Continue when the condition is satisfied.</li>
</ol>
<h2>11. WebDriverWait</h2>
<p><strong>WebDriverWait</strong> is the Selenium Java class commonly used for explicit waits. It is a specialization of <strong>FluentWait</strong> for WebDriver instances. :contentReference[oaicite:3]{index=3}</p>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);</code></pre>
<p>The timeout determines how long Selenium can wait for the condition before a timeout failure occurs.</p>
<h2>12. ExpectedConditions</h2>
<p><strong>ExpectedConditions</strong> provides commonly used predefined conditions for WebDriver tests. Examples include waiting for an element to be present, visible, clickable, an alert to appear, a URL to match, a title to match, an element to become stale, and more. :contentReference[oaicite:4]{index=4}</p>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Condition</th><th>Purpose</th></tr>
<tr><td>presenceOfElementLocated()</td><td>Waits for an element to exist in the DOM</td></tr>
<tr><td>visibilityOfElementLocated()</td><td>Waits for an element to be present and visible</td></tr>
<tr><td>elementToBeClickable()</td><td>Waits for an element to be visible and enabled for clicking</td></tr>
<tr><td>textToBePresentInElement()</td><td>Waits for specific text in an element</td></tr>
<tr><td>titleIs()</td><td>Waits for an exact page title</td></tr>
<tr><td>titleContains()</td><td>Waits for part of a page title</td></tr>
<tr><td>urlToBe()</td><td>Waits for an exact URL</td></tr>
<tr><td>urlContains()</td><td>Waits for part of a URL</td></tr>
<tr><td>alertIsPresent()</td><td>Waits for an alert</td></tr>
<tr><td>invisibilityOfElementLocated()</td><td>Waits for an element to become invisible or absent</td></tr>
<tr><td>stalenessOf()</td><td>Waits for an element to become detached from the DOM</td></tr>
<tr><td>frameToBeAvailableAndSwitchToIt()</td><td>Waits for a frame and switches into it</td></tr>
<tr><td>numberOfElementsToBe()</td><td>Waits for a specific number of matching elements</td></tr>
</table>
<h2>13. presenceOfElementLocated()</h2>
<p>The <strong>presenceOfElementLocated()</strong> condition waits until an element is present in the DOM.</p>
<pre><code>WebElement username =
wait.until(
ExpectedConditions.presenceOfElementLocated(
By.id("username")
)
);</code></pre>
<p>Presence does not necessarily mean that the element is visible or ready for user interaction. It means Selenium can locate the element in the DOM.</p>
<h2>14. visibilityOfElementLocated()</h2>
<p>The <strong>visibilityOfElementLocated()</strong> condition waits until the element is present and visible.</p>
<pre><code>WebElement username =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.sendKeys("admin");</code></pre>
<p>This is useful when an element exists in the DOM but may initially be hidden.</p>
<h2>15. elementToBeClickable()</h2>
<p>The <strong>elementToBeClickable()</strong> condition waits until an element is visible and enabled so that it can be clicked. :contentReference[oaicite:5]{index=5}</p>
<pre><code>WebElement loginButton =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);
loginButton.click();</code></pre>
<h2>16. Waiting for Text</h2>
<p>The <strong>textToBePresentInElement()</strong> condition can be used when a page displays text asynchronously.</p>
<pre><code>wait.until(
ExpectedConditions.textToBePresentInElement(
driver.findElement(By.id("message")),
"Login successful"
)
);</code></pre>
<h3>Alternative Locator-Based Example</h3>
<pre><code>wait.until(
ExpectedConditions.textToBePresentInElementLocated(
By.id("message"),
"Login successful"
)
);</code></pre>
<h2>17. Waiting for Page Title</h2>
<h3>titleIs()</h3>
<pre><code>wait.until(
ExpectedConditions.titleIs("Dashboard")
);</code></pre>
<h3>titleContains()</h3>
<pre><code>wait.until(
ExpectedConditions.titleContains("Dashboard")
);</code></pre>
<p>These conditions are useful when navigation is asynchronous or when the title changes after a page transition. :contentReference[oaicite:6]{index=6}</p>
<h2>18. Waiting for URL</h2>
<h3>urlToBe()</h3>
<pre><code>wait.until(
ExpectedConditions.urlToBe(
"https://example.com/dashboard"
)
);</code></pre>
<h3>urlContains()</h3>
<pre><code>wait.until(
ExpectedConditions.urlContains("dashboard")
);</code></pre>
<h2>19. Waiting for an Alert</h2>
<p>When an alert is generated by the application, Selenium can wait for the alert to appear.</p>
<pre><code>Alert alert =
wait.until(
ExpectedConditions.alertIsPresent()
);
System.out.println(alert.getText());
alert.accept();</code></pre>
<h2>20. Waiting for an Element to Disappear</h2>
<p>The <strong>invisibilityOfElementLocated()</strong> condition waits until an element becomes invisible or is no longer present.</p>
<pre><code>wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.id("loading")
)
);</code></pre>
<p>This is especially useful for loading spinners, overlays, progress indicators, and temporary messages.</p>
<h2>21. Waiting for a Loading Spinner</h2>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(15)
);
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.cssSelector(".loading-spinner")
)
);
driver.findElement(
By.id("submit")
).click();</code></pre>
<h2>22. Waiting for an Element to Become Stale</h2>
<p>The <strong>stalenessOf()</strong> condition waits until a previously located element is no longer attached to the DOM. :contentReference[oaicite:7]{index=7}</p>
<pre><code>WebElement oldElement =
driver.findElement(By.id("result"));
driver.findElement(By.id("refresh")).click();
wait.until(
ExpectedConditions.stalenessOf(oldElement)
);</code></pre>
<h2>23. Waiting for an iframe</h2>
<p>If an iframe is loaded dynamically, Selenium can wait for it to become available and switch into it.</p>
<pre><code>wait.until(
ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.id("paymentFrame")
)
);
driver.findElement(
By.id("cardNumber")
).sendKeys("4111111111111111");
driver.switchTo().defaultContent();</code></pre>
<p>Selenium provides frame-specific expected conditions that can switch the driver into the frame once it is available. :contentReference[oaicite:8]{index=8}</p>
<h2>24. Waiting for Multiple Elements</h2>
<p>Explicit waits can also be used when a page is expected to display multiple elements.</p>
<pre><code>List<WebElement> products =
wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.cssSelector(".product")
)
);
System.out.println(
"Products: " + products.size()
);</code></pre>
<h2>25. Waiting for a Specific Number of Elements</h2>
<pre><code>wait.until(
ExpectedConditions.numberOfElementsToBe(
By.cssSelector(".product"),
5
)
);</code></pre>
<p>This can be useful when an application loads a list incrementally and the test expects a particular number of elements before continuing. Selenium's Java API includes conditions for exact, minimum, and maximum element counts. :contentReference[oaicite:9]{index=9}</p>
<h2>26. Fluent Wait</h2>
<p><strong>FluentWait</strong> provides more control over waiting behavior. It allows configuration of the timeout, polling interval, and exceptions that should be ignored while the condition is being evaluated. :contentReference[oaicite:10]{index=10}</p>
<pre><code>FluentWait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);</code></pre>
<h2>27. Fluent Wait Syntax</h2>
<pre><code>Wait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(10))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);
wait.until(
driver -> driver.findElement(
By.id("username")
).isDisplayed()
);</code></pre>
<h2>28. Polling Interval</h2>
<p>The <strong>polling interval</strong> controls how frequently Selenium checks the waiting condition.</p>
<pre><code>FluentWait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(1));</code></pre>
<p>For example, a one-second polling interval means the condition is evaluated periodically rather than continuously. FluentWait supports configurable polling behavior. :contentReference[oaicite:11]{index=11}</p>
<h2>29. Ignoring Exceptions with FluentWait</h2>
<p>FluentWait can be configured to ignore selected exceptions while waiting.</p>
<pre><code>FluentWait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);</code></pre>
<p>This can be useful when an element is expected to appear during repeated polling.</p>
<h2>30. Custom Condition with FluentWait</h2>
<pre><code>Wait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(15))
.pollingEvery(Duration.ofMillis(500));
WebElement element =
wait.until(
driver -> {
WebElement field =
driver.findElement(By.id("username"));
return field.isDisplayed()
? field
: null;
}
);</code></pre>
<h2>31. Lambda Expression with WebDriverWait</h2>
<p>Java allows a custom condition to be expressed using a lambda.</p>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
driver ->
driver.findElement(
By.id("message")
).isDisplayed()
);</code></pre>
<p>Selenium's wait API accepts a condition that is evaluated until it returns a non-null or non-false result. :contentReference[oaicite:12]{index=12}</p>
<h2>32. Custom Text Wait</h2>
<pre><code>wait.until(
driver -> {
String text =
driver.findElement(
By.id("message")
).getText();
return text.equals("Success");
}
);</code></pre>
<h2>33. Custom Attribute Wait</h2>
<pre><code>wait.until(
driver -> {
String value =
driver.findElement(
By.id("status")
).getAttribute("class");
return value != null &&
value.contains("completed");
}
);</code></pre>
<h2>34. Wait for Element to Be Enabled</h2>
<pre><code>WebElement submitButton =
wait.until(
driver -> {
WebElement button =
driver.findElement(
By.id("submit")
);
return button.isEnabled()
? button
: null;
}
);
submitButton.click();</code></pre>
<h2>35. Wait for Element to Be Selected</h2>
<pre><code>wait.until(
driver ->
driver.findElement(
By.id("terms")
).isSelected()
);</code></pre>
<h2>36. Wait for Element to Be Visible and Enabled</h2>
<pre><code>WebElement submit =
wait.until(
driver -> {
WebElement element =
driver.findElement(
By.id("submit")
);
if (element.isDisplayed() &&
element.isEnabled()) {
return element;
}
return null;
}
);
submit.click();</code></pre>
<h2>37. Implicit vs Explicit Wait</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Implicit Wait</th><th>Explicit Wait</th></tr>
<tr><td>Configured on WebDriver</td><td>Configured for a particular condition</td></tr>
<tr><td>Applies broadly to element lookup</td><td>Applied where synchronization is required</td></tr>
<tr><td>Simple configuration</td><td>More precise control</td></tr>
<tr><td>Does not express detailed conditions</td><td>Supports visibility, clickability, text, title, URL, alerts, frames, etc.</td></tr>
<tr><td>Useful for general lookup delays</td><td>Useful for dynamic application states</td></tr>
</table>
<h2>38. Explicit Wait vs Fluent Wait</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Explicit Wait</th><th>Fluent Wait</th></tr>
<tr><td>Usually implemented with WebDriverWait</td><td>Implemented with FluentWait</td></tr>
<tr><td>Simple condition-based synchronization</td><td>Highly configurable synchronization</td></tr>
<tr><td>Uses predefined ExpectedConditions or custom conditions</td><td>Supports custom conditions and configurable polling</td></tr>
<tr><td>Less configuration required</td><td>More control over polling and ignored exceptions</td></tr>
<tr><td>Suitable for most common dynamic element scenarios</td><td>Useful for advanced synchronization requirements</td></tr>
</table>
<h2>39. Thread.sleep() in Selenium</h2>
<p><strong>Thread.sleep()</strong> pauses the Java thread for a fixed amount of time.</p>
<pre><code>Thread.sleep(5000);</code></pre>
<p>This means the program will stop execution for five seconds regardless of whether the element becomes ready after one second or remains unavailable for the entire five seconds.</p>
<h2>40. Why Thread.sleep() is Usually Not Preferred</h2>
<ul>
<li>It always waits the full specified duration.</li>
<li>It can make tests unnecessarily slow.</li>
<li>It does not check a meaningful application condition.</li>
<li>It can still fail if the application takes longer than the fixed delay.</li>
<li>It makes test synchronization less adaptive.</li>
</ul>
<h2>41. Thread.sleep() vs Explicit Wait</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Thread.sleep()</th><th>Explicit Wait</th></tr>
<tr><td>Fixed delay</td><td>Condition-based wait</td></tr>
<tr><td>Always waits the specified duration</td><td>Continues as soon as the condition succeeds</td></tr>
<tr><td>Not aware of application state</td><td>Checks application state</td></tr>
<tr><td>Can slow down tests</td><td>Usually more efficient for dynamic conditions</td></tr>
<tr><td>Less maintainable for synchronization</td><td>More expressive and maintainable</td></tr>
</table>
<h2>42. Mixing Implicit and Explicit Waits</h2>
<p>Care should be taken when combining implicit and explicit waits. Both mechanisms influence synchronization behavior, and mixing them can make timing behavior harder to understand and troubleshoot.</p>
<p>For most framework designs, it is useful to establish a clear synchronization strategy and use explicit waits where specific application conditions need to be verified.</p>
<h2>43. Wait for Login Button</h2>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement login =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("loginButton")
)
);
login.click();</code></pre>
<h2>44. Wait for Search Results</h2>
<pre><code>WebElement results =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".search-results")
)
);
System.out.println(
results.getText()
);</code></pre>
<h2>45. Wait for a Success Message</h2>
<pre><code>wait.until(
ExpectedConditions.textToBePresentInElementLocated(
By.id("message"),
"Login successful"
)
);</code></pre>
<h2>46. Wait for Loading Overlay to Disappear</h2>
<pre><code>wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.cssSelector(".loading-overlay")
)
);</code></pre>
<h2>47. Wait for URL After Login</h2>
<pre><code>loginButton.click();
wait.until(
ExpectedConditions.urlContains("dashboard")
);</code></pre>
<h2>48. Wait for Title After Navigation</h2>
<pre><code>driver.findElement(
By.linkText("Dashboard")
).click();
wait.until(
ExpectedConditions.titleContains("Dashboard")
);</code></pre>
<h2>49. Wait for Dynamic Button</h2>
<pre><code>WebElement button =
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector("button.submit")
)
);
button.click();</code></pre>
<h2>50. Wait for Dynamic Text</h2>
<pre><code>wait.until(
ExpectedConditions.textToBePresentInElementLocated(
By.id("status"),
"Completed"
)
);</code></pre>
<h2>51. Wait for AJAX Request Result</h2>
<p>When an application updates the page after an AJAX request, waiting for a meaningful UI condition is generally more reliable than using a fixed delay.</p>
<pre><code>wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".ajax-result")
)
);</code></pre>
<h2>52. Wait for a Modal Dialog</h2>
<pre><code>WebElement modal =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".modal")
)
);
System.out.println(
"Modal: " + modal.getText()
);</code></pre>
<h2>53. Wait for a Checkbox</h2>
<pre><code>WebElement checkbox =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("terms")
)
);
if (!checkbox.isSelected()) {
checkbox.click();
}</code></pre>
<h2>54. Wait for a Radio Button</h2>
<pre><code>WebElement radio =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("male")
)
);
if (!radio.isSelected()) {
radio.click();
}</code></pre>
<h2>55. Wait for an Input Field</h2>
<pre><code>WebElement username =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.clear();
username.sendKeys("admin");</code></pre>
<h2>56. Wait for Multiple Products</h2>
<pre><code>List<WebElement> products =
wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.cssSelector(".product-card")
)
);
for (WebElement product : products) {
System.out.println(product.getText());
}</code></pre>
<h2>57. Wait for an Alert and Accept It</h2>
<pre><code>Alert alert =
wait.until(
ExpectedConditions.alertIsPresent()
);
System.out.println(
"Alert: " + alert.getText()
);
alert.accept();</code></pre>
<h2>58. Wait for an iframe and Switch to It</h2>
<pre><code>wait.until(
ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.id("paymentFrame")
)
);
WebElement cardNumber =
driver.findElement(By.id("cardNumber"));
cardNumber.sendKeys("4111111111111111");
driver.switchTo().defaultContent();</code></pre>
<h2>59. Wait for a Page Element After Refresh</h2>
<pre><code>driver.navigate().refresh();
WebElement username =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.sendKeys("admin");</code></pre>
<h2>60. Handling TimeoutException</h2>
<p>If the expected condition is not satisfied within the configured timeout, Selenium can throw a <strong>TimeoutException</strong>.</p>
<pre><code>try {
WebElement button =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
);
button.click();
} catch (TimeoutException e) {
System.out.println(
"Element was not ready within the timeout."
);
}</code></pre>
<h2>61. Common Causes of TimeoutException</h2>
<ul>
<li>Incorrect locator.</li>
<li>Element never appears.</li>
<li>Element remains hidden.</li>
<li>Element remains disabled.</li>
<li>Application response is slower than expected.</li>
<li>Wrong page or browser window.</li>
<li>Element is inside an iframe.</li>
<li>Unexpected popup or overlay blocks the application.</li>
<li>Incorrect expected condition.</li>
<li>Application behavior changed.</li>
</ul>
<h2>62. Handling Stale Elements with Waits</h2>
<p>Dynamic applications can redraw elements. Selenium provides conditions such as <strong>stalenessOf()</strong> and <strong>refreshed()</strong> to help synchronize with DOM changes. :contentReference[oaicite:13]{index=13}</p>
<pre><code>WebElement oldElement =
driver.findElement(By.id("result"));
driver.findElement(By.id("refresh")).click();
wait.until(
ExpectedConditions.stalenessOf(oldElement)
);
WebElement newElement =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("result")
)
);</code></pre>
<h2>63. refreshed() Condition</h2>
<p>The <strong>refreshed()</strong> condition can wrap another expected condition when an element may be redrawn during the wait.</p>
<pre><code>WebElement result =
wait.until(
ExpectedConditions.refreshed(
ExpectedConditions.visibilityOfElementLocated(
By.id("result")
)
)
);</code></pre>
<h2>64. Logical AND Conditions</h2>
<p>Selenium's expected conditions support combining conditions with logical operations such as <strong>and()</strong>.</p>
<pre><code>wait.until(
ExpectedConditions.and(
ExpectedConditions.visibilityOfElementLocated(
By.id("submit")
),
ExpectedConditions.elementToBeClickable(
By.id("submit")
)
)
);</code></pre>
<h2>65. Logical OR Conditions</h2>
<p>The <strong>or()</strong> condition can be used when one of several possible conditions is acceptable.</p>
<pre><code>wait.until(
ExpectedConditions.or(
ExpectedConditions.visibilityOfElementLocated(
By.id("success")
),
ExpectedConditions.visibilityOfElementLocated(
By.id("error")
)
)
);</code></pre>
<p>Selenium's Java ExpectedConditions API provides logical <strong>and()</strong> and <strong>or()</strong> combinations for expected conditions. :contentReference[oaicite:14]{index=14}</p>
<h2>66. Wait Until a Condition Is False</h2>
<p>Wait APIs also support waiting until a condition becomes false through <strong>until()</strong> with an appropriate custom condition or the relevant wait functionality.</p>
<pre><code>wait.until(
driver ->
!driver.findElement(
By.id("loading")
).isDisplayed()
);</code></pre>
<h2>67. WebDriverWait with Custom Timeout Message</h2>
<p>FluentWait provides methods for configuring timeout behavior and timeout messages. :contentReference[oaicite:15]{index=15}</p>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.withMessage(
"Login button was not ready"
);
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);</code></pre>
<h2>68. Web Element Wait Strategy</h2>
<pre><code>Identify Element
|
v
Is Element Present?
|
Yes
|
v
Is Element Visible?
|
Yes
|
v
Is Element Enabled?
|
Yes
|
v
Is Element Clickable?
|
Yes
|
v
Perform Action</code></pre>
<h2>69. Choosing the Correct Wait</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Situation</th><th>Recommended Approach</th></tr>
<tr><td>General element lookup delay</td><td>Implicit Wait or targeted explicit synchronization</td></tr>
<tr><td>Element must become visible</td><td>visibilityOfElementLocated()</td></tr>
<tr><td>Element must be clicked</td><td>elementToBeClickable()</td></tr>
<tr><td>Element must exist in DOM</td><td>presenceOfElementLocated()</td></tr>
<tr><td>Text must appear</td><td>textToBePresentInElementLocated()</td></tr>
<tr><td>Loading overlay must disappear</td><td>invisibilityOfElementLocated()</td></tr>
<tr><td>Page title must change</td><td>titleIs() or titleContains()</td></tr>
<tr><td>URL must change</td><td>urlToBe() or urlContains()</td></tr>
<tr><td>Alert must appear</td><td>alertIsPresent()</td></tr>
<tr><td>Iframe must become available</td><td>frameToBeAvailableAndSwitchToIt()</td></tr>
<tr><td>DOM element must be replaced</td><td>stalenessOf() or refreshed()</td></tr>
<tr><td>Custom polling required</td><td>FluentWait</td></tr>
</table>
<h2>70. Complete Login Example Using Explicit Wait</h2>
<pre><code>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 LoginWithWait {
public static void main(String[] args) {
WebDriver driver =
new ChromeDriver();
try {
driver.manage().window().maximize();
driver.get(
"https://example.com"
);
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.clear();
username.sendKeys("admin");
password.clear();
password.sendKeys("Password123");
WebElement loginButton =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);
loginButton.click();
wait.until(
ExpectedConditions.urlContains(
"dashboard"
)
);
} finally {
driver.quit();
}
}
}</code></pre>
<h2>71. Complete Fluent Wait Example</h2>
<pre><code>import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
public class FluentWaitExample {
public static void main(String[] args) {
WebDriver driver =
new ChromeDriver();
try {
driver.get(
"https://example.com"
);
FluentWait<WebDriver> wait =
new FluentWait<>(driver)
.withTimeout(
Duration.ofSeconds(15)
)
.pollingEvery(
Duration.ofMillis(500)
)
.ignoring(
NoSuchElementException.class
);
WebElement username =
wait.until(
driver ->
driver.findElement(
By.id("username")
)
);
username.sendKeys("admin");
} finally {
driver.quit();
}
}
}</code></pre>
<h2>72. Practical E-Commerce Wait Example</h2>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(15)
);
WebElement searchBox =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("search")
)
);
searchBox.sendKeys("Laptop");
searchBox.sendKeys(Keys.ENTER);
WebElement product =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".product-card")
)
);
System.out.println(
"Product: " + product.getText()
);
WebElement addToCart =
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(".add-to-cart")
)
);
addToCart.click();
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".cart-success")
)
);</code></pre>
<h2>73. Practical Registration Form Wait Example</h2>
<pre><code>WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement firstName =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("firstName")
)
);
WebElement email =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("email")
)
);
WebElement password =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
);
firstName.sendKeys("John");
email.sendKeys("[email protected]");
password.sendKeys("Password123");
WebElement terms =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("terms")
)
);
if (!terms.isSelected()) {
terms.click();
}
WebElement register =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("register")
)
);
register.click();</code></pre>
<h2>74. Page Object Model with Waits</h2>
<p>In a professional Selenium framework, explicit waits are commonly centralized or encapsulated within page classes or reusable utility methods. This keeps synchronization logic consistent across tests.</p>
<pre><code>import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
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 enterUsername(String value) {
WebElement element =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
username
)
);
element.clear();
element.sendKeys(value);
}
public void enterPassword(String value) {
WebElement element =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
password
)
);
element.clear();
element.sendKeys(value);
}
public void clickLogin() {
WebElement element =
wait.until(
ExpectedConditions.elementToBeClickable(
loginButton
)
);
element.click();
}
}</code></pre>
<h2>75. Reusable Wait Utility</h2>
<p>A reusable wait utility can reduce duplicate wait code in larger Selenium projects.</p>
<pre><code>import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WaitUtility {
private WebDriver driver;
private WebDriverWait wait;
public WaitUtility(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
)
);
}
public void waitForInvisible(By locator) {
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
locator
)
);
}
}</code></pre>
<h2>76. Common Wait-Related Exceptions</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Exception</th><th>Possible Cause</th><th>Solution</th></tr>
<tr><td>TimeoutException</td><td>Condition was not satisfied within the timeout</td><td>Check locator, condition, page state, and timeout</td></tr>
<tr><td>NoSuchElementException</td><td>Element could not be found</td><td>Verify locator and synchronization</td></tr>
<tr><td>StaleElementReferenceException</td><td>DOM changed after the element was located</td><td>Locate the element again and use an appropriate stale-element wait</td></tr>
<tr><td>ElementNotInteractableException</td><td>Element cannot receive the requested interaction</td><td>Wait for visibility or interactability</td></tr>
<tr><td>ElementClickInterceptedException</td><td>Another element blocks the target</td><td>Wait for overlays to disappear and use elementToBeClickable()</td></tr>
</table>
<h2>77. Common Wait Mistakes</h2>
<ul>
<li>Using Thread.sleep() everywhere.</li>
<li>Using very large timeout values without justification.</li>
<li>Waiting for visibility when the actual requirement is clickability.</li>
<li>Using presence when the element must be visible.</li>
<li>Using an incorrect locator.</li>
<li>Ignoring loading overlays.</li>
<li>Ignoring iframe context.</li>
<li>Using stale WebElement references.</li>
<li>Adding unnecessary waits after every Selenium command.</li>
<li>Mixing implicit and explicit waits without a clear strategy.</li>
<li>Using JavaScript to bypass synchronization problems.</li>
<li>Not identifying the actual application condition that indicates readiness.</li>
</ul>
<h2>78. Best Practices for Selenium Waits</h2>
<ol>
<li>Prefer condition-based synchronization over fixed delays.</li>
<li>Use explicit waits for important dynamic application states.</li>
<li>Choose the expected condition that matches the actual requirement.</li>
<li>Use <strong>visibilityOfElementLocated()</strong> when an element must be visible.</li>
<li>Use <strong>elementToBeClickable()</strong> when an element must be ready for clicking.</li>
<li>Use <strong>presenceOfElementLocated()</strong> when DOM presence is sufficient.</li>
<li>Use <strong>invisibilityOfElementLocated()</strong> for loading overlays and temporary elements.</li>
<li>Use <strong>stalenessOf()</strong> when an old DOM element must disappear.</li>
<li>Use <strong>FluentWait</strong> when custom polling or exception handling is required.</li>
<li>Keep timeout values reasonable and consistent.</li>
<li>Use reusable wait utilities in larger frameworks.</li>
<li>Keep synchronization logic close to page interaction logic or in a dedicated utility layer.</li>
<li>Avoid unnecessary Thread.sleep() calls.</li>
<li>Do not use waits as a substitute for fixing an incorrect locator.</li>
<li>Always understand what application state indicates that the next action is safe.</li>
</ol>
<h2>79. Wait Strategy Decision Flow</h2>
<pre><code>Does the Test Need Synchronization?
|
Yes
|
v
Is General Element Lookup Delay Enough?
|
+----+----+
| |
Yes No
| |
v v
Implicit Wait Specific Condition?
|
v
Explicit Wait
|
v
Is Advanced Polling Needed?
|
+----+----+
| |
No Yes
| |
v v
WebDriverWait FluentWait</code></pre>
<h2>80. Waits with Locator Strategy</h2>
<p>Waits and locators work together. A wait cannot compensate for an incorrect or unstable locator. First select a reliable locator, then apply the appropriate synchronization condition.</p>
<pre><code>By loginButton =
By.cssSelector("button.login");
WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement button =
wait.until(
ExpectedConditions.elementToBeClickable(
loginButton
)
);
button.click();</code></pre>
<h2>81. Waits with WebElement Operations</h2>
<pre><code>WebElement username =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("username")
)
);
username.clear();
username.sendKeys("admin");
WebElement password =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("password")
)
);
password.clear();
password.sendKeys("Password123");
WebElement login =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("login")
)
);
login.click();</code></pre>
<h2>82. Waits and Page Load</h2>
<p>A page navigation command does not necessarily mean that every application element is ready for interaction. Modern applications may continue making API calls and rendering components after the initial navigation.</p>
<p>Therefore, instead of relying only on page navigation completion, wait for an application-specific condition such as a key element becoming visible, a loading overlay disappearing, or a successful navigation state being reached.</p>
<h2>83. Waits and AJAX Applications</h2>
<p>AJAX-based applications often update individual portions of a page without performing a full browser navigation. In such applications, explicit waits are particularly useful because the test can wait for the specific result of the asynchronous operation.</p>
<pre><code>driver.findElement(
By.id("loadData")
).click();
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("dataResult")
)
);</code></pre>
<h2>84. Waits and Loading Spinners</h2>
<p>A common real-world synchronization pattern is to wait for a loading spinner to disappear before interacting with the next element.</p>
<pre><code>wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.cssSelector(".spinner")
)
);
WebElement checkout =
wait.until(
ExpectedConditions.elementToBeClickable(
By.id("checkout")
)
);
checkout.click();</code></pre>
<h2>85. Waits and Dynamic Tables</h2>
<pre><code>wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("table tbody tr")
)
);
List<WebElement> rows =
driver.findElements(
By.cssSelector("table tbody tr")
);
for (WebElement row : rows) {
System.out.println(row.getText());
}</code></pre>
<h2>86. Waits and Search Suggestions</h2>
<pre><code>WebElement search =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("search")
)
);
search.sendKeys("Selenium");
WebElement suggestion =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".suggestion")
)
);
suggestion.click();</code></pre>
<h2>87. Waits and File Upload Controls</h2>
<p>For a standard file input, Selenium generally interacts directly with the input using <strong>sendKeys()</strong>. If the input is dynamically rendered, an explicit wait can be used first.</p>
<pre><code>WebElement fileInput =
wait.until(
ExpectedConditions.presenceOfElementLocated(
By.id("fileUpload")
)
);
fileInput.sendKeys(
"C:\\files\\document.pdf"
);</code></pre>
<h2>88. Waits and Alerts</h2>
<pre><code>driver.findElement(
By.id("delete")
).click();
Alert alert =
wait.until(
ExpectedConditions.alertIsPresent()
);
System.out.println(
alert.getText()
);
alert.accept();</code></pre>
<h2>89. Waits and Windows</h2>
<p>When an action opens a new browser window or tab, Selenium can wait until the expected number of windows is available.</p>
<pre><code>driver.findElement(
By.id("openWindow")
).click();
wait.until(
ExpectedConditions.numberOfWindowsToBe(
2
)
);
System.out.println(
"Windows: " + driver.getWindowHandles().size()
);</code></pre>
<h2>90. Practical Project: Selenium Wait Practice</h2>
<p>Build a Selenium Java automation project that demonstrates implicit, explicit, and fluent wait concepts on a dynamic web application.</p>
<h3>Project Requirements</h3>
<ul>
<li>Launch Chrome browser.</li>
<li>Open a test application.</li>
<li>Configure an appropriate synchronization strategy.</li>
<li>Locate a dynamically loaded username field.</li>
<li>Wait for the username field to become visible.</li>
<li>Enter username and password.</li>
<li>Wait for the login button to become clickable.</li>
<li>Click the login button.</li>
<li>Wait for a loading indicator to disappear.</li>
<li>Wait for a success message.</li>
<li>Validate the success message.</li>
<li>Wait for the expected URL.</li>
<li>Handle a dynamically displayed alert if required.</li>
<li>Close the browser using driver.quit().</li>
</ul>
<h2>91. Practical Complete Wait Program</h2>
<pre><code>import java.time.Duration;
import org.openqa.selenium.Alert;
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 SeleniumWaitPractice {
public static void main(String[] args) {
WebDriver driver =
new ChromeDriver();
try {
driver.manage().window().maximize();
driver.get(
"https://www.selenium.dev/selenium/web/web-form.html"
);
WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
WebElement textBox =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.name("my-text")
)
);
textBox.clear();
textBox.sendKeys(
"Selenium Waits with Java"
);
WebElement submitButton =
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector("button")
)
);
submitButton.click();
WebElement message =
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.id("message")
)
);
System.out.println(
"Message: " + message.getText()
);
} finally {
driver.quit();
}
}
}</code></pre>
<h2>92. Interview Questions on Selenium Waits</h2>
<h3>Q1. What is a wait in Selenium?</h3>
<p>A wait is a synchronization mechanism that allows Selenium to wait for a specified condition or element state before continuing execution.</p>
<h3>Q2. What are the main types of waits in Selenium?</h3>
<p>The commonly discussed Selenium wait mechanisms are Implicit Wait, Explicit Wait, and Fluent Wait. Fixed delays using Thread.sleep() are also possible in Java, but they are not condition-based synchronization.</p>
<h3>Q3. What is Implicit Wait?</h3>
<p>Implicit Wait configures WebDriver to wait for a specified amount of time during element lookup when the element is not immediately available.</p>
<h3>Q4. What is Explicit Wait?</h3>
<p>Explicit Wait waits for a particular condition, such as visibility, clickability, text, URL, title, alert presence, or frame availability.</p>
<h3>Q5. What is WebDriverWait?</h3>
<p>WebDriverWait is a Selenium Java class used for condition-based waits and is a specialization of FluentWait for WebDriver.</p>
<h3>Q6. What is FluentWait?</h3>
<p>FluentWait is a configurable wait that allows timeout, polling interval, and ignored exceptions to be specified.</p>
<h3>Q7. What is ExpectedConditions?</h3>
<p>ExpectedConditions is a Selenium support class containing commonly used predefined conditions for explicit waits.</p>
<h3>Q8. What is the difference between presence and visibility?</h3>
<p>Presence means the element exists in the DOM. Visibility means the element is present and visible for the relevant condition.</p>
<h3>Q9. What is elementToBeClickable()?</h3>
<p>It waits until an element is visible and enabled so that it can be clicked.</p>
<h3>Q10. Why should Thread.sleep() generally be avoided?</h3>
<p>Thread.sleep() always pauses for the full fixed duration and does not check whether the application is ready earlier or remains unready longer.</p>
<h3>Q11. What happens when an explicit wait times out?</h3>
<p>If the required condition is not satisfied within the configured timeout, a timeout-related failure such as TimeoutException can occur.</p>
<h3>Q12. How do you wait for an alert?</h3>
<pre><code>Alert alert =
wait.until(
ExpectedConditions.alertIsPresent()
);</code></pre>
<h3>Q13. How do you wait for an iframe?</h3>
<pre><code>wait.until(
ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.id("frame")
)
);</code></pre>
<h3>Q14. How do you wait for a loading spinner to disappear?</h3>
<pre><code>wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.cssSelector(".spinner")
)
);</code></pre>
<h3>Q15. Can you create a custom wait condition?</h3>
<p>Yes. Java lambdas can be used with wait.until(), and FluentWait can also be configured for custom polling behavior.</p>
<h2>93. Quick Revision</h2>
<table border="1" cellpadding="8" cellspacing="0">
<tr><th>Concept</th><th>Java Example</th><th>Purpose</th></tr>
<tr><td>Implicit Wait</td><td>implicitlyWait()</td><td>General element lookup synchronization</td></tr>
<tr><td>Explicit Wait</td><td>WebDriverWait</td><td>Wait for a specific condition</td></tr>
<tr><td>Fluent Wait</td><td>FluentWait</td><td>Custom timeout and polling behavior</td></tr>
<tr><td>Until</td><td>wait.until()</td><td>Wait for a condition</td></tr>
<tr><td>Presence</td><td>presenceOfElementLocated()</td><td>Wait for DOM presence</td></tr>
<tr><td>Visibility</td><td>visibilityOfElementLocated()</td><td>Wait for visibility</td></tr>
<tr><td>Clickable</td><td>elementToBeClickable()</td><td>Wait for click readiness</td></tr>
<tr><td>Text</td><td>textToBePresentInElementLocated()</td><td>Wait for text</td></tr>
<tr><td>Title</td><td>titleIs()</td><td>Wait for exact title</td></tr>
<tr><td>URL</td><td>urlContains()</td><td>Wait for URL state</td></tr>
<tr><td>Alert</td><td>alertIsPresent()</td><td>Wait for alert</td></tr>
<tr><td>Invisible</td><td>invisibilityOfElementLocated()</td><td>Wait for element to disappear</td></tr>
<tr><td>Stale</td><td>stalenessOf()</td><td>Wait for DOM detachment</td></tr>
<tr><td>Frame</td><td>frameToBeAvailableAndSwitchToIt()</td><td>Wait for iframe</td></tr>
<tr><td>Polling</td><td>pollingEvery()</td><td>Configure FluentWait polling</td></tr>
</table>
<h2>94. Final Summary</h2>
<p><strong>Waits with Java in Selenium</strong> are essential for creating reliable and stable browser automation. Modern applications frequently load elements asynchronously, update the DOM dynamically, display loading indicators, open dialogs, and change page content after API responses.</p>
<p><strong>Implicit Wait</strong> provides general synchronization for element lookup, while <strong>Explicit Wait</strong> allows the automation script to wait for a specific condition. <strong>WebDriverWait</strong> is commonly used for explicit waits, and <strong>ExpectedConditions</strong> provides reusable conditions such as visibility, presence, clickability, text, title, URL, alert, iframe, invisibility, and staleness. :contentReference[oaicite:16]{index=16}</p>
<p><strong>FluentWait</strong> provides additional control over timeout, polling interval, and ignored exceptions and is useful when advanced synchronization behavior is required. :contentReference[oaicite:17]{index=17}</p>
<p>For professional Selenium automation, use condition-based synchronization rather than unnecessary fixed delays, select the condition that matches the actual application state, use reliable locators, handle dynamic DOM changes, and centralize reusable wait logic where appropriate.</p>
<hr>
<h2>95. Course Resources</h2>
<p><strong>Learn more about Selenium Automation Testing:</strong></p>
<p><a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training</a></p>
<p><strong>Register for a course demo:</strong></p>
<p><a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a></p>
</div>