Module 3 · Automation with Selenium and a real project Lesson 8 of 15
Waits and selectors that survive
A hard-coded sleep makes a suite slow and still flaky. An explicit wait on a condition — visibility, presence, clickability — makes it fast and stable, and choosing the right condition is the whole craft.
The concept
Modern pages build themselves with JavaScript, so an element exists a moment after the page loads. WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'apply'))) waits for the thing you actually need instead of time.sleep(5), which is slow when it is fast and fails when it is slow.
Choose selectors a developer would keep: an id, a data-testid attribute, or a stable class — not div:nth-child(3) > span, which breaks the next time somebody reorders the layout. Ask for test ids in the code if you can; it is a conversation a junior tester can genuinely start.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# Bad: sleeping, and a selector tied to layout
# driver.find_element(By.XPATH, "//div[3]/div[2]/button")
# Good: wait for the CONDITION, then act
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "[data-testid=apply]"))
)
button.click()Common mistakes
The mistake is CSS paths copied from the browser inspector, which are tied to today's markup. The second is a fixed sleep everywhere, which makes a suite take twenty minutes and still flake. Waits that express the condition are both faster and more honest.
Worked example
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "dashboard"))) waits only as long as it needs to. time.sleep(5) is either too short on a slow build or five wasted seconds on every fast one.
Your practice task
Convert three of your hard sleeps into explicit waits and run the suite twice in a row to prove both passes are genuine.
Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.
Key takeaway
Modern pages build themselves with JavaScript, so an element exists a moment after the page loads.
Keep your place in this course
A free account saves which lessons you have finished and unlocks the certificate. The lesson itself stays free to read.
Create free account or sign inCourse complete
You finished every lesson.
Your certificate is ready, and the projects you built are the proof to attach to an application.
Almost there
15 lessons left of 15
Finish every lesson to unlock the certificate. Each one ends in a practice task — do them and the final project brief writes itself.