Wait Times - Implicit vs Explicit Waits
flowchart TD
A["a page that loads content after the initial response"] --> B{"how do you wait?"}
B -->|"time.sleep(5)"| C["always slow, and still fails on a slow day"]
B -->|"implicit wait"| D["applies to every find_element, retries until timeout"]
B -->|"explicit wait"| E["WebDriverWait(driver, 10).until(condition)"]
D --> F["only knows 'does it exist yet'"]
E --> G["can wait for visible, clickable, staleness, custom"]
H["setting both"] --> I["timeouts compound unpredictably -- pick one"]
G --> J["prefer explicit: it says what you are waiting FOR"]
Why waits matter
Section titled “Why waits matter”Dynamic pages load elements later.
If you try to locate elements too early, you get:
NoSuchElementExceptionElementNotInteractableException
Implicit wait
Section titled “Implicit wait”from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(5) # secondsIt applies to element lookups globally.
Explicit wait (recommended)
Section titled “Explicit wait (recommended)”from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
h1 = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.TAG_NAME, "h1"))
)
print(h1.text)
finally:
driver.quit()Prefer explicit waits for specific elements.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading