Open In App

Selenium Waits

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is one of the most popular and powerful tools when it comes to automating web applications. Selenium is widely used for automating user interaction on a web page. One of the crucial aspects of automating web applications in Selenium is handling Dynamic Web applications and ensuring proper synchronization between the testing scripts and loading time of the website, sometimes the element we want to interact with is not available for interaction which may lead to faulty test cases.

What is Selenium Waits?

Dynamic Web Applications often load asynchronously making it difficult to predict whether a particular element we want to interact with is available for interaction or not.

  1. Selenium Waits is a mechanism that helps to address this issue, it instructs the automation script to wait for a certain condition to be met before we move on to the next stage.
  2. Without proper synchronization, tests may fail due to the element not being available or ready when we are trying to interact with it.

Why are Selenium Waits Important?

Selenium waits are important in automating web applications to handle the timing issues and ensure that our testing scripts are interacting with the element accurately. They help in addressing the delay in element availability, page loading and rendering making out testing script more reliable.

Example:

waits1

Selenium Waits

Here when we click on the create image button it takes some time to load the image, here if we use selenium click on the create image button and locate the image it will not be able to locate the image because the automation script will try to locate the image immediately and when it isn’t available for interaction.

Python3




from selenium import webdriver
from selenium.webdriver.common.by import By
 
driver=webdriver.Chrome()
driver.get(url)
 
try:
    button=driver.find_element(By.ID,"createImage")
    button.click()
    img=driver.find_element(By.CSS_SELECTOR,"#output img")
except:
    print("Not found")
else:
    print("found")


Output:

waits2

Selenium Waits

Explanation:

  1. Here we have used find_element() method along with ID locating strategy to find the create image button which has the Id of “createImage” and click on it.
  2. Then we have used the find_element() method along with CSS_SLECTOR locating strategy to locate the image tag which is inside a div having an id of output.
  3. But the scripts fail to locate the image because the image was not available for interaction that moments, in this the image takes few seconds after the button click to load so it was not available for interaction that moment.
  4. So the output was Not Found

To know more about Locating Strategies in Selenium and How to locate an element on a webpage refer Selenium Locating Strategies

So, in order to synchronize our automation script with the loading time of the element we’ll have to use Selenium Waits.

Types of Selenium Waits

Selenium Waits are the mechanism that allows the automation script to pause the execution and wait until certain conditions are met before throwing an error if the element is not found. Selenium Waits helps to synchronize the execution script with the loading time of the website. There are two types of Waits In Selenium they are

  1. Implicit Waits: Implicit Waits is a type of wait which instruct the Web Driver to wait for specific amount of time before throwing an error. It is applied globally.
  2. Explicit Waits: Explicit wait is a type of wait which instructs the Web Driver to wait until a certain condition is met or maximum type is elapsed.

How to use Selenium Waits?

1. Implicit Waits

Implicit Wait is a type of Wait in Selenium which instructs the Web Driver to wait for a certain amount of time before throwing a exception if an element is not found. It is applied globally to each, and every element location calls for the entire session. If the implicit wait is set for 10 seconds, the Web Driver will wait for 10 second before throwing an error and during that duration as soon as the element is found it will return the elements reference.

Syntax:

driver.implicitly_wait(time_in_seconds)

#replace time_in_second with the integer value of seconds.

Example:

Python3




from selenium import webdriver
from selenium.webdriver.common.by import By
 
driver=webdriver.Chrome()
driver.get(url)
 
#implicit wait of 5 seconds
driver.implicitly_wait(5)
try:
    button=driver.find_element(By.ID,"createImage")
    button.click()
    img=driver.find_element(By.CSS_SELECTOR,"#output img")
except:
    print("Not found")
else:
    print("found")


Output:

waits3

Selenium Waits

Explanation:

Here we have set the implicit wait to 5 seconds so now before throwing an error Selenium Web Driver will wait for 5 seconds during which the image gets rendered and returns its reference, so the output is “found”.

2. Explicit Waits

Explicit wait is a type of wait in Selenium which instructs the Web Driver to wait until a certain condition is met or maximum time is elapsed. Unlike Implicit Waits, Explicit waits are not applied globally they are more specific and allows the Web Driver to wait for a certain condition for a particular element before throwing an error.

Python3




from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
driver=webdriver.Chrome()
driver.get(url)
wait=WebDriverWait(driver,5)
try:
    button=driver.find_element(By.ID,"createImage")
    button.click()
    img = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#output img")))
except:
    print("Not found")
else:
    print("found")


Output:

waits4

Selenium Waits

Explanation:

  • WebDriverWait(driver,timeout): WebDriverWait is used to set the maximum timeout for our explicit wait.
  • wait.until()- wait.until() method is used with EC.some_condition which wait for the condition to meet for specified duration of time.
  • EC.presence_of_element_located: used to check the presence of an element in the dom.

Best Practices for Using Selenium Waits

Here are some best practices for using Selenium Waits which will help you in creating stable and reliable automation scripts.

  1. Using Explicit Waits Over Implicit Waits: Whenever possible try to use explicit waits over implicit waits, explicit waits provide more control and are more specific and avoids unnecessary waits for all elements.
  2. Choosing the right wait condition: Select the wait condition that is best suited for your testing scenario, Selenium provides variety of expected condition like element_to_be_clickable , presence_of_element_located and many more.
  3. Adjust the wait time appropriately: Set the wait time based on the web application behavior that are long enough to ensure reliability but not too long.
  4. Using Explicit Waits for Specific Element: Use explicit waits for crucial element to wait for their presence, visibility and are interactive.
  5. Handle Expected Exceptions: Make sure to handle common exceptions like TimeoutException (which occurs when the element is not found, or the condition is not met within the specified time) and StateElementReferenceException (which occurs when the previously located element on a webpage becomes no longer available or attached to the DOM).
  6. Prioritize Key user Actions: Make sure to focus your waits on the condition that are directly related to the most critical user interactions.
  7. Regular Review and Update Waits: Make sure to regularly review and update the waits as your application evolves to ensure they remain effective.

Conclusion

Selenium waits plays an important in handle the dynamic behaviour of the website. Dynamic website often loads at different speeds some elements load immediately which some takes some time due to network latency, server response times and if an automation script tries to interact with an element which is not currently load it may lead to unreliable results. By incorporating the implicit and explicit waits we can ensure the robustness of our automation script making them resilient to loading times of different elements and dynamic content changes.



Similar Reads

Selenium Wait Commands - Implicit Waits and Explicit Waits
Selenium Python is one of the great tools for testing automation. These days most of the 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 El
3 min read
Implicit Waits in Selenium Python
Selenium Python is one of the great tools for testing automation. These days most of the 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 El
2 min read
Explicit waits in Selenium Python
Selenium Python is one of the great tools for testing automation. These days most of the 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 El
3 min read
How to use Selenium and Selenium WebDriver Manager to Login to a Website with Python?
Selenium is an open-source tool that is used for automating the tests carried out on different web browsers. Selenium WebDriver is a web framework that allows executing cross-browsing testing. This article focuses on discussing how to log in to a website with Python using Selenium and Selenium WbDriver Manager. Table of Content What is Selenium?Wha
5 min read
What is the Difference Between Selenium Core Extensions and Selenium IDE Extensions?
Selenium is a popular tool for testing and automating web applications. It has changed over time and has different parts that help users in different ways. The two important parts are Selenium Core (from the past) and Selenium IDE. Both have extensions that add additional features to the tool. This article looks at the differences between Selenium
8 min read
Selenium vs Cypress - Which Framework is better to learn Selenium or Cypress?
This article focuses on discussing the key considerations when choosing between Cypress and Selenium for Automation testing. We'll compare their features, benefits, and limitations, helping one determine which framework aligns best with the testing requirements and objectives. Table of Content What is Selenium?Advantages of SeleniumDisadvantages of
5 min read
What is the main difference between Selenium 1 and Selenium 2?
Selenium is a suite of software widely used in the field of automation and testing, it has been a key player in the field of automation for a long time. This article will deep into Automation and discuss the difference between Selenium version 1 and Selenium version 2, highlighting their differences, advantages, and disadvantages. Whether the user
10 min read
Differences Between Selenium Standalone server and Selenium Client and WebDriver Server
In web application testing, Selenium has emerged as one of the most basic foundations, providing robust mechanisms for automating browsers. Among its suite, two elements stand out, which are: 'Selenium-server-standalone.jar' and 'Selenium Client and WebDriver'. Both 'Selenium-server-standalone.jar' and 'Selenium Client and WebDriver' are very impor
4 min read
Selenium Python Tricks
Selenium: Selenium Python bindings provide a convenient API to access Selenium Web Driver like Firefox, Chrome, etc. What is webdriver? Selenium WebDriver is an automation testing tool. When I say automation, it means it automates test scripts written in Selenium. Webdriver Install Chrome: https://sites.google.com/a/chromium.org/chromedriver/downlo
3 min read
Python | SMS Bomber using Selenium
Here, we are going to learn a simple SMS bomber trick (for fun and educational purpose). Selenium is a free tool for automated testing across different browsers. In this tutorial, we will learn to send automatically number of spam SMS for given number of frequency and interval.Requirement: You need to install chromedriver and set path. Click here t
2 min read