|
| 1 | +.. _waits: |
| 2 | + |
| 3 | +Waits |
| 4 | +----- |
| 5 | + |
| 6 | +These days most of the web apps are using AJAX techniques. When a |
| 7 | +page is loaded to browser, the elements within that page may load at |
| 8 | +different time intervals. This makes locating elements difficult, if |
| 9 | +the element is not present in the DOM, it will raise |
| 10 | +`ElementNotVisibleException` exception. Using waits, we can solve |
| 11 | +this issue. Waiting provides some time interval between actions |
| 12 | +performed - mostly locating element or any other operation with the |
| 13 | +element. |
| 14 | + |
| 15 | +Selenium webdriver two types of waits - implicit & explicit. An |
| 16 | +explicit wait makes WebDriver to wait for a certain condition to occur |
| 17 | +before proceeding further with executions. An implicit wait makes |
| 18 | +WebDriver to poll the DOM for a certain amount of time when trying to |
| 19 | +locate an element. |
| 20 | + |
| 21 | + |
| 22 | +Explicit Waits |
| 23 | +~~~~~~~~~~~~~~ |
| 24 | + |
| 25 | +An explicit waits is code you define to wait for a certain condition |
| 26 | +to occur before proceeding further in the code. The worst case of |
| 27 | +this is time.sleep(), which sets the condition to an exact time period |
| 28 | +to wait. There are some convenience methods provided that help you |
| 29 | +write code that will wait only as long as required. WebDriverWait in |
| 30 | +combination with ExpectedCondition is one way this can be |
| 31 | +accomplished. |
| 32 | + |
| 33 | +:: |
| 34 | + |
| 35 | + from selenium import webdriver |
| 36 | + from selenium.webdriver.common.by import By |
| 37 | + from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 |
| 38 | + from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0 |
| 39 | + |
| 40 | + ff = webdriver.Firefox() |
| 41 | + ff.get("http://somedomain/url_that_delays_loading") |
| 42 | + try: |
| 43 | + element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement"))) |
| 44 | + finally: |
| 45 | + ff.quit() |
| 46 | + |
| 47 | + |
| 48 | +This waits up to 10 seconds before throwing a TimeoutException or if |
| 49 | +it finds the element will return it in 0 - 10 seconds. WebDriverWait |
| 50 | +by default calls the ExpectedCondition every 500 milliseconds until it |
| 51 | +returns successfully. A successful return is for ExpectedCondition |
| 52 | +type is Boolean return true or not null return value for all other |
| 53 | +ExpectedCondition types. |
| 54 | + |
| 55 | +*Expected Conditions* |
| 56 | + |
| 57 | +There are some common conditions that are frequently come across when |
| 58 | +automating web browsers. Listed below are Implementations of |
| 59 | +each. Selenium Python binding provides some convienence methods so you |
| 60 | +don't have to code an expected_condition class yourself or create your |
| 61 | +own utility package for them. |
| 62 | + |
| 63 | + title_is |
| 64 | + title_contains |
| 65 | + presence_of_element_located |
| 66 | + visibility_of_element_located |
| 67 | + visibility_of |
| 68 | + presence_of_all_elements_located |
| 69 | + text_to_be_present_in_element |
| 70 | + text_to_be_present_in_element_value |
| 71 | + frame_to_be_available_and_switch_to_it |
| 72 | + invisibility_of_element_located |
| 73 | + element_to_be_clickable - it is Displayed and Enabled. |
| 74 | + staleness_of |
| 75 | + element_to_be_selected |
| 76 | + element_located_to_be_selected |
| 77 | + element_selection_state_to_be |
| 78 | + element_located_selection_state_to_be |
| 79 | + alert_is_present |
| 80 | + |
| 81 | +:: |
| 82 | + |
| 83 | + from selenium.webdriver.support import expected_conditions as EC |
| 84 | + |
| 85 | + wait = WebDriverWait(driver, 10) |
| 86 | + element = wait.until(EC.element_to_be_clickable((By.Id,'someid'))) |
| 87 | + |
| 88 | +The expected_conditions module contains a set of predefined conditions |
| 89 | +to use with WebDriverWait. |
| 90 | + |
| 91 | + |
| 92 | +Implicit Waits |
| 93 | +~~~~~~~~~~~~~~ |
| 94 | + |
| 95 | +An implicit wait is to tell WebDriver to poll the DOM for a certain |
| 96 | +amount of time when trying to find an element or elements if they are |
| 97 | +not immediately available. The default setting is 0. Once set, the |
| 98 | +implicit wait is set for the life of the WebDriver object instance. |
| 99 | + |
| 100 | +:: |
| 101 | + |
| 102 | + from selenium import webdriver |
| 103 | + |
| 104 | + ff = webdriver.Firefox() |
| 105 | + ff.implicitly_wait(10) # seconds |
| 106 | + ff.get("http://somedomain/url_that_delays_loading") |
| 107 | + myDynamicElement = ff.find_element_by_id("myDynamicElement") |
| 108 | + |
| 109 | + |
0 commit comments