Wait

in selenium, waits are used when an element takes some time to load.

Implicit Wait

driver = webdriver.Chrome()
driver.implicitly_wait(3)
  • Implicit wait is defined globally. Meaning, if we give 3 seconds, all the web element interaction we perform using selenium will abide by it.

  • Let’s say you give driver.implicitly_wait(3)

    Now, since this is globally defined, all driver.find_element() will try to find the element for 3 seconds. If the element is found, great we move on to the next line.

    Else, after 3 seconds the failure error message is returned.

Explicit Wait

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

driver = webdriver.Chrome()

wait = WebDriverWait(driver, 9)
wait.until(expected_conditions.presence_of_element_located((By.XPATH, "//div[text='Hello']")))
  • Explicit waits are special waits for those elements that take way longer compared to the other elements on the page.

  • So, explicit waits are for individual elements, therefore its not globally defined.

Fluent Wait

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

driver = webdriver.Chrome()

wait = WebDriverWait(driver, timeout=10, poll_frequency=2, ignored_exceptions=[NoSuchElementException])
button = wait.until(EC.element_to_be_clickable((By.ID, "submit")))
'''
waits up to 10 seconds
checks every 2 seconds
ignores NoSuchElementException error while waiting
clicks the button as soon as its clickable.
'''
  • It is a more customizable version of explicit wait that allows more options that we can control like polling intervals, and the ability to ignore certain exceptions during the wait.

  • It polls the DOM at a user defined interval for specified condition with an option to ignore exceptions.

  • When we give an exception to ignore (example: NoSuchElementException), and we don’t find a particular element using the fluent wait, we are returned with a timeout error.

Updated on