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 selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions from selenium.webdriver.common.by import By driver = webdriver.Chrome(executable_path=r'D:/PATH/chromedriver.exe'); driver.get("https://chercher.tech/practice/explicit-wait"); wait = WebDriverWait(driver, 30) # timeout in seconds -> 30 wait.until(expected_conditions.element_to_be_clickable((By.XPATH, "//button[@id='btn1']")));
WebdriverWait comes makes the driver object wait but we have to use the condition present in the ExpectedConditions.
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.
driver.get("https://chercher.tech/practice/explicit-wait"); wait = WebDriverWait(driver, 30) wait.until(expected_conditions.title_is("WebdriverWait Practice"))
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) wait.until(expected_conditions.title_contains("Practice"))
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) wait.until(expected_conditions.presence_of_all_elements_located ((By.ID, "id of element")))
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) wait.until(expected_conditions.visibility_of_all_elements_located ((By.ID, "id of element")))
Waits for the given time for the given text to be present in the specified element.
wait = WebDriverWait(driver, 30) wait.until(expected_conditions.text_to_be_present_in_element ((By.ID, "id of element"), "text to be present"))
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) wait.until(expected_conditions.frame_to_be_available_and_switch_to_it ((By.ID, "id of element")))
Waits for the element to be visible and enabled such that the user can click it.
wait = WebDriverWait(driver, 30) wait.until(expected_conditions.element_to_be_clickable ((By.ID, "id of element")))
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) wait.until(expected_conditions.staleness_of ((By.ID, "id of element")))
Waits till an element is get selected. Applicable for dropdowns and Checkboxes, radio buttons.
wait = WebDriverWait(driver, 30) wait.until(expected_conditions.element_located_to_be_selected ((By.ID, "id of element")))
Few more conditions are present but explore yourself, so you will have a better understanding.
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.
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: break sleep(5) i = i +5 print(page_state) # instead of printing use the variable for other purposes
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() browser.get("url") delay = 3 # seconds try: 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!")
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.