Waits, Actions & Frames — Killing Flakiness at the Source
Flaky tests — the ones that pass, then fail, then pass again — have one main cause. Here's the cause, and how to shut it down for good.
The idea, in one line
Most flakiness comes from acting on an element before the page is ready. The fix is to wait for the right thing at the right moment.
Two kinds of wait (don't mix them)
- Implicit wait —
driver.implicitly_wait(10)sets one global timeout for every lookup. Convenient, but it hides timing bugs. - Explicit wait —
WebDriverWait(driver, 10).until(...)waits for one specific condition (clickable, visible, text present) at one step. Precise and self-documenting.
See it work
▸ try it
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.webdriver.common.action_chains import ActionChains
wait = WebDriverWait(driver, timeout=10)
# Wait for THIS condition only — not a blind sleep
btn = wait.until(EC.element_to_be_clickable((By.ID, "submit")))
btn.click()
# Hover over a menu to reveal it
menu = driver.find_element(By.ID, "account")
ActionChains(driver).move_to_element(menu).perform()
# Step inside an iframe, then step back out
driver.switch_to.frame("payment_iframe")
driver.find_element(By.ID, "card").send_keys("4111111111111111")
driver.switch_to.default_content()Read it top to bottom: wait until the button can actually be clicked, then click it. Hover to reveal a menu. Move into an iframe to reach its content, then move back out.
Advanced — gestures, frames and windows
- For anything past click and type — hover, drag, key combos — use
ActionChains. - Content inside an iframe is a separate document. Selenium can't see it until you call
driver.switch_to.frame(...), and you return withswitch_to.default_content(). - New tabs and popups work the same way, through
driver.window_handlesandswitch_to.window(...).
Grounded in the official Selenium (Python) docs — Waits, Actions API, Frames
All lessons in UI Automation: Selenium + Playwright + pytest
- Selenium Locators: Finding the Element You Mean
- Waits, Actions & Frames — Killing Flakiness at the Source
- Playwright: Auto-Waiting, Locators & Tracing
- pytest Deep-Dive: Fixtures, conftest, Parametrize & Markers
- Page Object Model, Done Properly
- The Hard Stuff: Dynamic Elements, iframes, Uploads & Network Stubbing