Explicit waits in Selenium Python - GeeksforGeeks

19 May, 2020
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 Explicit wait in Selenium Python.
Explicit WaitsAn explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. Explicit waits are achieved by using webdriverWait class in combination with expected_conditions. Let’s consider an example –
# import necessary classesfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC  # create driver object driver = webdriver.Firefox()  # A URL that delays loadingdriver.get("http://somedomain / url_that_delays_loading")  try:    # wait 10 seconds before looking for element    element = WebDriverWait(driver, 10).until(        EC.presence_of_element_located((By.ID, "myDynamicElement"))    )finally:    # else quit    driver.quit()
This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.Expected Conditions –There are some common conditions that are frequently of use when automating web browsers. For example, presence_of_element_located, title_is, ad so on. one can check entire methods from here – Convenience Methods. Some of them are –
title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present
Explicit wait as defined would be the combination of WebDriverWait and Expected conditions. Let’s implement this on https://www.geeksforgeeks.org/ and wait 10 seconds before locating an element.
# import webdriver from selenium import webdriver from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC  # create webdriver object driver = webdriver.Firefox()     # get geeksforgeeks.org driver.get("https://www.geeksforgeeks.org/")     # get element  after explicitly waiting for 10 secondselement = WebDriverWait(driver, 10).until(        EC.presence_of_element_located((By.link_text, "Courses"))    )# click the element element.click() 
Output –First it opens https://www.geeksforgeeks.org/ and then finds Courses link
It clicks on courses links and is redirected to https://practice.geeksforgeeks.org/
Python-selenium
selenium
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
Reading and Writing to text files in Python
*args and **kwargs in Python
Create a Pandas DataFrame from Lists
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?