Example of Selenium Code
Let’s delve into the implementation of Fluent Wait in Selenium WebDriver using Java:
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
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.FluentWait;
import org.openqa.selenium.support.ui.Wait;
public class FluentWaitTest {
public static void main(String[] args)
{
// Set the path to ChromeDriver
System.setProperty("webdriver.chrome.driver",
"yourPath/to/chromedriver");
// Initialize WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to the webpage
driver.get("https://somewebsite.com");
// Define Fluent Wait with a maximum timeout of 30
// seconds and polling every 2 seconds
Wait<WebDriver> wait
= new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(40))
.pollingEvery(Duration.ofSeconds(3))
.ignoring(TimeoutException.class);
try {
// Wait until the element with ID "my_element"
// is clickable
WebElement element = wait.until(
ExpectedConditions.elementToBeClickable(
By.id("my_element")));
// Once the element is clickable, perform
// desired actions
element.click();
System.out.println(
"Element clicked successfully!");
}
catch (TimeoutException e) {
System.out.println(
"Timed out waiting for element to be clickable..");
}
finally {
// Quit the WebDriver
driver.quit();
}
}
}
Output
Explanation of the above Java Code
Import Statements:
- Import necessary Selenium WebDriver and support classes/interfaces for interacting with web elements, waiting for conditions, and handling exceptions.
Class Definition:
- Define a class named FluentWaitExample which contains the main method.
WebDriver Initialization:
- Set the system property to specify the path to the ChromeDriver executable.
- Initialize a new instance of the ChromeDriver, which launches a new Chrome browser session.
Navigation:
- Use the get() method to navigate to the specified URL (“https://example.com”).
Fluent Wait Configuration:
- Define a Fluent Wait with the WebDriver instance.
- Set the maximum timeout duration to 30 seconds using with Timeout(Duration.ofSeconds(30)).
- Specify the polling interval as 2 seconds using polling Every(Duration.ofSeconds(2)).
- Ignore the ‘TimeoutException’ during the waiting period using ignoring(TimeoutException.class).
Waiting for Element to be Clickable:
- Use the until() method of the Fluent Wait object to wait until the element with ID “my_element” is clickable.
- The ‘ExpectedConditions’ class provides various expected conditions to wait for, and elementToBeClickable() ensures that the element can be clicked.
- Once the element is clickable, store it in the WebElement object named element.
Performing Action on Element:
- If the element is clickable within the specified timeout, perform the desired action, which is clicking the element using the click() method.
- Print a success message indicating that the element was clicked successfully.
Exception Handling:
- Catch any TimeoutException that may occur during the waiting period.
- If a TimeoutException occurs, print a message indicating that the waiting timed out for the element to be clickable.
WebDriver Cleanup:
- Use the quit() method to close the browser window and terminate the WebDriver session, ensuring proper cleanup.
NOTE:
Ensure you have the Selenium WebDriver Java bindings and ChromeDriver configured correctly in your project. You can download the WebDriver Java bindings from the Selenium website and ChromeDriver from the ChromeDriver website.
What is Fluent Wait in Selenium?
Selenium has rock web automation testing that has allowed testers to mimic interactions with web applications that users may have performed in the past. Yet, one of the drawbacks of automated testing is that dynamic web elements may not be generally available during testing or appear inconsistent.
Table of Content
- What are Wait Commands in Selenium?
- Why do Users Need Selenium to Wait for Commands?
- What is Fluent Wait in Selenium?
- Syntax of Fluent Wait in Selenium
- Different Features of Fluent Wait
- Example of Selenium Code
- Conclusion
To resolve this issue, we are here with the Wait Commands in Selenium.