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()
url="http://127.0.0.1:5500/index.html"
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.

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()
url="http://127.0.0.1:5500/index.html"
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:

  • 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.

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.

Similar Reads

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....

Types of Selenium Waits

...

How to use 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...

Best Practices for Using Selenium Waits

1. Implicit Waits...

Conclusion

...