Middle
Name several types of expectations in Selenium and explain them.
sobes.tech AI
Answer from AI
-
Implicit Waits
- Configured once for the entire WebDriver session.
- WebDriver will wait for a specified amount of time before throwing a
NoSuchElementExceptionwhen searching for an element. - The wait time applies to each call of
findElement(s). - If the element is found earlier, WebDriver will not wait until the end.
-
Explicit Waits
- Applied to a specific condition for a particular element.
- WebDriver will wait for the specified condition to be true within the set maximum time.
- If the condition is met earlier, the execution continues.
- If the condition is not met within the maximum time, a
TimeoutExceptionis thrown. - Usually used with
WebDriverWaitandExpectedConditions.
// Example of Explicit Wait WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("myButton"))); -
Fluent Waits
- A more advanced version of Explicit Waits.
- Allows defining not only the maximum wait time but also:
- The polling interval.
- Types of exceptions to ignore during waiting.
// Example of Fluent Wait Wait<WebDriver> wait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofSeconds(5)) .ignoring(NoSuchElementException.class); WebElement element = wait.until(driver -> driver.findElement(By.id("myElement")));
Summary table:
| Wait Type | Application Area | Flexibility | Exception Handling | Usage |
|---|---|---|---|---|
| Implicit Wait | Entire WebDriver session | Low (fixed time) | Automatic | General wait time for all searches |
| Explicit Wait | Specific condition for an element | High (conditional) | Manual (TimeoutException) |
Waiting for a specific element state |
| Fluent Wait | Specific condition for an element | Very high (interval, ignore) | Manual (TimeoutException) |
Fine-tuned wait configuration |