Using WebDriverWait in Selenium — All you need to know

As a developer, you might have witnessed ‘Wait’ commands while writing the first Selenium program. These are essential pieces of code that are required to execute a test case properly. This article talks in depth about the Wait commands, their significance, types, and how they are used.

What are Selenium Waits?

Selenium Waits are commands that help wait for a certain condition to happen before proceeding further in the code. They are an essential part of writing a robust automation script. They allow you to pause the script execution for a certain period or until specific conditions are met. Waits are essential to handle dynamic web pages or elements that may take some time to load or appear on the page. It is important to use the correct type of Selenium Wait for the given situation to ensure that the script runs properly and does not fail due to the page not loading. 

Using WebDriverWait in Selenium Java is essential for implementing explicit waits in test automation. It allows you to wait for specific conditions before performing actions, leading to more efficient and reliable test scripts by synchronizing with the application’s behavior. This ensures better stability and accuracy in your test execution.

What are the types of Selenium Waits?

Selenium provides different types of Waits to handle synchronization in test scripts. These Waits help ensure that the interactions with web elements occur at the appropriate time. Here are the three types of Waits in Selenium:

Explicit Wait

In Selenium, an Explicit Wait allows you to wait for specific conditions to be met before proceeding with further actions in your test scripts. It provides more control and precision compared to Implicit Waits, as you can define the conditions and maximum time to wait for a specific element or condition to become true. The WebDriverWait class is used to implement Explicit Waits in Selenium.

Here’s a detailed explanation of Explicit Waits in Selenium:

How does it work?

The Explicit Wait starts by creating an instance of WebDriverWait, passing the WebDriver instance, and the maximum time to wait as parameters.

You can specify a condition (expected condition) that needs to be satisfied for the wait to complete successfully.

The Wait will keep polling the condition at regular intervals until it becomes true or the maximum wait time is reached.

If the condition is not met within the specified time, a TimeoutException will be thrown.

Usage example


 WebDriver wait in Python
   from selenium import webdriver
   from selenium.webdriver.common.by import By
   from selenium.webdriver.support.ui import WebDriverWait
   from selenium.webdriver.support import expected_conditions as EC

   driver = webdriver.Chrome()
   wait = WebDriverWait(driver, 10)  # Wait for a maximum of 10 seconds

   driver.get("https://www.example.com")
   element = wait.until(EC.visibility_of_element_located((By.ID, "myElement")))

In this example, we create an instance of WebDriverWait, specifying the maximum wait time as 10 seconds. We then use the `until()` method to wait until the element’s visibility with ID “myElement” is satisfied. Once the condition is met, the element is returned, allowing you to proceed with further actions.

Supported Expected Conditions

Selenium provides a set of expected conditions that can be used with Explicit Waits, such as visibility of an element, element to be clickable, presence of an element, and more.

You can use the expected conditions from the `expected_conditions` module in Selenium (`selenium.webdriver.support.expected_conditions`) with the WebDriverWait instance.

Explicit Waits are particularly useful when synchronizing your test scripts with specific conditions or element states. They provide a more fine-grained control over waiting and improve the stability and reliability of your test automation by ensuring that the interactions with web elements occur at the appropriate time.

Implicit Waits

In Selenium, an Implicit Wait is a type of Wait that sets a default timeout for the entire duration of the test script. It instructs the WebDriver to wait for a certain amount of time for an element to be available before throwing an exception. The Implicit Wait is applied globally to all the elements on the page, ensuring that Selenium Waits for a specified period before performing any action.

When an Implicit Wait is set, if the WebDriver cannot immediately find an element, it will wait for the specified time duration before throwing a NoSuchElementException. This helps handle scenarios where elements take some time to load or appear on the page.

Usage example:

Here’s an example of how to use an Implicit Wait in Selenium using Python:


from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)  # Wait for 10 seconds

driver.get("https://www.example.com")
element = driver.find_element_by_id("myElement")

In the example above, an Implicit Wait of 10 seconds is set using `implicitly_wait(10)`. This means that if the `find_element_by_id` method is unable to locate the element immediately, it will wait for a maximum of 10 seconds for the element to appear before throwing an exception.

It’s important to note that the Implicit Wait is set once for the entire lifetime of the WebDriver instance. If you want to change the timeout, you need to set it again.

Implicit Waits can be useful in scenarios where elements have unpredictable loading times or when dealing with slow or unreliable network conditions. However, it’s generally recommended to use Explicit Waits, which provide more precise control and are more suitable for handling synchronization in Selenium tests.

Fluent Waits

Fluent Wait in Selenium is a powerful feature that allows the Selenium WebDriver to wait for a specific condition, such as the visibility of a web element, within a defined time period. It intelligently checks for the condition at regular intervals until the element is found or the timeout occurs.

This wait mechanism is particularly useful when dealing with web elements that may take longer to load, commonly seen in Ajax applications. With Fluent Wait, you can set a default polling period as per your requirement and even configure it to ignore specific exceptions during the polling process.

Fluent Waits are often referred to as smart Waits because they don’t wait unnecessarily for the entire defined duration. As soon as the condition specified in the `.until(YourCondition)` method evaluates to true, the test execution proceeds without delay.

By leveraging Fluent Wait, you can enhance your test scripts to handle dynamic web elements and ensure efficient synchronization between the WebDriver and the application under test.

Usage example

Here’s an example that demonstrates the usage of FluentWait in Selenium:


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import FluentWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()
wait = FluentWait(driver, timeout=10, poll_frequency=0.5, ignored_exceptions=[TimeoutException])

driver.get("https://www.example.com")
element = wait.until(EC.visibility_of_element_located((By.ID, "myElement")))

In the example above, we create a FluentWait object by providing the WebDriver instance, the maximum timeout (in seconds), the polling frequency (how often to check for the condition), and any exceptions to ignore during the wait. We then use the wait.until() method and specify the expected condition, in this case, the visibility of an element with a specific ID.

The FluentWait will repeatedly check for the condition until it becomes true or until the maximum timeout is reached. It will wait for the specified polling frequency before each check. If the condition is not met within the specified timeout, a TimeoutException will be raised.

Why are Waits crucial in the process of software testing?

Waits play a crucial role in software testing, particularly in automated testing scenarios. Their significance lies in ensuring reliable and stable test automation by synchronizing the test execution with the application’s actual state. Here are the key reasons why Waits are important in software testing:

1. Handling dynamic web elements: Web applications often have elements that may appear or disappear dynamically, or they may take some time to load. Waits allow tests to pause until these elements are available, ensuring that the interaction occurs at the right time.

2. Synchronization with asynchronous operations: Many modern applications use asynchronous operations, such as AJAX requests or animations, which can impact the timing of element availability. Waits help testers synchronize their actions with these operations, preventing race conditions and timing-related failures.

3. Stabilizing test execution: Tests executed without appropriate Waits can encounter intermittent failures due to timing issues. By using Waits, tests can wait for elements or conditions to stabilize, reducing false-positive and false-negative results and improving the overall reliability of the test suite.

4. Handling delays in network or application response: Network latency or delays in application response can cause timing mismatches between the test script and the actual behavior of the application. Waits provide a mechanism to account for these delays, ensuring that tests wait for the expected behavior before proceeding.

5. Enhancing test robustness: Waits help make test scripts more robust by allowing them to adapt to varying system conditions. By incorporating appropriate Waits, tests can handle different execution environments, system loads, and response times without compromising the test results.

Comparing Implicit, Explicit, and Fluent Waits

Implicit WaitExplicit WaitFluent Wait
Usage
Used throughout the test scriptUsed when Waiting for a specific elementUsed when more control over Waits is needed
Behavior
Implicit Waits apply to all web elements without Explicit synchronizationExplicit Waits apply to specific elements or conditionsFluent Waits provide more control with custom polling intervals, timeout, and exception handling
Granularity
Implicit Waits have a single timeout applied globally to the entire script.Explicit Waits allow different conditions for specific casesFluent Waits allow setting individual timeouts and polling intervals for different situations
Exceptions
Implicit Waits ignore exceptions during waitingExplicit Waits throw exceptions if conditions are not metFluent Waits can be configured to ignore specific exceptions during waiting
Readability
Implicit Waits do not involve Explicit Waiting commands, making code shorter.Explicit Waits use Explicit Wait conditions for better clarityFluent Waits provide more readable and flexible code due to the ability to set custom timeouts and polling intervals

How to implement WebDriverWait in Selenium?

In Selenium with C#, you can use the WebDriverWait class to wait for specific conditions to be met before proceeding with further actions in your test scripts. Here’s an example of how to use WebDriverWait in Selenium with C#:

Import the necessary namespaces:


using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

Create an instance of WebDriverWait by passing the WebDriver instance and the maximum timeout duration (in seconds) to the constructor:


WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

Use the ‘Until’ method of WebDriverWait along with ExpectedConditions to wait for a specific condition to be true:


IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("myElement")));

In this example, we are waiting for the element with the ID “myElement” to become visible on the page. The ElementIsVisible method is an example of an ExpectedCondition that waits until the element is visible.

Once the condition is met, the Until method will return the element that satisfies the condition, allowing you to perform further actions on it.

By using WebDriverWait, you can ensure that your test scripts wait for specific conditions to be met before proceeding, improving synchronization with the application under test and enhancing the stability and reliability of your tests.

The framework leveraged in the example above is the Selenium WebDriver framework with the support of the OpenQA.Selenium and OpenQA.Selenium.Support.UI namespaces in C#. Selenium WebDriver is extensively used for automating web browsers, allowing developers to write tests in various programming languages, including C#. The WebDriverWait class is part of the Selenium WebDriver framework and provides functionality for waiting and synchronizing test scripts with specific conditions in the web application being tested.

WebDriverWait in Appium

In Appium, WebDriverWait is a class that allows you to apply explicit waits in your test scripts. WebDriverWait is used to wait for a specific condition to be true before proceeding with further actions in the test. This is especially useful when dealing with mobile apps, where elements may take some time to load or become interactable.

To use WebDriverWait in Appium, you first need to import the class from the Selenium WebDriver library. Then, you can create an instance of WebDriverWait and specify the maximum wait time and the expected condition you want to wait for. The WebDriver will repeatedly check for the condition until it becomes true or until the specified timeout is reached.

Here’s an example of how to use WebDriverWait in Appium with C#:


using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Support.UI;

class AppiumTest
{
    static void Main(string[] args)
    {
        // Set your Appium server URL and desired capabilities for the Android app
        var appiumOptions = new AppiumOptions();
        appiumOptions.AddAdditionalCapability("platformName", "Android");
        appiumOptions.AddAdditionalCapability("deviceName", "your_device_name");
        appiumOptions.AddAdditionalCapability("appPackage", "com.example.app");
        appiumOptions.AddAdditionalCapability("appActivity", ".MainActivity");

        // Create the AppiumDriver instance
        AppiumDriver<IWebElement> driver = new AndroidDriver<IWebElement>(new Uri("http://localhost:4723/wd/hub"), appiumOptions);

        // Set the maximum wait time (in seconds) for WebDriverWait
        int maxWaitTime = 30;
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(maxWaitTime));

        // Find an element by its ID
        By elementById = By.Id("element_id");

        // Use WebDriverWait to wait for the element to be clickable
        IWebElement clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(elementById));

        // Perform actions on the clickable element
        clickableElement.Click();

        // Quit the driver and close the session
        driver.Quit();
    }
}

HeadSpin’s integration capabilities with Selenium

HeadSpin’s data science driven Platform offers extensive integration capabilities with multiple automation frameworks. The HeadSpin Selenium integration extends the standard Selenium capabilities with custom features and controls to enhance the testing experience. It provides various capabilities for managing browser instances, enabling video and network capture, configuring network settings, and more. Users can set session names, descriptions, and tags, as well as add custom measurements to the test session. The platform also allows automatic labeling of commands and provides options for device control and behavior settings. With these extensive capabilities, testers can tailor their Selenium sessions to specific requirements and effectively manage the testing environment.

Bottom line

Waits in Selenium are essential tools for test automation that help ensure the stability, reliability, and accuracy of test scripts. They address the challenges of dynamic web pages, asynchronous operations, and varying loading times. By intelligently incorporating Explicit, Implicit, or Fluent Waits, testers can synchronize test execution with the application’s behavior, making the automation process more robust and effective.

When used appropriately, Waits enhance test stability, reduce flakiness, and improve the overall test automation process. They contribute to delivering high-quality software by ensuring that tests produce consistent and reliable results across various application scenarios. Understanding the differences between these wait strategies and applying them judiciously can significantly enhance the effectiveness of test automation efforts and help teams deliver better software products.

This article originally published on:

https://www.headspin.io/blog/using-webdriver-waits-in-selenium