Skip to content

How to wait for the page to load in Selenium

A Selenium script can reach a page and still fail on the next line because the element it needs has not appeared yet.

OUTPUT FORMATSHTMLJSONMarkdown

Selenium offers three wait strategies for this, and each one fits a different situation. The default driver behaviour also matters: it waits for the page itself to load, but it does not wait for AJAX requests started by that page.

The three wait strategies at a glance

Selenium gives you three ways to handle timing, and they differ in what they wait for and how much control you get.

  • Explicit wait — pause until a specific condition is true, such as a particular element becoming visible on the screen. This is the most precise option when you know which element signals readiness.
  • Implicit wait — pause for a fixed time interval before failing to find an element. Simple, but it applies a blanket delay rather than checking for a condition.
  • Fluent wait — works like an explicit wait but adds control over timeouts and polling frequency, so you can decide how often Selenium rechecks the condition.

A quick way to choose: if you can name the element or state that means "ready", use an explicit wait. If you only need a short grace period for element lookups, an implicit wait may be enough. If you need to tune how often the check runs, reach for a fluent wait.

What the driver waits for by default

When the driver navigates to a URL, it waits for the page to load. What it does not do is wait for AJAX requests that the page starts after loading. That gap is the usual reason a script finds an empty or incomplete page even though navigation succeeded.

To close the gap, tell Selenium explicitly what to wait for. The WebDriverWait class combined with the expected_conditions module lets you wait for a specific element rather than guessing a sleep duration.

A useful mental model: the driver's default wait covers the document, while an explicit wait covers the content your script actually needs.

Example: waiting for an element by ID

The example below navigates to a site and waits for an element with the ID wrapper. It uses a three-second timeout and prints one message if the element appears in the DOM and another if the wait times out.

The complete, exact Python snippet is supplied as a protected code module and accompanies this page. Read it as a small contract with five moving parts:

  • Driver setup — a Chrome driver is created from a local executable path.
  • Navigation — driver.get(...) opens the target URL; the site base URL is a configuration placeholder rather than a hard-coded address.
  • Timeout value — delay = 3 sets how long the wait may last, in seconds.
  • Condition — EC.presence_of_element_located((By.ID, 'wrapper')) checks that an element with that ID exists in the DOM.
  • Outcome branches — success prints that the element is present; a TimeoutException prints that it did not show up.

Two details are easy to miss. First, presence_of_element_located confirms the element is in the DOM, not that it is visible or clickable. Second, the timeout is a ceiling, not a fixed sleep: the wait returns as soon as the condition is satisfied.

Telling the failure modes apart

When a wait fails, the cause is not always the same. Use this checklist to narrow it down before changing timeouts.

  1. Page never loaded — navigation itself failed or is still in progress. Check the URL and whether the driver reached the document at all.
  2. Page loaded, AJAX content missing — the default wait finished, but the data your element depends on arrives later. Switch to an explicit wait on the element that appears after the AJAX call.
  3. Element exists but is not visible — presence_of_element_located can succeed while the element is hidden. If you need visibility, use a visibility-oriented expected condition instead.
  4. Element never appears because the locator is wrong — a timeout will look identical to a slow page. Verify the ID, class or selector against the rendered DOM.
  5. Timeout too short — the condition is correct but the page is slower than the configured delay. Increase the timeout rather than adding fixed sleeps.

A TimeoutException only tells you the condition was not met within the limit. It does not tell you which of the above happened, so inspect the page state when the exception fires.

Related Selenium tasks

Once waiting is under control, these companion tasks often come next in a scraping script:

  • Locating elements by XPath or by CSS selector.
  • Blocking image loading to speed up page loads.
  • Reading the page source after the content has settled.
  • Scrolling to an element before interacting with it.
  • Taking a screenshot for debugging a failed wait.
  • Fixing chromedriver or geckodriver PATH errors.
  • Saving and loading cookies between runs.

Each of these is covered in its own Selenium question page, and a broader Selenium web scraping tutorial is available for the end-to-end workflow.

Frequently asked questions

Does Selenium wait for AJAX requests by default?

No. The driver's default wait covers the page load, not the AJAX requests the page starts afterward. If your target element is populated by AJAX, add an explicit wait for that element.

What is the difference between explicit and fluent waits?

Both wait for a condition, but a fluent wait gives you additional control over timeouts and polling frequency, so you can tune how often Selenium rechecks the condition.

Why does my element wait time out even though the page loaded?

Common causes are AJAX content that has not arrived, a locator that does not match the rendered DOM, an element that exists but is hidden, or a timeout that is shorter than the page needs. Check the page state when the TimeoutException fires to tell these apart.

Does presence_of_element_located guarantee the element is visible?

No. It confirms the element is present in the DOM. If you need visibility or clickability, use an expected condition that checks those states.

Should I use time.sleep instead of WebDriverWait?

A fixed sleep always waits the full duration, even when the element appears sooner. WebDriverWait returns as soon as the condition is met and raises a TimeoutException when it is not, which makes failures easier to detect and handle.