Waits
Modern pages load content asynchronously. Correct waiting is the difference between stable and flaky Selenium tests.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Related tutorials
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#
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));
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#
- WebDriverWait polls the condition every 500 ms.
- The lambda returns null until results are visible.
- A second wait checks the spinner is gone.
- 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#
Slow and still flaky. Wait for conditions.
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
var pay = wait.Until(d => { var b = d.FindElement(By.Id("pay")); return b.Enabled ? b : null; });
Mini quiz#
Which wait polls for a specific condition?
Want more? Practise all Automation Testing MCQs · Automation Testing interview questions
My notes#
Saved in this browser.