Sobes.tech
Middle

Tell me about different types of waits in Selenium.

sobes.tech AI

Answer from AI

In Selenium WebDriver, different types of waits are used to synchronize script actions with page loading.

  1. Implicit Wait: Sets a maximum time for Selenium to try to find an element on the page before throwing a NoSuchElementException. It applies globally to all element search operations after its set.

    from selenium import webdriver
    
    # Creating a driver instance
    driver = webdriver.Chrome()
    
    # Setting implicit wait (10 seconds)
    driver.implicitly_wait(10)
    
    # Subsequent element search operations will use this wait
    # driver.get("http://example.com")
    # element = driver.find_element_by_id("some_id")
    
    # Closing the driver
    # driver.quit()
    
  2. Explicit Wait: Sets a maximum time to wait for a specific condition before throwing a TimeoutException. It is applied to a particular element or condition. To use it, the WebDriverWait class and one of the predefined or custom "expected conditions" are required.

    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
    
    # Creating a driver instance (implicit wait is not needed here, 
    # if explicit wait is used for specific elements)
    driver = webdriver.Chrome()
    
    # driver.get("http://example.com")
    
    # Setting explicit wait (10 seconds) for element presence
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "some_id"))
        )
        # Further actions with the element
        # element.click()
    except:
        # Handling exception if element is not found within 10 seconds
        print("Element not found within 10 seconds")
    
    # Setting explicit wait for element to be clickable
    try:
        clickable_element = WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, "//button[@id='submit']"))
        )
        # clickable_element.click()
    except:
        print("Element did not become clickable within 15 seconds")
    
    # Closing the driver
    # driver.quit()
    
  3. Fluent Wait: Similar to explicit wait, it allows setting a maximum wait time and condition. Additionally, it allows specifying a polling interval for condition checks and a list of exceptions to ignore during waiting.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import FluentWait
    from selenium.common.exceptions import NoSuchElementException
    
    # Creating a driver instance
    driver = webdriver.Chrome()
    
    # driver.get("http://example.com")
    
    # Setting fluent wait
    wait = FluentWait(driver, timeout=30, poll_frequency=5,
                      ignored_exceptions=[NoSuchElementException])
    
    try:
        element = wait.until(lambda driver: driver.find_element(By.ID, "some_id"))
        # Further actions with the element
        # element.click()
    except:
        print("Element not found using fluent wait")
    
    # Closing the driver
    # driver.quit()
    

Comparison:

Wait Type Application Condition Flexibility Usage
Implicit Global Element search Low (only time) Simple setup for all elements
Explicit Specific Defined condition High (time, condition) Waiting for specific element states
Fluent Specific Defined condition Highest (time, condition, interval, ignore exceptions) Complex wait scenarios with error handling