Selenium Wait Commands – Implicit, Explicit, and Fluent Wait
Last Updated :
05 Jun, 2024
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 –
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.
Implicit waits in Selenium PythonExplicit 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? – GeeksforGeeks
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.
Similar Reads
Automation Testing - Software Testing
Automated Testing means using special software for tasks that people usually do when checking and testing a software product. Nowadays, many software projects use automation testing from start to end, especially in agile and DevOps methods. This means the engineering team runs tests automatically wi
15+ min read
Automation Testing Roadmap: A Complete Guide to Automation Testing [2025]
Test automation has become a vital aspect of the Software Development Life Cycle (SDLC), aimed at reducing the need for manual effort in routine and repetitive tasks. Although manual testing is crucial for ensuring the quality of a software product, test automation plays a significant role as well.
10 min read
How to Start Automation Testing from Scratch?
Automation Testing is the practice of using automated tools and scripts to execute tests on software applications, reducing manual effort and increasing efficiency. Starting automation testing from scratch involves several key steps, including selecting the right automation tool, identifying test ca
8 min read
Benefits of Automation Testing
In today's fast-paced software development landscape, integrating automation into development and testing processes is crucial for staying competitive. Automation testing offers numerous benefits, including cost savings, faster feedback loops, better resource allocation, higher accuracy, increased t
4 min read
Stages of Automation Testing Life Cycle
In this article, we will explore the phases and methodologies involved in automation testing and the phases of the automation testing lifecycle. We'll cover everything from scoping your test automation to creating a solid test plan and strategy. You'll also learn about setting up the perfect test en
12 min read
Top Automation Testing Books For 2024
In this article, we can explore the top 10 books for automation testing, providing a top-level view of each book's content material and why it's worth considering for everybody interested in this sector. Table of Content Top 10 Books for Automation Testing BooksConclusionFAQs on Top Automation Test
12 min read
Top Test Automation mistakes and Tips for QA teams to avoid them
In the dynamic landscape of software testing, avoiding common test automation pitfalls is crucial for QA teams aiming for efficient and successful testing processes. This article delves into prevalent errors in test automation and provides valuable insights on how QA teams can steer clear of these m
7 min read
Essential Skills for a Successful Automation Tester
In the domain of software testing, automation plays a crucial role in ensuring efficiency, accuracy, and speed. However, to be a successful automation tester, one must possess a specific set of skills beyond just technical proficiency. This article explores the essential skills required for automati
6 min read
Steps to Select the Right Test Automation tools
Selecting the right test automation tools is critical for ensuring efficient and effective testing processes in software development projects. In this article, we will discuss the key steps involved in choosing the most suitable automation tools for your project needs. From understanding project req
5 min read
Best Test Automation Practices in 2024
Test Automation continues to evolve with new technologies and methodologies emerging each year. In 2024, staying updated with the latest best practices is crucial for efficient and effective testing processes. From robust test design to continuous integration and deployment, this article explores th
7 min read