What is the explicit wait in Selenium with python?

While working with Selenium, there may be situations when we see that after page load action by the browser, the web elements are getting loaded at various intervals of time.

This type of situation leads to syncing problems between Selenium and the web element on the page. The identification of elements does not happen due to the absence of that element in DOM. The exception like ElementNotVisibleException is thrown due to this.

The wait concept in Selenium overcomes this problem and gives a delay between elements identification and actions performed on them. The explicit wait is applied not to all but to a specific element on the page. It is actually a condition which has to be met before moving to the next step of the test case.

The explicit wait is also a dynamic in nature which means the wait will be there as long as necessary. Thus if the wait time is five seconds and our given condition is satisfied at the third second, we need not halt the execution for the next two seconds.

The webdriverWait class along with expected_conditions is used to create an explicit wait. The webdriverWait class can call the ExpectedCondition after every 500ms by default for checking if the condition is met.


w = WebDriverWait(driver, 7)

The expected conditions commonly used in explicit wait are listed below −

  • title_contains
  • visibility_of_element_located
  • presence_of_element_located
  • title_is
  • visibility_of
  • element_selection_state_to_be
  • presence_of_all_elements_located
  • element_located_to_be_selected
  • alert_is_present
  • element_located_selection_state_to_be
  • staleness_of
  • element_to_be_clickable
  • invisibility_of_element_located
  • frame_to_be_available_and_switch_to_it
  • text_to_be_present_in_element_value
  • text_to_be_present_in_element
  • element_to_be_selected

Explicit is more difficult to implement than implicit wait, however it does not slow down the execution and is applicable to a specific element on a page.


Code Implementation with explicit wait.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then
#invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
#setting implicit wait 10 seconds
# to maximize the browser window
#to refresh the browser
# identifying the link with the help of link text locator
driver.find_element_by_link_text("Search Students").click()
CSS_SELECTOR, "input[name = 'txtFilterLocation']")))
#to close the browser