What are the differences between Selenium's explicit and implicit waits?

Selenium's Explicit and Implicit waits are both applied during tests to improve their stability when interacting with web elements that may not be immediately available. However, there are considerable differences between them.

Explicit Wait

An explicit wait is a code you define to halt the program for a certain amount of time until a specific condition is met. The wait is 'explicit' because you specify what to wait for.

Here's an example in Python:

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.Firefox()
driver.get("http://www.example.com")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "my_element"))
    )
finally:
    driver.quit()

And here's a JavaScript example:

const {Builder, By, until} = require('selenium-webdriver');

let driver = new Builder().forBrowser('firefox').build();
driver.get('http://www.example.com');

driver.wait(until.elementLocated(By.id('my_element')), 10000)
    .then(element => {
        // Interact with the element here
    })
    .finally(() => driver.quit());

In the above examples, the code will wait up to 10 seconds for an element with the ID my_element to be present before throwing a timeout exception.

Implicit Wait

An implicit wait tells WebDriver to wait for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, and once set, the implicit wait is set for the life of the WebDriver object instance.

Here's an example in Python:

from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://www.example.com")
element = driver.find_element_by_id("my_element")

And here's a JavaScript example:

const {Builder, By} = require('selenium-webdriver');

let driver = new Builder().forBrowser('firefox').build();
driver.manage().timeouts().implicitlyWait(10000); // 10 seconds
driver.get('http://www.example.com');

let element = driver.findElement(By.id('my_element'));

In the above examples, the code will wait up to 10 seconds when trying to find an element or elements before throwing a NoSuchElementException.

Differences

The key differences between the two waits are:

  1. When they are applied: An implicit wait is set for the duration of the WebDriver, whereas an explicit wait is set for a particular instance only.
  2. What they wait for: An explicit wait is used to wait for a certain condition to occur before proceeding further in the code. An implicit wait is to wait for a certain amount of time before throwing a NoSuchElementException.
  3. Flexibility: Explicit waits are more flexible as they allow you to wait until a certain condition is met, while implicit waits only wait for a fixed amount of time.

It's worth noting that mixing implicit and explicit waits can cause unpredictable wait times. Explicit waits are generally more reliable, but can be more complex to code, as they require specifying conditions.

Related Questions

Get Started Now

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