Selenium Wait Commands – Implicit, Explicit, and Fluent Wait

Selenium Python is one of the great tools for testing automation. These days most web apps are using AJAX techniques. When a page is loaded by the browser, the elements within that page may load at different time intervals. This makes locating elements difficult: if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleException exception. Using waits, we can solve this issue. Waiting provides some slack between actions performed – mostly locating an element or any other operation with the element.

Selenium WebDriver provides Three types of waits –

Table of Content

  • Implicit Waits
  • Explicit Waits
  • Fluent Waits

Implicit Waits

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object. Let’s consider an example.

Python
# import webdriver
from selenium import webdriver

driver = webdriver.Firefox()

# set implicit wait time
driver.implicitly_wait(10) # seconds

# get url (Replace sample URL with your URL )
driver.get("http://somedomain / url_that_delays_loading")

# get element after 10 seconds ( Replace myDynamicElement elememnt with your elememnt id )
myDynamicElement = driver.find_element_by_id("myDynamicElement")

This waits up to 10 seconds before throwing a Timeout Exception unless it finds the element to return within 10 seconds. To check out how to practically implement Implicit Waits in WebDriver, checkout.

Explicit Waits

An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. Explicit waits are achieved by using webdriverWait class in combination with expected_conditions. Let’s consider an example –

Python
# import necessary classes
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# create driver object 
driver = webdriver.Firefox()

# A URL that delays loading (Replace sample URL with your URL )
driver.get("http://somedomain / url_that_delays_loading")

try:
    # wait 10 seconds before looking for element
    # ( Replace myDynamicElement elememnt with your elememnt id )
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    # else quit
    driver.quit()

This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.

Expected Conditions

There are some common conditions that are frequently of use when automating web browsers. For example, presence_of_element_located, title_is, ad so on.

One can check entire methods from here – Convenience Methods.

Some of them are –

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • element_located_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present

To check out how to practically implement Implicit Waits in WebDriver, checkout Explicit waits in Selenium Python

Fluent Waits

Fluent Wait is one of the mechanisms available at Selenium WebDriver that permits testers to set the maximal time for a condition to be accomplished and the frequency of their attempts to check the condition. Unlike such waits as Implicit Wait or Explicit Wait, Fluent Wait offers more flexibility since programmers can change the wait conditions according to the complications and situations during the runtime.

  • Fluent wait is an explicit wait in Selenium that pings the web driver to wait for a circumstance and how regularly to check that scenario before returning an “ElementNotVisibleException” exception.
  • To better understand, a fluent wait is also called a smart wait because it does not “wait” out the whole predetermined time as coded.
  • Rather, the test proceeds to run as soon as the element is visible.
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

# Create driver object
driver = webdriver.Firefox()

# Navigate to a URL that delays loading (Replace sample URL with your URL )
driver.get("http://somedomain/url_that_delays_loading")

try:
    # Fluent wait for the element to be present
    # ( Replace myDynamicElement elememnt with your elememnt id )
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    # Quit the driver
    driver.quit()
  • Fluent Wait provides a built-in timeout handler that allows you to set the maximum time to wait for a condition to be fulfilled. If the condition is not met within the specified time, Fluent Wait throws a ‘TimeoutException’, which allows you to gracefully handle timeout situations in your automation scripts.
  • Fluent Wait in Selenium WebDriver provides a flexible and robust solution for handling dynamic web elements and asynchronous behavior in test automation.
  • Fluent Wait improves the reliability and stability of automation scripts by allowing testers to set maximum wait times, polling intervals, and custom conditions.
  • Its dynamic nature and exception handling capabilities make it a valuable tool for ensuring accurate and efficient testing of web applications.

To check out how to practically implement Fluent Wait in WebDriver, checkout What is Fluent Wait in Selenium? – w3wiki

Conclusion

Selenium Python provides flexible waiting mechanisms like Implicit, Explicit, and Fluent Waits to handle dynamic web elements effectively and easily, improving test reliability and stability for the automation testing. These waits help to manage timing issues, with checking the elements are located and interacted with as intended in asynchronous web environments.

Frequently Asked Questions on Selenium Wait Commands – Implicit, Explicit, and Fluent Wait

How to apply explicit wait in POM?

“ExpectedConditions” and “WebDriverWait” are required to be used.

How to wait for 5 seconds in Selenium?

Syntax: WebDriverWait wait = new WebDriverWait(driver,Duration. ofSeconds(seconds));  seconds will be change in the number of seconds the user want Selenium webdriver to wait.

Is implicit wait static or dynamic?

It is a dynamic wait.