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 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 two types of waits – implicit & explicit. This article revolves around Implicit waits in Selenium Python.

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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

# import webdriver
from selenium import webdriver
  
driver = webdriver.Firefox()
  
# set implicit wait time
driver.implicitly_wait(10) # seconds
  
# get url
driver.get("http://somedomain / url_that_delays_loading")
  
# get element after 10 seconds
myDynamicElement = driver.find_element_by_id("myDynamicElement")

chevron_right


This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds.

How to create an Implicit wait in Selenium Python ?

Implicit wait as defined would be the set using implicitly_wait method of driver. Let’s implement this on https://www.geeksforgeeks.org/ and wait 10 seconds before locating an element.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import webdriver 
from selenium import webdriver 
  
# create webdriver object 
driver = webdriver.Firefox() 
    
# set implicit wait time
driver.implicitly_wait(10) # seconds
  
# get geeksforgeeks.org 
    
# get element after 10 seconds
element = driver.find_element_by_link_text("Courses")
  
# click element
element.click()
  

chevron_right


Output –
First it opens https://www.geeksforgeeks.org/ and then finds Courses link
driver-methods-Selenium-Python

It clicks on courses links and is redirected to https://practice.geeksforgeeks.org/

action-chains-selenium-Python




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.