Selenium + Python · Lesson 24 of 43

0/43 done

Module 24: Waits

24. Robust Synchronization: WebDriverWait & Expected Conditions

18 minlesson

Your task

Implement wait_for_clickable(driver, locator, timeout=10):

  1. Return WebDriverWait(driver, timeout).until(EC.element_to_be_clickable(locator)).

Write it in the editor, then press Check answer (Ctrl + Enter).

WebDriverWait in Python

python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, timeout=10)

# Wait until element is visible:
banner = wait.until(EC.visibility_of_element_located((By.ID, "welcome-banner")))

# Wait until clickable:
submit_btn = wait.until(EC.element_to_be_clickable((By.ID, "submit-btn")))
submit_btn.click()

Key Takeaways & Walkthrough Summary

Welcome to Phase 4! Avoiding time.sleep() and mastering WebDriverWait is the key to eliminating flakiness in Python automation. In this lesson, you will master WebDriverWait combined with expected_conditions for visibility, clickability, and presence. Let's master waits!

Part of the Selenium + Python course (Selenium with Python). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why is time.sleep() considered an automation anti-pattern?

time.sleep() freezes the Python process unconditionally, slowing test suites when elements are ready early and failing when network delays exceed the hardcoded sleep.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip