Explain explicit wait in Selenium webdriver in Python.

Rest AssuredPythonDynamic Programming

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.

  • alert_is_present
  • element_selection_state_to_be
  • presence_of_all_elements_located
  • element_located_to_be_selected
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • element_located_to_be_selected
  • visibility_of_element_located
  • presence_of_element_located
  • title_is
  • title_contains
  • visibility_of
  • staleness_of
  • element_to_be_clickable
  • invisibility_of_element_located
  • 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.

Example

Code Implementation

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()

Output

raja
Updated on 19-Nov-2021 11:32:24

Advertisements