What is implicit and explicit wait in Selenium WebDriver, and when should each be used?

Implicit Wait

In Selenium WebDriver, an implicit wait is used to tell the WebDriver to wait for a certain amount of time before it throws a NoSuchElementException. The default setting is 0 (zero). Once you set the time, the WebDriver will wait for the element for that amount of time before throwing an exception. This means that if the element appears within the specified time frame, the WebDriver will proceed with the execution of the code; otherwise, it will throw an exception after the time has elapsed.

The implicit wait is set for the entire duration of the WebDriver object instance.

Python Example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10) # waits up to 10 seconds before throwing a NoSuchElementException
driver.get("http://example.com")
my_element = driver.find_element_by_id("myElement")

JavaScript Example (WebDriverIO - a custom implementation of WebDriver API for Node.js):

const { remote } = require('webdriverio');

(async () => {
  const browser = await remote({
      logLevel: 'error',
      path: '/',
      capabilities: {
          browserName: 'chrome'
      }
  });

  browser.setTimeout({ 'implicit': 10000 }); // waits up to 10 seconds before throwing a NoSuchElement error
  await browser.url('http://example.com');
  const myElement = await browser.$('#myElement');
})();

Explicit Wait

An explicit wait is a code you define to wait for a certain condition to occur before proceeding further with the execution. It's more sophisticated than implicit wait because it allows you to wait for just the right moment or condition rather than having a fixed time-out. You tell WebDriver to wait for certain conditions (Expected Conditions) or the maximum time exceeded, whichever comes first.

Explicit wait should be used when certain elements take different amounts of time to appear on the page and you want to wait for them in a more precise way.

Python Example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("http://example.com")
wait = WebDriverWait(driver, 10) # waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds.
my_element = wait.until(EC.visibility_of_element_located((By.ID, "myElement")))

JavaScript Example (WebDriverIO):

const { remote } = require('webdriverio');

(async () => {
  const browser = await remote({
      logLevel: 'error',
      path: '/',
      capabilities: {
          browserName: 'chrome'
      }
  });

  await browser.url('http://example.com');
  const myElement = await browser.$('#myElement');
  await myElement.waitForDisplayed({ timeout: 10000 }); // waits up to 10 seconds before throwing an error unless it becomes visible within 10 seconds.
})();

When to Use Each

  • Implicit Wait: Use an implicit wait when you want to set a base waiting time for all elements in your script, ensuring that WebDriver polls the DOM for a certain amount of time when trying to find any element not immediately available.

  • Explicit Wait: Use an explicit wait when you need to wait for specific conditions on a certain element, such as visibility, clickability, presence, or absence, etc. This is more flexible than implicit wait as it allows you to wait for elements with different conditions.

It's recommended not to mix implicit and explicit waits as it can lead to unpredictable wait times. Generally, explicit waits are preferred due to their flexibility and the ability to wait for specific conditions on an element, providing a more robust way to interact with elements that might take varying amounts of time to appear or be ready for interaction.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon