Difference between implicit and explicit wait in selenium

As I explained in my article about the wait commands in Selenium Webdriver, Selenium is a powerful tool used for automating browser actions, and efficient waiting mechanisms are crucial for its successful utilization. Among the various waiting strategies Selenium offers, implicit and explicit waits are fundamental concepts. Let’s explore the differences between them to optimize your test automation.

Implicit Wait: Patiently Waiting for Elements

Implicit wait is a time delay that the WebDriver waits for elements to be present before throwing an exception. When set, it’s applied globally for all the elements in the script. The WebDriver will wait for a specified amount of time before throwing a NoSuchElementException, giving the web page enough time to load and elements to become accessible.

Here’s a basic implementation of an implicit wait in Python:

from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(10) # 10 seconds implicit wait # Perform actions or find elements element = driver.find_element_by_id("element_id") element.click() driver.quit()

In this example, the WebDriver will wait for up to 10 seconds before throwing an exception if it can’t find the element.

Explicit Wait: Precise Waiting for Specific Conditions

On the other hand, explicit wait is a more specific and controlled way of waiting for elements. Unlike implicit waits, explicit waits target a particular condition to be met before proceeding. You can define the condition and maximum waiting time for a specific element, making your script more precise and efficient.

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()

# Explicit wait for up to 10 seconds until the element is clickable
element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "element_id"))
)
element.click()

driver.quit()

In this example, the script waits for up to 10 seconds for the element to be clickable. If the condition is not met within the specified time, a TimeoutException is raised.

Key Differences at a Glance

  • Scope:
    • Implicit wait is applied globally to the WebDriver instance for all elements.
    • Explicit wait is applied to a specific element based on defined conditions.
  • Granularity:
    • Implicit wait is less granular as it applies a blanket wait for all elements.
    • Explicit wait allows for more precise control by defining conditions for specific elements.
  • Time Management:
    • Implicit wait waits for a defined time before throwing an exception.
    • Explicit wait waits for a condition to be met within a defined time.

Understanding these differences and effectively using implicit and explicit waits in your Selenium scripts will greatly enhance your automation efforts, leading to more robust and reliable test automation.

Happy testing!

Leave a Comment

Scroll to Top