DEV Community

Cover image for How to Stop Selenium Tests from Clicking Before the Page is Ready
Mangai Ram
Mangai Ram

Posted on • Originally published at testleaf.com

How to Stop Selenium Tests from Clicking Before the Page is Ready

Selenium: Using Explicit Waits for Spinners, Pop-ups & Loading Screens


Why This Matters

One of the most common problems in Selenium testing is:
Your test tries to click a button, type in a field, or read data before the page finishes loading.

This leads to errors like:

  • ElementNotInteractableException
  • NoSuchElementException
  • StaleElementReferenceException

In simple words → Selenium is faster than your website.

The browser is still loading, but Selenium is already trying to interact.

That’s where Explicit Waits help.


What Are Explicit Waits?

An Explicit Wait tells Selenium:

“Wait until a certain condition is true before moving to the next step.”

You control what to wait for and how long to wait.

Unlike general fixed waits (like Thread.sleep(5000)),
explicit waits are smart waits — they wait only until needed, not longer.


Example: Waiting for a Loading Spinner to Disappear

Many modern web apps show a spinner or loading animation while content loads.

Without Explicit Wait

driver.findElement(By.id("checkout")).click();
Enter fullscreen mode Exit fullscreen mode

If spinner is still visible → test fails.

With Explicit Wait

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".spinner")));
driver.findElement(By.id("checkout")).click();
Enter fullscreen mode Exit fullscreen mode

Now Selenium waits until the spinner disappears before clicking.


Example: Waiting for a Modal Popup to Appear

Sometimes you need to wait for something to show up.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-modal")));
Enter fullscreen mode Exit fullscreen mode

This ensures the modal is fully loaded before interacting.


Waiting for Network / AJAX Load

Some pages update data in the background (no full page refresh).
Instead of checking time manually, we wait until the page appears “stable.”

new WebDriverWait(driver, Duration.ofSeconds(15))
        .until(webDriver -> ((JavascriptExecutor) webDriver)
        .executeScript("return document.readyState").equals("complete"));
Enter fullscreen mode Exit fullscreen mode

This says:

Wait until page loading is finished.


Why Explicit Waits Are Better Than Thread.sleep()

Thread.sleep() Explicit Wait
Always waits full time Waits only as long as needed
Slows tests Makes tests faster
Doesn’t care if page is ready Responds to real conditions
Not reliable Very reliable

So instead of blindly waiting → Selenium waits intelligently.


When to Use Explicit Waits

Use explicit waits when your app has:

  • Loading spinners
  • Popup messages
  • Slow dropdowns
  • AJAX-based content loads
  • React/Angular dynamic UI updates

Basically → Anytime the page takes time to update.


Best Practices

Good Practice Why
Wait for disappearing elements Prevent premature clicks
Wait for clickable element Ensures stability
Wait for text/value change Validates real-time updates
Avoid long timeouts Speed + accuracy

Real-Life Example

Before waits:
Testers complained that tests passed sometimes and failed sometimes (flaky tests).

After using explicit waits:
Stable results, fewer failures, and faster execution.

This is one of the biggest improvements QA leads notice.


In Simple Words

  • Websites don’t load instantly.
  • Selenium is very fast.
  • If Selenium tries to interact before the page is ready → the test fails.
  • Explicit waits fix this by making Selenium pause only until the page is ready.

FAQs

Q1: Do waits slow down tests?

No. They actually make tests faster because Selenium doesn’t have to retry failed steps.

Q2: Should we remove Thread.sleep() completely?

Yes — use explicit waits instead. They are smarter and more reliable.

Q3: Is it hard to convert to explicit waits?

No — just replace direct actions with wait-and-action patterns.


Summary
Using explicit waits makes Selenium automation much more stable.
You avoid flaky tests, reduce failures, and let Selenium interact only when the page is ready.


Still Selenium rule the Automation industry?

Top comments (0)