How to Use the Python Selenium wait() Function

09/12/2021

Contents

In this article, you will learn how to use the Python selenium wait() function.

Python Selenium wait() Function

The WebDriverWait class in the Selenium Python bindings provides a convenient way to wait for a page element to be in a specific state before proceeding with the script. The until() method is used in conjunction with the WebDriverWait class to define the condition that must be met before the wait is considered fulfilled and the script continues execution.

Here’s an example of how to use the WebDriverWait class in Python to wait for an element to be clickable:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# create a new Firefox driver
driver = webdriver.Firefox()

# navigate to a website
driver.get("http://www.google.com")

# wait for the search button to be clickable
search_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "btnK")))

# click the search button
search_button.click()

# close the browser
driver.quit()

In this example, the WebDriverWait class is used to wait for the search button element to be clickable, with a timeout of 10 seconds. The element_to_be_clickable condition from the expected_conditions module is used to define the expected state of the element. If the condition is not met within the specified timeout, a TimeoutException will be raised.

You can also specify multiple conditions in the until() method. For example, you can wait for an element to be present and visible:

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "element_id")) &
    EC.visibility_of_element_located((By.ID, "element_id"))
)

In this example, the script will wait for the element with the ID “element_id” to be both present in the DOM and visible on the page before proceeding with the script.

The WebDriverWait class allows you to wait for a specific condition to be met before proceeding with the script. This is useful when the state of a page element is dynamic and can change, for example, due to AJAX requests or animations.

The until() method of the WebDriverWait class takes a single argument, which is a callable that returns a Boolean value indicating whether the condition has been met. The callable is repeatedly executed until it returns True or the timeout specified in the constructor of the WebDriverWait class is exceeded.

In the example I provided, the element_to_be_clickable condition from the expected_conditions module is used. This condition returns True if the specified element is present in the DOM and visible on the page, and can be interacted with, i.e. it’s not disabled or covered by another element.

There are many other conditions available in the expected_conditions module that you can use to wait for specific states of page elements. Here are a few of them:

  • title_is: returns True if the title of the page is equal to the expected value.
  • title_contains: returns True if the title of the page contains the expected value.
  • presence_of_element_located: returns True if the specified element is present in the DOM.
  • visibility_of_element_located: returns True if the specified element is present in the DOM and visible on the page.
  • element_to_be_selected: returns True if the specified element is selected.
  • element_to_be_clickable: returns True if the specified element is present in the DOM, visible on the page, and can be interacted with.

You can also define custom conditions by passing a callable to the until() method. For example, you can wait for an element to have a specific attribute value:

def element_has_attribute(driver, attribute, value):
    element = driver.find_element_by_id("element_id")
    return element.get_attribute(attribute) == value

element = WebDriverWait(driver, 10).until(
    element_has_attribute(driver, "data-attribute", "expected_value")
)

In this example, the element_has_attribute function is used to wait for the element with the ID “element_id” to have a data-attribute equal to “expected_value”. The function returns True if the condition is met and False otherwise. The until() method is called with this function as the argument, and the script will wait until the function returns True or the timeout is exceeded.