selenium
  1. selenium-seleniumwaits

Selenium Waits

Selenium Waits are commands used in Selenium WebDriver to pause the execution of the script for a certain amount of time or until certain conditions are met before proceeding to the next step. It is crucial to use Selenium Waits when building automation scripts to ensure that the application under test is fully loaded, and the application elements are visible on the web page before the script interacts with them.

Syntax

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

WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.ID, "myElement")))

Example

Below is an example of using Selenium Waits in Python:

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 driver instance
driver = webdriver.Chrome()

# Navigate to the web page
driver.get("http://www.example.com")

# Wait for the page to fully load before interacting with the elements
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "username")))

# Locate the username input field and enter a value
username_field = driver.find_element(By.NAME, "username")
username_field.send_keys("exampleuser")

# Locate the password input field and enter a value
password_field = driver.find_element(By.NAME, "password")
password_field.send_keys("examplepassword")

# Click on the login button
login_button = driver.find_element(By.XPATH, "//button[text()='Login']")
login_button.click()

# Close the driver instance and exit the script
driver.quit()

Explanation

In this example, Selenium Waits are used to wait for the username input field to be present on the web page before interacting with it. This is achieved by using the WebDriverWait class, which takes two arguments: the driver object and the timeout duration (in seconds) to wait for the element to be present. The until() method is used to specify the expected condition for the wait time, which in this case is the presence of the element located by its NAME attribute with a value of username.

Use

Selenium Waits are used in automation testing to ensure that the web page and its elements are fully loaded before interacting with them. The most commonly used Selenium Waits include:

  • Implicit Waits: pausing the script for a fixed duration of time before proceeding to the next step. Implicit waits are set at the beginning of the script and apply globally to all elements.

  • Explicit Waits: pausing the script until a certain condition is met before proceeding to the next step. Explicit waits are used when waiting for specific elements, such as buttons or input fields, with unique locators.

  • Expected Conditions: pre-defined conditions used in explicit waits to check if a certain element is present, visible, or enabled on the web page.

  • Fluent Waits: a flexible and customizable wait type that polls the web page at regular intervals until the expected condition is met, or the timeout duration is reached.

Important Points

  • Always use Selenium Waits when building automation scripts to avoid interacting with elements before the web page is fully loaded.

  • Use Implicit Waits for a fixed wait time, but be aware that this may lead to longer test times.

  • Use Explicit Waits with expected conditions for more specific waits, but ensure that they are only applied to specific elements.

  • Use Fluent Waits to set custom wait times and intervals, but be careful to set an appropriate timeout duration to avoid long test times.

Summary

Selenium Waits are an essential part of writing robust automation scripts in Selenium. They allow the script to wait until the web page and its elements are fully loaded before interacting with them, preventing run-time errors and improving test reliability. There are various types of Selenium Waits available, including implicit waits, explicit waits, expected conditions, and fluent waits, each with its own use cases and benefits.

Published on: