Implicit Waits
Understand implicit waits, their global behavior, and when to use them versus explicit waits.
Selenium 3 & 4 Medium
Implicit waits tell WebDriver to poll the DOM for a certain amount of time when trying to find elements that are not immediately available. Once set, the implicit wait applies globally to all findElement calls.
Setting Implicit Wait
Configure Implicit Wait
Selenium 3 & 4 Stable
import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import java.time.Duration;
WebDriver driver = new ChromeDriver();
// Set implicit wait to 10 secondsdriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Now all findElement calls will wait up to 10 secondsWebElement element = driver.findElement(By.id("dynamic-element"));// If not found within 10 seconds, throws NoSuchElementExceptionfrom selenium import webdriverfrom selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# Set implicit wait to 10 secondsdriver.implicitly_wait(10)
# Now all find_element calls will wait up to 10 secondselement = driver.find_element(By.ID, "dynamic-element")# If not found within 10 seconds, raises NoSuchElementExceptionconst { Builder, By } = require('selenium-webdriver');
const driver = await new Builder().forBrowser('chrome').build();
// Set implicit wait to 10 secondsawait driver.manage().setTimeouts({ implicit: 10000 });
// Now all findElement calls will wait up to 10 secondsconst element = await driver.findElement(By.id('dynamic-element'));// If not found within 10 seconds, throws NoSuchElementErrorusing OpenQA.Selenium;using OpenQA.Selenium.Chrome;
IWebDriver driver = new ChromeDriver();
// Set implicit wait to 10 secondsdriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
// Now all FindElement calls will wait up to 10 secondsIWebElement element = driver.FindElement(By.Id("dynamic-element"));// If not found within 10 seconds, throws NoSuchElementExceptionHow Implicit Wait Works
- You call
findElement() - WebDriver checks if element exists immediately
- If not found, WebDriver waits and retries
- This continues until element is found OR timeout expires
- If timeout expires without finding element, exception is thrown
findElement() called │ ├─► Element found immediately? ─► YES ─► Return element │ NO (element not in DOM) │ ├─► Wait ~500ms, retry │ ├─► Element found? ─► YES ─► Return element │ NO │ ├─► Timeout expired? ─► YES ─► Throw NoSuchElementException │ NO │ └─► Loop back to wait and retryImplicit Wait Scope
Implicit Wait Is Global
Selenium 3 & 4 Stable
// Set once at the beginningdriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Applies to ALL subsequent findElement callsdriver.findElement(By.id("header")); // Waits up to 10sdriver.findElement(By.id("content")); // Waits up to 10sdriver.findElement(By.id("footer")); // Waits up to 10s
// Change it anytimedriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
// Now all calls wait up to 5 secondsdriver.findElement(By.id("sidebar")); // Waits up to 5s
// Disable implicit waitdriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));# Set once at the beginningdriver.implicitly_wait(10)
# Applies to ALL subsequent find_element callsdriver.find_element(By.ID, "header") # Waits up to 10sdriver.find_element(By.ID, "content") # Waits up to 10sdriver.find_element(By.ID, "footer") # Waits up to 10s
# Change it anytimedriver.implicitly_wait(5)
# Now all calls wait up to 5 secondsdriver.find_element(By.ID, "sidebar") # Waits up to 5s
# Disable implicit waitdriver.implicitly_wait(0)// Set once at the beginningawait driver.manage().setTimeouts({ implicit: 10000 });
// Applies to ALL subsequent findElement callsawait driver.findElement(By.id('header')); // Waits up to 10sawait driver.findElement(By.id('content')); // Waits up to 10sawait driver.findElement(By.id('footer')); // Waits up to 10s
// Change it anytimeawait driver.manage().setTimeouts({ implicit: 5000 });
// Now all calls wait up to 5 secondsawait driver.findElement(By.id('sidebar')); // Waits up to 5s
// Disable implicit waitawait driver.manage().setTimeouts({ implicit: 0 });// Set once at the beginningdriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
// Applies to ALL subsequent FindElement callsdriver.FindElement(By.Id("header")); // Waits up to 10sdriver.FindElement(By.Id("content")); // Waits up to 10sdriver.FindElement(By.Id("footer")); // Waits up to 10s
// Change it anytimedriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
// Now all calls wait up to 5 secondsdriver.FindElement(By.Id("sidebar")); // Waits up to 5s
// Disable implicit waitdriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);Limitations of Implicit Waits
1. Only Waits for Element Existence
Implicit wait only checks if an element exists in the DOM. It does NOT wait for:
- Element to be visible
- Element to be clickable
- Element to have specific text
- Element to be enabled
- Any other condition
Implicit Wait Limitations
Selenium 3 & 4 Fragile
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Element exists but is hidden - implicit wait returns it anyway!WebElement hiddenElement = driver.findElement(By.id("loading-spinner"));hiddenElement.click(); // Might fail - element exists but not clickable
// Element exists but disabledWebElement disabledButton = driver.findElement(By.id("submit-btn"));disabledButton.click(); // Might fail - element exists but disabled
// For these cases, use explicit waits instead!driver.implicitly_wait(10)
# Element exists but is hidden - implicit wait returns it anyway!hidden_element = driver.find_element(By.ID, "loading-spinner")hidden_element.click() # Might fail - element exists but not clickable
# Element exists but disableddisabled_button = driver.find_element(By.ID, "submit-btn")disabled_button.click() # Might fail - element exists but disabled
# For these cases, use explicit waits instead!await driver.manage().setTimeouts({ implicit: 10000 });
// Element exists but is hidden - implicit wait returns it anyway!const hiddenElement = await driver.findElement(By.id('loading-spinner'));await hiddenElement.click(); // Might fail - element exists but not clickable
// Element exists but disabledconst disabledButton = await driver.findElement(By.id('submit-btn'));await disabledButton.click(); // Might fail - element exists but disabled
// For these cases, use explicit waits instead!driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
// Element exists but is hidden - implicit wait returns it anyway!IWebElement hiddenElement = driver.FindElement(By.Id("loading-spinner"));hiddenElement.Click(); // Might fail - element exists but not clickable
// Element exists but disabledIWebElement disabledButton = driver.FindElement(By.Id("submit-btn"));disabledButton.Click(); // Might fail - element exists but disabled
// For these cases, use explicit waits instead!2. Slows Down Failing Tests
If an element truly doesn’t exist, you wait the full timeout before failing:
Slow Failure with Implicit Wait
Selenium 3 & 4 Stable
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// If you're checking for element absence, this is slow!try { driver.findElement(By.id("error-message")); System.out.println("Error is displayed");} catch (NoSuchElementException e) { // Takes 30 seconds to reach this point! System.out.println("No error");}
// Better: Use findElements which returns empty list immediately// (but still waits with implicit wait set)driver.implicitly_wait(30)
# If you're checking for element absence, this is slow!try: driver.find_element(By.ID, "error-message") print("Error is displayed")except NoSuchElementException: # Takes 30 seconds to reach this point! print("No error")
# Better: Use find_elements which returns empty list# (but still waits with implicit wait set)await driver.manage().setTimeouts({ implicit: 30000 });
// If you're checking for element absence, this is slow!try { await driver.findElement(By.id('error-message')); console.log('Error is displayed');} catch (e) { // Takes 30 seconds to reach this point! console.log('No error');}driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
// If you're checking for element absence, this is slow!try{ driver.FindElement(By.Id("error-message")); Console.WriteLine("Error is displayed");}catch (NoSuchElementException){ // Takes 30 seconds to reach this point! Console.WriteLine("No error");}Implicit vs Explicit Waits
| Feature | Implicit Wait | Explicit Wait |
|---|---|---|
| Scope | Global | Per call |
| Conditions | Only element existence | Any condition |
| Flexibility | Low | High |
| Mixing | Not recommended | Preferred |
| Performance | Can slow tests | More efficient |
Best Practices
- Keep implicit waits short (0-5 seconds) or don’t use them
- Prefer explicit waits for specific conditions
- Don’t mix long implicit waits with explicit waits
- Set once at the beginning of your test setup
- Consider disabling implicit waits entirely and using only explicit waits
Recommended Approach
Selenium 3 & 4 Stable
public class BaseTest { protected WebDriver driver;
@BeforeMethod public void setup() { driver = new ChromeDriver();
// Option 1: Short implicit wait for basic stability driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
// Option 2: No implicit wait, use explicit waits everywhere // driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0)); }}class BaseTest: def setup_method(self): self.driver = webdriver.Chrome()
# Option 1: Short implicit wait for basic stability self.driver.implicitly_wait(2)
# Option 2: No implicit wait, use explicit waits everywhere # self.driver.implicitly_wait(0)async function setup() { const driver = await new Builder().forBrowser('chrome').build();
// Option 1: Short implicit wait for basic stability await driver.manage().setTimeouts({ implicit: 2000 });
// Option 2: No implicit wait, use explicit waits everywhere // await driver.manage().setTimeouts({ implicit: 0 });
return driver;}public class BaseTest{ protected IWebDriver Driver;
[SetUp] public void Setup() { Driver = new ChromeDriver();
// Option 1: Short implicit wait for basic stability Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
// Option 2: No implicit wait, use explicit waits everywhere // Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0); }}Next Steps
- Explicit Waits - Wait for specific conditions
- Fluent Waits - Advanced wait configuration
- Expected Conditions - Built-in wait conditions
- Debugging Tips - Troubleshoot timing issues