Python WebdriverWait

WebdriverWait makes the selenium wait till certain conditions are met or till the maximum time limit is reached before throwing an Exception. We can reuse the WebdriverWait object once we create it. WebdriverWait also called as ExplicitWait.

The WebdriverWait will be applicable for only one line, we have to use WebdriverWait with ExpectedConditions class. WebdriverWait does not have any effect on findElement and findElements.

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.

#Syntax : 
wait=WebDriverWait( driver, timeoutInSeconds);

The below code waits for the element to become clickable

from selenium import webdriver
from import WebDriverWait
from import expected_conditions
from import By

driver = webdriver.Chrome(executable_path=r'D:/PATH/chromedriver.exe');
wait = WebDriverWait(driver, 30) # timeout in seconds -> 30
wait.until(expected_conditions.element_to_be_clickable((By.XPATH, "//button[@id='btn1']")));

Different Expected Conditions with WebdriverWait

WebdriverWait comes makes the driver object wait but we have to use the condition present in the ExpectedConditions.

Selenium throws selenium.common.exceptions.TimeoutException exception if the given condition is not met.

title_is :

Selenium waits for the title to become the given value, if the title does not match with the given string within the time limit then selenium throws TimeoutException.

wait = WebDriverWait(driver, 30)
wait.until(expected_conditions.title_is("WebdriverWait Practice"))
title_contains :

title_contains expects the webpage title should contain the given keyword if the given keyword is not matched selenium throws Timeout exception

wait = WebDriverWait(driver, 3)
presence_of_element_located :

Selenium waits for an element to be present on the DOM of a page. This function will not throw any error even if the element is not visible on UI. presence_of_element_located function returns the WebElement once it is located otherwise throws an exception.

wait = WebDriverWait(driver, 30)
           ((By.ID, "id of element")))
  • presence_of_all_elements_located : Waits for the presence of all the elements.
visibility_of_element_located :

Selenium waits for an element to present in the DOM of a page and visible on UI. Visibility means that the elements are not only displayed but also have a height and width that is greater than 0.

wait = WebDriverWait(driver, 30)
           ((By.ID, "id of element")))
  • invisibility_of_element_located : An Expectation for checking that an element is either invisible or not present in the DOM. locator used to find the element
text_to_be_present_in_element :

Waits for the given time for the given text to be present in the specified element.

wait = WebDriverWait(driver, 30)
           ((By.ID, "id of element"), "text to be present"))
frame_to_be_available_and_switch_to_it :

Selenium waits for the target frame to be available to switch to. If the frame is available driver control switches to frame.

wait = WebDriverWait(driver, 30)
           ((By.ID, "id of element")))
element_to_be_clickable :

Waits for the element to be visible and enabled such that the user can click it.

wait = WebDriverWait(driver, 30)
           ((By.ID, "id of element")))
staleness_of :

Wait until an element is no longer attached to the DOM, in other for the element to get refreshed. Returns False if the element is still attached to the DOM, true otherwise.

wait = WebDriverWait(driver, 30)
           ((By.ID, "id of element")))
element_to_be_selected :

Waits till an element is get selected. Applicable for dropdowns and Checkboxes, radio buttons.

wait = WebDriverWait(driver, 30)
           ((By.ID, "id of element")))

Few more conditions are present but explore yourself, so you will have a better understanding.

Wait until the page to load in Selenium python

Using Get

By default, selenium python waits till the page load is complete when we use the get() function.


In case if your elements load after some time or if your application loads elements based on a file receives from the server then, the get() method might give control to next before loading completely.

Using javascript

document.readyState in javascript return the current state of the page, it will return one of five values:

  • uninitialized - Has not started loading yet
  • loading - the webpage Is loading
  • loaded - the webpage has been loaded
  • interactive - Has loaded enough and the user can interact with it
  • complete - the webpage is fully loaded

We have to retry if the page is not loaded, I am trying to retry for 60 seconds.

print("Checking id webpage loaded"+self.driver.current_url)
page_state = driver.execute_script('return document.readyState;')
i = 60 # seconds
while page_state is not "complete":
    if i>60:
    i = i +5
print(page_state) # instead of printing use the variable for other purposes
Using WebdriverWait

When you are not sure how many items going to load and if the items loaded from ajax then try to use the explicit wait.

browser = webdriver.Firefox()
delay = 3 # seconds
    targetElement = WebDriverWait(browser, delay).until(
        expected_conditions.presence_of_element_located((By.ID, "element's id"))
    print("all elements loaded!")
except TimeoutException:
    print ("Loading took too much time!")
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Edmund.Rudy
    A strong-willed person, his world is full of infinite possibilities
  • Reply