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.
Fluent waits in 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).
wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[NoSuchElementException]) element = wait.until(EC.visibility_of_element_located((By.ID, 'submit_button')))
wait = WebDriverWait(driver, 5, poll_frequency=0.2) element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.login')))
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.
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()
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.
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.