UI Automation: Selenium + Playwright + pytest · Lesson 2 of 6

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.

By Shahriyar · Updated

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)

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

Grounded in the official Selenium (Python) docs — Waits, Actions API, Frames

All lessons in UI Automation: Selenium + Playwright + pytest

  1. Selenium Locators: Finding the Element You Mean
  2. Waits, Actions & Frames — Killing Flakiness at the Source
  3. Playwright: Auto-Waiting, Locators & Tracing
  4. pytest Deep-Dive: Fixtures, conftest, Parametrize & Markers
  5. Page Object Model, Done Properly
  6. The Hard Stuff: Dynamic Elements, iframes, Uploads & Network Stubbing