Open In App

Selenium Waits

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:



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.




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:

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:




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:

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.




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:

Selenium Waits

Explanation:

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.


Article Tags :