Selenium Wait Strategies

In real - world browser operations, we naturally wait for the page to respond. Usually, this takes 1 - 3 seconds, but network or server issues may lead to a longer wait. When Selenium simulates real - world operations, the code execution speed is fast, which may cause problems. To improve the stability and robustness of automated scripts, we need to add waiting mechanisms at key points.

Selenium supports three waiting strategies: Hard Wait, Implicit Wait, and Explicit Wait. Among them, explicit and implicit waits are provided by WebDriver, while hard wait relies on Python's time module.

1.1 Hard Wait

Hard wait is implemented through time.sleep(), which pauses the current process for a specified period.

Function:

  • Reduce the execution speed, facilitate the observation of operation effects, and ensure the stable operation of the script.

Advantages:

  • Simple to use, can be added as needed, and only affects the execution time of subsequent statements without considering complex logic.

Disadvantages:

  • The waiting time is fixed and cannot adapt to complex environments. If an element is loaded in 2 seconds but a 30 - second wait is set, it will waste the script execution time.

The time unit is second (s).

from selenium import webdriver
from time import sleep

# Configure Chrome options
chrome_options = webdriver.ChromeOptions()
# Specify the driver path (example path, need to be modified according to the actual situation)
driver_path = "D:\\drivers\\chromedriver.exe"
# Initialize the browser driver
driver = webdriver.Chrome(executable_path = driver_path, options = chrome_options)

# Open the Baidu page
driver.get("https://www.baidu.com")
# Hard wait for 3 seconds
sleep(3)
# Locate the search box and enter content
driver.find_element_by_id("kw").send_keys("selenium")

# Close the browser
driver.quit()

1.2 Implicit Wait

WebDriver provides the implicitly_wait() method to implement implicit wait, and the default waiting time is 0 seconds.

Function:

  • Provide buffer time when finding or operating elements. It only needs to be set once when the browser starts, and it will take effect for all subsequent element operations.

Advantages:

  • It takes effect globally and only needs to be set once. The waiting time is flexible. If the element is loaded in advance, the script will continue to execute immediately.
  • Throws NoSuchElementException after timeout.

Disadvantages:

  • It is impossible to set waiting for a specific element, and the same waiting time is used for all element operations.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from time import ctime

# Initialize the driver
driver = webdriver.Chrome("D:\\chromedriver.exe")
# Set implicit wait for 10 seconds
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")

try:
    print(ctime())
    # Try to locate a non - existent element to trigger the waiting mechanism
    driver.find_element_by_id("kw22").send_keys("selenium")
except NoSuchElementException as e:
    print(f"Element not found: {e}")
finally:
    print(ctime())
    driver.quit()

The working principle of implicit wait: When the script executes element location, if the element is not found immediately, WebDriver will continuously check in a polling manner until the element appears or times out. For example, if the element is loaded in the 6th second, the script will continue to execute immediately; if it times out (for example, the element is still not found after 10 seconds), an exception will be thrown.

1.3 Explicit Wait

Explicit wait sets waiting conditions for a specific element. It periodically checks whether the element meets the conditions within a specified time. If the conditions are met before the timeout, the script continues to execute; otherwise, a TimeoutException is thrown.

Function:

  • Flexibly control the waiting time of a single element to adapt to elements with network fluctuations or slow loading.

Advantages:

  • Strong pertinence. The maximum waiting time and check frequency can be set to avoid meaningless waiting.

Disadvantages:

  • The implementation steps are relatively cumbersome, and it is necessary to select an appropriate expected condition method.
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
from datetime import datetime
from time import sleep

# Initialize the driver
driver = webdriver.Chrome("D:\\chromedriver.exe")
driver.get("https://www.baidu.com")

print(datetime.now())
try:
    # Explicit wait: wait for a maximum of 5 seconds, check every 0.5 seconds whether the element exists
    element = WebDriverWait(driver, 5, 0.5).until(
        EC.presence_of_element_located((By.ID, "kj"))
    )
    element.send_keys("selenium")
except Exception as e:
    print(f"Timeout or error: {e}")

sleep(5)
driver.quit()

The WebDriverWait class is provided by WebDriver and is used to periodically check the element state within a specified time. Its construction parameters are as follows:

WebDriverWait(driver, timeout, poll_frequency = 0.5, ignored_exceptions = None)
  • driver: Browser driver object.
  • timeout: Maximum waiting time (in seconds).
  • poll_frequency: Check interval (default is 0.5 seconds).
  • ignored_exceptions: Exception thrown after timeout (default is NoSuchElementException).

WebDriverWait is usually used with the until() or until_not() method:

  • until(method, message = ''): Stop waiting when method returns True.
  • until_not(method, message = ''): Stop waiting when method returns False.

The expected_conditions class provides a variety of expected condition judgment methods. The commonly used methods are as follows:

Method Description
element_to_be_clickable() Whether the element is clickable
element_to_be_selected() Whether the element is selected (such as in a drop - down list)
presence_of_element_located() Whether the element exists in the DOM tree (does not guarantee visibility)
visibility_of_element_located() Whether the element is visible (not hidden and width and height > 0)
title_is() Whether the page title exactly matches
url_to_be() Whether the current URL matches the specified address
frame_to_be_available_and_switch_to_it() Switch to the specified iframe

Tags: Selenium python Web Automation Wait Strategies Implicit Wait

Posted on Sun, 20 Sep 2026 16:45:05 +0000 by webster08