Why to Use Wait: Most of the web apps are using AJAX techniques, This makes page element is loaded to browser may load at different time intervals.Using waits, we can solve this issue as Wait provides the time interval and some conditions to locate element.
Hence we can avoid the ElementNotVisibleException exception.
Selenium Webdriver provides two types of waits:
1.Implicit
2.Explicit.
Explicit Waits:-
An explicit wait makes WebDriver to wait for a certain condition to occur before proceeding further with executions. There convenience methods provided that help you write code that will wait only as long as required. Using WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 driver = webdriver.Firefox() driver.get("<a href="http://demoaut.com/")">http://demoaut.com/")</a> try: userElement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "userName"))) passElement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "password"))) submitElement = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "login"))) # login userElement.send_keys("testName") passElement.send_keys("testPass") submitElement.click() finally: driver.quit() |
Below is the Expected Conditions that are used when automating web browsers.
-title_is
-title_contains
-presence_of_element_located
-visibility_of_element_located
-visibility_of
-presence_of_all_elements_located
-text_to_be_present_in_element
-text_to_be_present_in_element_value
-frame_to_be_available_and_switch_to_it
-invisibility_of_element_located etc.
Implicit Waits:-
Its used to tell WebDriver to search the DOM for a certain amount of time to find an element or elements if they are not immediately available.
In the next blog, We will see the Page Object and other important topics in Python.
Thank’s for sharing your knowledge. It will help us to solving problems related to automation testing using selenium web Driver.
My Pleasure 🙂