3. Explicit waits — never Thread.sleep
Selenium WebDriver · 48 pages source format
Flaky Selenium is almost always wrong waits. WebDriverWait + ExpectedConditions beat sleep every time.
What you'll learn
- Implicit vs explicit waits
- ExpectedConditions
- Custom wait conditions
- FluentWait
Ban Thread.sleep
sleep(3) hides races. Under CI load, races become flakes. Zero tolerance.
Do this now
Search codebase for sleep/Thread.sleep/time.sleep. Replace all.
Explicit wait pattern
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "btn"))).click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#start button")))
btn.click()
wait.until(EC.visibility_of_element_located((By.ID, "finish")))Do this now
the-internet.herokuapp.com/dynamic_loading/ — wait for Hello World visible after Start button.
Expected conditions catalog
visibility_of, element_to_be_clickable, text_to_be_present_in_element, invisibility_of_element_located.
Do this now
Use 4 different EC types across 4 tests. Document favorites in WAITS.md.
Avoid implicit wait mixing
Do not mix implicit and explicit waits — unpredictable timeouts. Pick explicit only.
Do this now
Ensure driver.implicitly_wait is NOT set (or set to 0).