标签:例子 .com thrown XA instance 下拉 elf res ini
前言
经常有小伙伴问,如何判断一个元素是否存在,如何判断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)
标签:例子 .com thrown XA instance 下拉 elf res ini
原文地址:https://www.cnblogs.com/jason89/p/8997741.html