Automation Testing · Selenium WebDriver

Waits

Modern pages load content asynchronously. Correct waiting is the difference between stable and flaky Selenium tests.

Intermediate 5 min Lesson 7 of 24
Before you start:Locators
0/24 in Automation Testing
On this page · 5 min · Intermediate

Learning objectives#

  • Explain why fixed sleeps fail
  • Use explicit waits with conditions
  • Understand implicit waits and why not to mix them
  • Wait for dynamic content

Explanation#

Thread.Sleep(5000) either waits too long (slow) or not long enough (flaky).

An implicit wait applies a global timeout to every FindElement. An explicit wait (WebDriverWait) polls until a condition you choose is true.

Prefer explicit waits and do not combine them with implicit waits — mixing causes unpredictable timeouts.

Wait for what the user waits for: an element visible, clickable, text present or a spinner gone.

Code example: Explicit waits with WebDriverWait#

C#Explicit waits with WebDriverWait Illustrative
using OpenQA.Selenium.Support.UI;   // dotnet add package Selenium.Support


var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));


// Wait until the search results are visible
driver.FindElement(By.Name("q")).SendKeys("laptop" + Keys.Enter);
var results = wait.Until(d =>
{
    var list = d.FindElements(By.CssSelector("[data-testid='result']"));
    return list.Count > 0 && list[0].Displayed ? list : null;
});


// Wait until the loading spinner disappears
wait.Until(d => d.FindElements(By.CssSelector(".spinner")).Count == 0);


Assert.That(results!.Count, Is.GreaterThan(0));
Expected output
The test continues the moment results appear — 300 ms on a fast day, up to 10 s on a slow one — and fails clearly after the timeout.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. WebDriverWait polls the condition every 500 ms.
  2. The lambda returns null until results are visible.
  3. A second wait checks the spinner is gone.
  4. Timeouts throw WebDriverTimeoutException with a clear message.

Real-world use case#

Replacing every Thread.Sleep in a suite with explicit waits cut run time by 40% and removed most random failures.

Common mistakes#

Thread.Sleep everywhere

Slow and still flaky. Wait for conditions.

Mixing implicit and explicit waits

Timeouts can multiply. Choose explicit waits.

Best practices#

  • Explicit waits for specific conditions
  • No Thread.Sleep
  • Reasonable timeouts (5–15 s)
  • Wrap common waits in helper methods

Remember this#

Sleep = slow or flaky.

WebDriverWait + condition.

Don’t mix implicit and explicit.

Practice exercise#

Your task: Wait until a button with id "pay" is enabled.

Show solution
C# Illustrative
var pay = wait.Until(d => { var b = d.FindElement(By.Id("pay")); return b.Enabled ? b : null; });

Mini quiz#

Waits quizMultiple choice Question 1 of 1

Which wait polls for a specific condition?

Score so far: 0/0

Want more? Practise all Automation Testing MCQs · Automation Testing interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.