0
0
Selenium Pythontesting~5 mins

Fluent waits in Selenium Python

Choose your learning style9 modes available
Introduction

Fluent waits help your test wait patiently for something to happen on a web page. They check often and stop waiting as soon as the thing appears.

When a web element takes time to load but you don't know exactly how long.
When you want to check for an element repeatedly until it shows up or a timeout happens.
When the page content changes dynamically and you want to wait for a specific condition.
When you want to avoid waiting the full timeout if the element appears early.
When you want to ignore certain errors while waiting, like element not found.
Syntax
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

wait = WebDriverWait(driver, timeout, poll_frequency=interval, ignored_exceptions=[NoSuchElementException])
element = wait.until(EC.presence_of_element_located((By.ID, 'element_id')))

timeout is how long to wait in seconds before giving up.

poll_frequency is how often to check for the element (default is 0.5 seconds).

Examples
Waits up to 10 seconds, checking every 1 second, ignoring 'NoSuchElementException', until the submit button is visible.
Selenium Python
wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[NoSuchElementException])
element = wait.until(EC.visibility_of_element_located((By.ID, 'submit_button')))
Waits up to 5 seconds, checking every 0.2 seconds, until the login button is clickable.
Selenium Python
wait = WebDriverWait(driver, 5, poll_frequency=0.2)
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.login')))
Sample Program

This script opens a browser to example.com and waits up to 10 seconds for an element with ID 'delayed_element' to appear. It checks every 0.5 seconds and ignores errors if the element is not found yet. If found, it prints the element's text. Otherwise, it prints a timeout message.

Selenium 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
from selenium.common.exceptions import NoSuchElementException

# Setup driver (make sure chromedriver is in PATH)
driver = webdriver.Chrome()
driver.get('https://example.com')

# Fluent wait: wait up to 10 seconds, check every 0.5 seconds, ignore NoSuchElementException
wait = WebDriverWait(driver, 10, poll_frequency=0.5, ignored_exceptions=[NoSuchElementException])

try:
    element = wait.until(EC.presence_of_element_located((By.ID, 'delayed_element')))
    print('Element found:', element.text)
except Exception as e:
    print('Element not found within time')

# Close the browser
driver.quit()
OutputSuccess
Important Notes

Fluent waits are better than fixed waits because they stop waiting as soon as the element appears.

Always use specific locators like ID or CSS selectors for better performance.

Ignoring exceptions helps avoid test failures while waiting.

Summary

Fluent waits repeatedly check for an element until it appears or timeout.

They let you set how often to check and which errors to ignore.

Use them to make your tests more reliable and faster.