Skip to content
All notes
34SeleniumAutomationSelenium

What are different types of waits in Selenium?

Waits in Selenium are used to handle synchronization issues when elements take time to load, preventing test failures due to timing issues.

Types of Waits

1. Implicit Wait:

  • Global wait applied to all elements
  • Waits for specified time before throwing NoSuchElementException
  • Set once and applies throughout the test
  • Not recommended for modern tests
# Python
driver.implicitly_wait(10)  # Wait up to 10 seconds

// Java
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

2. Explicit Wait:

  • Wait for specific condition on specific element
  • More flexible and recommended
  • Can wait for various conditions (visibility, clickability, etc.)
  • Throws TimeoutException if condition not met
# Python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "myElement")))
element = wait.until(EC.element_to_be_clickable((By.ID, "button")))

3. Fluent Wait:

  • More customizable than explicit wait
  • Can define polling frequency
  • Can ignore specific exceptions
# Python
from selenium.webdriver.support.ui import WebDriverWait

wait = WebDriverWait(driver, timeout=10, poll_frequency=1,
                     ignored_exceptions=[NoSuchElementException])
element = wait.until(EC.presence_of_element_located((By.ID, "myElement")))

Common Expected Conditions

  • presence_of_element_located - Element present in DOM
  • visibility_of_element_located - Element visible on page
  • element_to_be_clickable - Element visible and enabled
  • invisibility_of_element - Element not visible
  • text_to_be_present_in_element - Specific text in element
  • alert_is_present - Alert is present

Best Practices

  • Prefer explicit waits over implicit waits
  • Avoid Thread.sleep() (hard waits)
  • Use appropriate expected conditions
  • Set reasonable timeout values
  • Don’t mix implicit and explicit waits