Selenium Webdriver - Explicit and Implicit Wait

Let us understand what an explicit wait in the Selenium Webdriver is.
An explicit wait is applied to instruct the webdriver to wait for a specific condition before moving to the other steps in the automation script.
Explicit wait is implemented using the WebDriverWait class along with expected_conditions. The expected_conditions class has a group of pre-built conditions to be used along with the WebDriverWait class.
The pre-built conditions which are to be used along with the WebDriverWait class are given below −
alert_is_present
alert_is_present
element_selection_state_to_be
element_selection_state_to_be
presence_of_all_elements_located
presence_of_all_elements_located
element_located_to_be_selected
element_located_to_be_selected
text_to_be_present_in_element
text_to_be_present_in_element
text_to_be_present_in_element_value
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
frame_to_be_available_and_switch_to_it
element_located_to_be_selected
element_located_to_be_selected
visibility_of_element_located
visibility_of_element_located
presence_of_element_located
presence_of_element_located
title_is
title_is
title_contains
title_contains
visibility_of
visibility_of
staleness_of
staleness_of
element_to_be_clickable
element_to_be_clickable
invisibility_of_element_located
invisibility_of_element_located
element_to_be_selected
element_to_be_selected
Let us wait for the text - Team @ Tutorials Point which becomes available on clicking the link - Team on the page.
On clicking the Team link, the text Team @ Tutorials Point appears.
The code implementation for the explicit wait is as follows −
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify element
l = driver.find_element_by_link_text('Team')
l.click()
#expected condition for explicit wait
w = WebDriverWait(driver, 5)
w.until(EC.presence_of_element_located((By.TAG_NAME, 'h1')))
s = driver.find_element_by_tag_name('h1')
#obtain text
t = s.text
print('Text is: ' + t)
#driver quit
driver.quit()
The output is mentioned below −
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the text (obtained from the text method) - Team @ Tutorials Point gets printed in the console.
An implicit wait is applied to instruct the webdriver for polling the DOM (Document Object Model) for a specific amount of time while making an attempt to identify an element which is currently unavailable.
The default value of the implicit wait time is 0. Once a wait time is set, it remains applicable through the entire life of the webdriver object. If an implicit wait is not set and an element is still not present in DOM, an exception is thrown.
The syntax for the implicit wait is as follows −
driver.implicitly_wait(5)

Here, a wait time of five seconds is applied to the webdriver object.
The code implementation for the implicit wait is as follows −
from selenium import webdriver
#set path of chromedriver.exe
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait of 0.5s
driver.implicitly_wait(0.5)
#url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify link with link text
l = driver.find_element_by_link_text('FAQ')
#perform click
l.click()
print('Page navigated after click: ' + driver.title)
#driver quit
driver.quit()
The output is mentioned below −
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. On clicking on the FAQ link, the webdriver waits for 0.5 seconds and then moves to the next step. Also, the title of the next page(obtained from the driver.title method) - Frequently Asked Questions - Tutorialspoint gets printed in the console.

    46 Lectures 
     5.5 hours 

 Aditya Dua

    296 Lectures 
     146 hours 

 Arun Motoori

    411 Lectures 
     38.5 hours 

 In28Minutes Official

    22 Lectures 
     7 hours 

 Arun Motoori

    118 Lectures 
     17 hours 

 Arun Motoori

    278 Lectures 
     38.5 hours 

 Lets Kode It
 Print
 Add Notes
 Bookmark this page