1. 程式人生 > >Selenium2+python自動化42-判斷元素(expected_conditions)

Selenium2+python自動化42-判斷元素(expected_conditions)

.com boolean als ESS 返回 sent XA lis alt

前言

經常有小夥伴問,如何判斷一個元素是否存在,如何判斷alert彈窗出來了,如何判斷動態的元素等等一系列的判斷,在selenium的expected_conditions模塊收集了一系列的場景判斷方法,這些方法是逢面試必考的!!!

expected_conditions一般也簡稱EC,本篇先介紹下有哪些功能,後續更新中會單個去介紹。

一、功能介紹和翻譯

title_is: 判斷當前頁面的title是否完全等於(==)預期字符串,返回布爾值

title_contains : 判斷當前頁面的title是否包含預期字符串,返回布爾值

presence_of_element_located : 判斷某個元素是否被加到了dom樹裏,並不代表該元素一定可見

visibility_of_element_located : 判斷某個元素是否可見. 可見代表元素非隱藏,並且元素的寬和高都不等於0

visibility_of : 跟上面的方法做一樣的事情,只是上面的方法要傳入locator,這個方法直接傳定位到的element就好了

presence_of_all_elements_located : 判斷是否至少有1個元素存在於dom樹中。舉個例子,如果頁面上有n個元素的class都是‘column-md-3‘,那麽只要有1個元素存在,這個方法就返回True

text_to_be_present_in_element : 判斷某個元素中的text是否 包含 了預期的字符串

text_to_be_present_in_element_value : 判斷某個元素中的value屬性是否 包含 了預期的字符串

frame_to_be_available_and_switch_to_it : 判斷該frame是否可以switch進去,如果可以的話,返回True並且switch進去,否則返回False

invisibility_of_element_located : 判斷某個元素中是否不存在於dom樹或不可見

element_to_be_clickable : 判斷某個元素中是否可見並且是enable的,這樣的話才叫clickable

staleness_of : 等某個元素從dom樹中移除,註意,這個方法也是返回True或False

element_to_be_selected : 判斷某個元素是否被選中了,一般用在下拉列表

element_selection_state_to_be : 判斷某個元素的選中狀態是否符合預期

element_located_selection_state_to_be : 跟上面的方法作用一樣,只是上面的方法傳入定位到的element,而這個方法傳入locator

alert_is_present : 判斷頁面上是否存在alert

二、查看源碼和註釋

1.打開python裏這個目錄l可以找到:Lib\site-packages\selenium\webdriver\support\expected_conditions.py

技術分享圖片

  1 from selenium.common.exceptions import NoSuchElementException
  2 from selenium.common.exceptions import NoSuchFrameException
  3 from selenium.common.exceptions import StaleElementReferenceException
4 from selenium.common.exceptions import WebDriverException 5 from selenium.common.exceptions import NoAlertPresentException 6 7 """ 8 * Canned "Expected Conditions" which are generally useful within webdriver 9 * tests. 10 """ 11 12 13 class title_is(object): 14 """An expectation for checking the title of a page.
15 title is the expected title, which must be an exact match 16 returns True if the title matches, false otherwise.""" 17 def __init__(self, title): 18 self.title = title 19 20 def __call__(self, driver): 21 return self.title == driver.title 22 23 24 class title_contains(object): 25 """ An expectation for checking that the title contains a case-sensitive 26 substring. title is the fragment of title expected 27 returns True when the title matches, False otherwise 28 """ 29 def __init__(self, title): 30 self.title = title 31 32 def __call__(self, driver): 33 return self.title in driver.title 34 35 36 class presence_of_element_located(object): 37 """ An expectation for checking that an element is present on the DOM 38 of a page. This does not necessarily mean that the element is visible. 39 locator - used to find the element 40 returns the WebElement once it is located 41 """ 42 def __init__(self, locator): 43 self.locator = locator 44 45 def __call__(self, driver): 46 return _find_element(driver, self.locator) 47 48 49 class visibility_of_element_located(object): 50 """ An expectation for checking that an element is present on the DOM of a 51 page and visible. Visibility means that the element is not only displayed 52 but also has a height and width that is greater than 0. 53 locator - used to find the element 54 returns the WebElement once it is located and visible 55 """ 56 def __init__(self, locator): 57 self.locator = locator 58 59 def __call__(self, driver): 60 try: 61 return _element_if_visible(_find_element(driver, self.locator)) 62 except StaleElementReferenceException: 63 return False 64 65 66 class visibility_of(object): 67 """ An expectation for checking that an element, known to be present on the 68 DOM of a page, is visible. Visibility means that the element is not only 69 displayed but also has a height and width that is greater than 0. 70 element is the WebElement 71 returns the (same) WebElement once it is visible 72 """ 73 def __init__(self, element): 74 self.element = element 75 76 def __call__(self, ignored): 77 return _element_if_visible(self.element) 78 79 80 def _element_if_visible(element, visibility=True): 81 return element if element.is_displayed() == visibility else False 82 83 84 class presence_of_all_elements_located(object): 85 """ An expectation for checking that there is at least one element present 86 on a web page. 87 locator is used to find the element 88 returns the list of WebElements once they are located 89 """ 90 def __init__(self, locator): 91 self.locator = locator 92 93 def __call__(self, driver): 94 return _find_elements(driver, self.locator) 95 96 97 class visibility_of_any_elements_located(object): 98 """ An expectation for checking that there is at least one element visible 99 on a web page. 100 locator is used to find the element 101 returns the list of WebElements once they are located 102 """ 103 def __init__(self, locator): 104 self.locator = locator 105 106 def __call__(self, driver): 107 return [element for element in _find_elements(driver, self.locator) if _element_if_visible(element)] 108 109 110 class text_to_be_present_in_element(object): 111 """ An expectation for checking if the given text is present in the 112 specified element. 113 locator, text 114 """ 115 def __init__(self, locator, text_): 116 self.locator = locator 117 self.text = text_ 118 119 def __call__(self, driver): 120 try: 121 element_text = _find_element(driver, self.locator).text 122 return self.text in element_text 123 except StaleElementReferenceException: 124 return False 125 126 127 class text_to_be_present_in_element_value(object): 128 """ 129 An expectation for checking if the given text is present in the element‘s 130 locator, text 131 """ 132 def __init__(self, locator, text_): 133 self.locator = locator 134 self.text = text_ 135 136 def __call__(self, driver): 137 try: 138 element_text = _find_element(driver, 139 self.locator).get_attribute("value") 140 if element_text: 141 return self.text in element_text 142 else: 143 return False 144 except StaleElementReferenceException: 145 return False 146 147 148 class frame_to_be_available_and_switch_to_it(object): 149 """ An expectation for checking whether the given frame is available to 150 switch to. If the frame is available it switches the given driver to the 151 specified frame. 152 """ 153 def __init__(self, locator): 154 self.frame_locator = locator 155 156 def __call__(self, driver): 157 try: 158 if isinstance(self.frame_locator, tuple): 159 driver.switch_to.frame(_find_element(driver, 160 self.frame_locator)) 161 else: 162 driver.switch_to.frame(self.frame_locator) 163 return True 164 except NoSuchFrameException: 165 return False 166 167 168 class invisibility_of_element_located(object): 169 """ An Expectation for checking that an element is either invisible or not 170 present on the DOM. 171 172 locator used to find the element 173 """ 174 def __init__(self, locator): 175 self.locator = locator 176 177 def __call__(self, driver): 178 try: 179 return _element_if_visible(_find_element(driver, self.locator), False) 180 except (NoSuchElementException, StaleElementReferenceException): 181 # In the case of NoSuchElement, returns true because the element is 182 # not present in DOM. The try block checks if the element is present 183 # but is invisible. 184 # In the case of StaleElementReference, returns true because stale 185 # element reference implies that element is no longer visible. 186 return True 187 188 189 class element_to_be_clickable(object): 190 """ An Expectation for checking an element is visible and enabled such that 191 you can click it.""" 192 def __init__(self, locator): 193 self.locator = locator 194 195 def __call__(self, driver): 196 element = visibility_of_element_located(self.locator)(driver) 197 if element and element.is_enabled(): 198 return element 199 else: 200 return False 201 202 203 class staleness_of(object): 204 """ Wait until an element is no longer attached to the DOM. 205 element is the element to wait for. 206 returns False if the element is still attached to the DOM, true otherwise. 207 """ 208 def __init__(self, element): 209 self.element = element 210 211 def __call__(self, ignored): 212 try: 213 # Calling any method forces a staleness check 214 self.element.is_enabled() 215 return False 216 except StaleElementReferenceException: 217 return True 218 219 220 class element_to_be_selected(object): 221 """ An expectation for checking the selection is selected. 222 element is WebElement object 223 """ 224 def __init__(self, element): 225 self.element = element 226 227 def __call__(self, ignored): 228 return self.element.is_selected() 229 230 231 class element_located_to_be_selected(object): 232 """An expectation for the element to be located is selected. 233 locator is a tuple of (by, path)""" 234 def __init__(self, locator): 235 self.locator = locator 236 237 def __call__(self, driver): 238 return _find_element(driver, self.locator).is_selected() 239 240 241 class element_selection_state_to_be(object): 242 """ An expectation for checking if the given element is selected. 243 element is WebElement object 244 is_selected is a Boolean." 245 """ 246 def __init__(self, element, is_selected): 247 self.element = element 248 self.is_selected = is_selected 249 250 def __call__(self, ignored): 251 return self.element.is_selected() == self.is_selected 252 253 254 class element_located_selection_state_to_be(object): 255 """ An expectation to locate an element and check if the selection state 256 specified is in that state. 257 locator is a tuple of (by, path) 258 is_selected is a boolean 259 """ 260 def __init__(self, locator, is_selected): 261 self.locator = locator 262 self.is_selected = is_selected 263 264 def __call__(self, driver): 265 try: 266 element = _find_element(driver, self.locator) 267 return element.is_selected() == self.is_selected 268 except StaleElementReferenceException: 269 return False 270 271 272 class alert_is_present(object): 273 """ Expect an alert to be present.""" 274 def __init__(self): 275 pass 276 277 def __call__(self, driver): 278 try: 279 alert = driver.switch_to.alert 280 alert.text 281 return alert 282 except NoAlertPresentException: 283 return False 284 285 286 def _find_element(driver, by): 287 """Looks up an element. Logs and re-raises ``WebDriverException`` 288 if thrown.""" 289 try: 290 return driver.find_element(*by) 291 except NoSuchElementException as e: 292 raise e 293 except WebDriverException as e: 294 raise e 295 296 297 def _find_elements(driver, by): 298 try: 299 return driver.find_elements(*by) 300 except WebDriverException as e: 301 raise e

本篇的判斷方法和場景很多,先貼出來,後面慢慢更新,詳細講解每個的功能的場景和用法。

這些方法是寫好自動化腳本,提升性能的必經之路,想做好自動化,就得熟練掌握。

Selenium2+python自動化42-判斷元素(expected_conditions)