Web scraping
How do I wait for a page to load in Selenium?
driver.get already waits for the document to finish loading; for anything rendered after that, use WebDriverWait with an expected condition tied to the element you need.
The answer
With the default page load strategy, driver.get() returns once document.readyState is "complete". That is enough for static pages. On modern sites, though, data often arrives through scripts after that point, so you add an explicit wait: WebDriverWait(driver, 15).until(condition), where the condition checks for the element, text or state you actually need. Avoid fixed time.sleep() calls, which make scripts both slower and flakier.
Explicit waits with expected conditions
from selenium.webdriver.common.by import By # used for every wait target
from selenium.webdriver.support.ui import WebDriverWait # explicit waits
import selenium.webdriver.support.expected_conditions as EC
waiter = WebDriverWait(driver, 15, poll_frequency=0.3)
driver.get("https://fares.example.travel/results?from=LIS&to=AMS")
waiter.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, ".loading-overlay")))
first_fare = waiter.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.fare-card")))
waiter.until(lambda d: len(d.find_elements(By.CSS_SELECTOR, "div.fare-card")) >= 5)
The conditions you will use most are presence_of_element_located (in the DOM), visibility_of_element_located (shown on screen), element_to_be_clickable, text_to_be_present_in_element and invisibility_of_element_located for spinners. Any function that takes the driver and returns a truthy value also works, as the lambda shows.
Page load strategies
You can change what get() waits for through the options object:
| Strategy | get() returns when | Good for |
|---|---|---|
normal (default) | readyState is complete, all subresources loaded | simple, predictable scripts |
eager | readyState is interactive, HTML parsed | pages with slow images or ads |
none | right after the navigation starts | full manual control with explicit waits |
Set it with options.page_load_strategy = "eager" before creating the driver. With eager or none, explicit waits become essential, since get() no longer waits for much.
Implicit waits: use one style
driver.implicitly_wait(10) makes every find_element retry for up to ten seconds. It is convenient but blunt, and the Selenium documentation warns against mixing it with explicit waits, because the two can combine into unpredictable delays. Pick explicit waits for scraping work, set the implicit wait to zero, and your timings will be easier to reason about.
Waiting after a click that navigates
After clicking a link, the old page may still be in place for a moment, and a wait for a common element (say, the header) succeeds on the old page. Wait for the old page to go away first: keep a reference to an element from it and wait for EC.staleness_of(old_element), then wait for content of the new page. Checking EC.url_contains("/page/2") is another simple signal.
When the wait never ends
A TimeoutException means the condition did not become true. Before raising the timeout, look at what the browser shows. Save driver.page_source and a screenshot in the exception handler. Common causes are a consent dialog covering the page, a login redirect, a changed selector, and bot checks such as a reCAPTCHA or Turnstile widget. In the last case no amount of waiting helps; detect the widget, solve it with a token task, and continue as shown in the Selenium integration. The timeout entry explains how to choose sensible limits.
Common mistakes
- Waiting for presence, then clicking. An element can exist while hidden or covered. Wait for clickability before clicking.
- Catching StaleElementReferenceException everywhere. It signals that the page re-rendered. Locate the element again after the wait instead of reusing old handles.
- Long global timeouts. A 60 second wait on every lookup makes a broken run take hours. Keep waits short and specific, and fail fast.
- Waiting on readyState in a single page app. It stays complete while the app swaps views. Wait for content.
Waits in a page object
As scripts grow, put the waits next to the selectors they belong to. A small class per page type, with methods like open() that navigates and waits for that page's ready signal, keeps the waiting logic in one place. When the site changes its loading behavior, you fix one method instead of every script that visits the page.
Related techniques
If you parse with Beautiful Soup after the wait, take the snapshot only once the condition passes, as described in combining Beautiful Soup with Selenium. Puppeteer users have a parallel guide on waiting in Puppeteer, and the Selenium and Python guide puts these waits in a full scraper.
Common questions
Does driver.get wait for AJAX content?
No. It waits for the document load event; data fetched by scripts afterwards needs an explicit wait.
Should I use implicit or explicit waits?
Explicit waits. They target a specific condition and do not interfere with each other the way mixed waits can.
How do I wait for text to change?
Use text_to_be_present_in_element, or a lambda that compares the element text with the old value.
What poll frequency should WebDriverWait use?
The default half second is fine; a shorter interval reacts faster at the cost of a few more browser calls.
Why does my wait pass but the click fails?
The element exists but is covered or disabled. Wait for element_to_be_clickable and close overlays first.
Keep reading
- SeleniumThe long standing browser automation project built on WebDriver, usable from many languages and scalable through Grid.
- Web scraping with Selenium and PythonSelenium scraping that stays stable: explicit waits, locators that survive redesigns, headless Chrome, infinite scroll and stale elements.
- SeleniumThe reference Selenium setup in Python and Java: explicit waits, reCAPTCHA v2 and Turnstile tokens, callbacks, retries and Grid scaling.
- How do I combine Beautiful Soup with Selenium?Let Selenium render the page and wait for content, then hand driver.page_source to Beautiful Soup for fast, readable parsing.