- Selenium Tutorial
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium IDE Tutorial
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - Errors & Logging
- Selenium - Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
- Selenium Useful Resources
- Selenium - Quick Guide
- Selenium - Useful Resources
- Selenium - Automation Practice
- Selenium - Discussion
Selenium Webdriver - Wait Support
Selenium Webdriver can be used in association with explicit, implicit, and fluent waits to achieve synchronization and provide Wait support. The waits are mainly applied in the tests to deal with dynamic web pages.
Often there lies some lag time before the whole page loads, and web elements are completely available on the web page. The waits available in Selenium Webdriver help to hold back the test execution till an element appears/disappears on a web page in its correct state.
Let us discuss some of the wait types in Selenium Webdriver −
Implicit Wait
It is the default wait available in Selenium. It is a kind of global wait applicable to the whole driver session. The default wait time is 0, meaning if an element is not found, an error will be thrown straight away. However, if a wait time is set, an error will be thrown once the wait time surpasses. Once the element is identified, its reference is returned and execution moves to the next step. We should use implicit wait in an optimal way, a larger wait time would increase the execution time of the tests.
Syntax
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
In the above example, a NoSuchElementException will be thrown after 15 seconds.
Explicit Wait
It is similar to loops added to code that poll the web page for a particular scenario to become true prior exiting the loop and moving to the next line of code. If the situation is unfulfilled within the time set out, a timeout exception will be thrown.The explicit waits are applied to a specific element with its condition with the help of the ExpectedConditions and WebDriverWait classes. Some of the expected conditions for explicit waits are −
titleContains, alertIsPresent, invisibilityOfElementLocated(By locator), titleIs(), invisibilityOfElementWithText(By locator, String s), visibilityOf(), textToBePresentInElement(By locator, String s), visibilityOfElementLocated(By locator), visibilityOfAllElements(), presenceOfAllElementsLocatedBy(By locator), presenceOfElementLocated(By locator), elementToBeClickable(By locator), stalenessOf(WebElement e), textToBePresentInElementValue(), textToBePresentInElementLocated(), elementSelectionStateToBe(), elementToBeSelected(), frameToBeAvaliableAndSwitchToIt()
We should be cautious while adding both implicit and explicit waits in the same driver session, for instance having a combination of explicit wait of 15 seconds and implicit wait of 10 seconds can result in a timeout to occur after 20 seconds.
Syntax
WebDriverWait wt = new WebDriverWait(driver,5); wt.until(ExpectedConditions.invisibilityOfElementLocated (By.xpath("//*[@class='mui−btn']")));
In the above example, a TimeOutException will be thrown after 5 seconds in case the expected condition of an element to be invisible is not met within that specified time.
Fluent Wait
This is the maximum time the driver waits for a specific condition for an element to be true. It also determines the interval at which the driver will verify(polling interval) prior to locating an element or throwing an exception. The fluent wait is a customized explicit wait which gives the option to handle specific exceptions automatically along with customized messages when an exception occurs. The FluentWait class is used to add fluent waits to tests.
Syntax
Wait wt = new FluentWait(driver).withTimeout(20, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(ElementNotInteractableException.class); wt.until(ExpectedConditions.titleIs("Tutorialspoint"));
In the above example, timeout and polling interval are specified meaning that the driver would wait for 20 seconds and perform a polling in an interval of 5 seconds within the timeout time for the Tutorialspoint browser title condition to be met. If the condition is not satisfied, within that time frame, an exception will be thrown, else the next step will be executed.
Let us take an example of the below image, where we would first click on the Click Me button.
After clicking on the Click Me, we would use explicit wait and wait for the presence of the text You have done a dynamic click to be available on a web page.
Syntax
Syntax on explicit wait −
Webdriver driver = new ChromeDriver(); // identify button then click on it WebElement l = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[1]")); l.click(); // Identify text WebElement e = driver.findElement(By.xpath("//*[@id='welcomeDiv']")); // explicit wait to expected condition for presence of a text WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(2)); wt.until(ExpectedConditions.presenceOfElementLocated (By.xpath("//*[@id='welcomeDiv']"))); // get text System.out.println("Get text after clicking: " + e.getText());
Example
Code Implementation on ExplicitsWait.java class file.
package org.example; 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; import java.time.Duration; import java.util.concurrent.TimeUnit; public class ExplicitsWait { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // launching a browser and open a URL driver.get("https://www.tutorialspoint.com/selenium/practice/buttons.php"); // identify button then click on it WebElement l = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]")); l.click(); // Identify text WebElement e = driver.findElement(By.xpath("//*[@id='welcomeDiv']")); // explicit wait to expected condition for presence of a text WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(2)); wt.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='welcomeDiv']"))); // get text System.out.println("Get text after clicking: " + e.getText()); // Quitting browser driver.quit(); } }
Output
Get text after clicking: You have done a dynamic click Process finished with exit code 0
In the above example, the text obtained after clicking on the Click Me button was You have done a dynamic click.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us take another example of the below page where we would first click the Color Change button.
After clicking on the Color Change, we would use fluent wait and wait for the presence of the button Visible After 5 Seconds to be available on a web page.
Syntax
Syntax on fluent wait −
Webdriver driver = new ChromeDriver(); // identify button then click WebElement l = driver.findElement(By.xpath("<value of xpath>")); l.click(); // fluent wait of 6 secs till other button appears Wait<WebDriver> w = new FluentWait<WebDriver>(driver) .withTimeout(Duration.ofSeconds(20)) .pollingEvery(Duration.ofSeconds(6)) .ignoring(NoSuchElementException.class); WebElement m = w.until(ExpectedConditions.visibilityOfElementLocated (By.xpath("<value of xpath>"))); // checking button presence System.out.println("Button appeared: " + m.isDisplayed());
Example
Code Implementation on Fluentwts.java class file.
package org.example; import org.openqa.selenium.*; 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; import java.time.Duration; public class Fluentwts { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // launching a browser and open a URL driver.get("https://www.tutorialspoint.com/selenium/practice/dynamic-prop.php"); // identify button then click WebElement l = driver.findElement(By.xpath("//*[@id='colorChange']")); l.click(); // fluent wait of 6 secs till other button appears Wait<WebDriver> w = new FluentWait<WebDriver>(driver) .withTimeout(Duration.ofSeconds(20)) .pollingEvery(Duration.ofSeconds(6)) .ignoring(NoSuchElementException.class); WebElement m = w.until(ExpectedConditions.visibilityOfElementLocated (By.xpath("//*[@id='visibleAfter']"))); // checking button presence System.out.println("Button appeared: " + m.isDisplayed()); // Quitting browser driver.quit(); } }
Output
Button appeared: true Process finished with exit code 0
In the above example, we observed that the button Visible After 5 Seconds obtained after clicking on the button Color Change.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
In this tutorial, we had discussed how to handle Wait support using Selenium Webdriver.
To Continue Learning Please Login
Login with Google