[python] 2024-10-03 圈点435
摘要:selenium之expected_conditions模块
expected_conditions模块的网址介绍:
https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions
expected_conditions模块运用时,一般先通过as关键字将 expected_conditions重命名为ec,再调用预期条件方法来判断。
expected_conditions模块传参是locator 或 element:
locator 使用By方法定位元素的元组
element 要获取的WebElement元素对象
expected_conditions 类提供的预期条件 判断方法大致如下:
1.title_is(‘title’) 判断当前页面的title是否完全等于(==)预期字符串,
返回布尔值:如果完全一致,返回Ture,否则返回Flase
和显式等待结合后,条件符合返回True,不符合 显式等待+报错
WebDriverWait(self.driver, 10).until(ec.title_is('百度一下,你就知道'), '失败') 打印结果是True
2.title_contains(‘title’) 判断当前页面的title是否包含预期字符串,
返回布尔值:True/False;如果完全一致,返回Ture,否则返回Flase
和显式等待结合后,条件符合返回True,不符合 显式等待+报错
WebDriverWait(self.driver, 10).until(ec.title_contains('知道'), '失败') 打印结果是True
3.presence_of_element_located(locator) 判断某个locator元素是否被加到DOM树里,并不代表该元素一定可见(元素是隐藏的)
如果定位到就返回WebElement,找不到元素 报错Message: no such element: Unable to locate element
WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.ID, 'su'))) 打印结果是WebElement
和显式等待结合后,如果定位到就返回WebElement,找不到元素 显式等待+报错
4.visibility_of_element_located(locator) 判断某个locator元素是否可见。
可见代表非隐藏、可显示,并且元素的宽和高都大于0
如果定位到就返回WebElement,找不到元素 报错Message: no such element: Unable to locate element
WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.ID, 'su'))) 打印结果是WebElement
和显式等待结合后,如果定位到就返回WebElement,找不到元素 显式等待+报错
5.visibility_of(element) 判断element元素是否可见。
Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
直接传element;
如果可见就返回这个WebElement元素;不可见就返回 False;找不到的 报错的是 no such element: Unable to locate element
和显式等待结合后,不可见的 显式等待+报错;可见的返回WebElement;不存在的报错 Message: no such element: Unable to locate element
WebDriverWait(driver, 10).until(ec.visibility_of(driver.find_element(By.ID, 'su')))
WebDriverWait(driver, 10).until(ec.visibility_of(driver.find_element_by_link_text('高级搜索123')))
如果是第二个 实际不会到显式等待
10.invisibility_of_element_located(locator) 判断某个locator元素是否隐藏DOM树(隐藏、不存在、可见)
# 可见的元素返回False,不存在的元素见返回True;隐藏的元素返回WebElement
# 和显式等待结合后,可见的元素显式等待+报错;隐藏的元素会返回WebElement;不存在的元素 返回True
# WebDriverWait(self.driver, 10).until(ec.invisibility_of_element_located((By.ID, 'toStationText')), '失败')
7.text_to_be_present_in_element(locator,text) 判断某个locator元素的text是否包含了预期的字符串
# 请先确定此元素的text [元素文本]
# 符合就返回True,不符合返回False;
# 和显式等待结合后,符合返回True;不符合,显式等待+报错
WebDriverWait(self.driver, 10).until(ec.text_to_be_present_in_element((By.PARTIAL_LINK_TEXT, '多产'), '产品'), '失败')
8.text_to_be_present_in_element_value(locator,value) 判断某个元素的value属性值是否包含了预期的字符串
# 请先确定此元素的value属性值 print(abc.get_attribute('value'))
# 条件符合 返回True;条件不符合返回False
# 和显式等待结合后,返回返回True;条件不符合 显式等待+报错
# WebDriverWait(driver, 10).until(ec.text_to_be_present_in_element((By.ID, 'su'),'百度一下'))
# 等待'su'元素的value属性值是否包含'百度一下'
9.frame_to_be_available_and_switch_to_it() 判断该frame是否可以switch进去
# 如果直接传入定位方式id,可以的话就返回True并switch进去,否则返回False
# 也可传入locator元组或WebElement,可以的话就返回True并switch进去,否则报错 Message: no such element
# 和显式等待结合后,传入定位方式id、locator元组,可以的话就返回True并switch进去,否则 显式等待+报错
# 和显式等待结合后,传入WebElement,可以的话就返回True并switch进去,
# 否则找不到定位报错 selenium.common.exceptions.NoSuchElementException: Message: no such element
# WebElement因为是去定位某个元素,定位不到会报错,用不到那个显式等待的!在执行前就已经报错 找不到元素
frame的定位 driver.switch_to.frame(xxxx) xxxx可以是id属性值;或是查找到这个元素WebElement,不可以使用locator元组
WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it('frame_id'))
WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it((By.ID, 'frame_id')))
WebDriverWait(driver, 10).until(ec.frame_to_be_available_and_switch_to_it(
driver.find_element(By.ID, 'frame_id')))
11.element_to_be_clickable(locator) 判断某个locator元素是否可点击
# 元素可以被看见visibility_of_element_located()并且可以被使用is_enabled()返回WebElement;
# 找不到的元素 报错 Message: no such element: Unable to locate element
# 隐藏的元素返回 False;
# WebDriverWait 实际显式等待+ 报错的 包括 隐藏的元素和不存在的元素;可点击返回WebElement
# WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.ID, 'su')))
12.staleness_of(element) 等某个元素从DOM树中移除。传入WebElement对象
# returns False if the element is still attached to the DOM, true otherwise.
# 如果元素is_enabled()返回False;其他返回True;根本不存在的报错 Message: no such element
# 和显式等待结合后,元素is_enabled() 显式等待+报错;不然返回True;根本不存在的报错 Message: no such element
WebDriverWait(self.driver, 10).until(ec.staleness_of(self.driver.find_element_by_id('su')))
13.element_to_be_selected(element) 等待element元素是被选中,一般用在下拉列表
# 如果元素是被选中的,返回True;如果不是,返回False;定位失败的 报错 Message: no such element: Unable to locate element
# 和显式等待结合后,符合条件返回True;不符合就显式等待+报错;定位失败的 报错 Message: no such element
16.element_located_to_be_selected(locator) 等待locator元素是被选中
# 如果元素是被选中的,返回True;如果不是,返回False;元素是根本不存在的,就报错 Message: no such element: Unable to locate element
# 和显式等待结合后,符合条件的选项返回True;不符合条件的选项、和不存在的选项 都显式等待+报错
14.element_selection_state_to_be(element, is_selected) 判断某个元素的选中状态是否符合预期;
# 传入WebElement对象以及2种状态(True、False),相等返回True,否则返回False;找不到的报错 Message: no such element: Unable to locate element
# is_selected is a Boolean.所以状态一定不能输入'is_selected'
# 和显式等待结合后 符合判断条件的返回True;不符合判断条件的 显式等待+ 报错;找不到元素的报错 Message: no such element: Unable to locate element
15.element_located_selection_state_to_be(locator,is_selected) 判断某个元素的选中状态是否符合预期。
# 传入locator以及2种状态(True、False),相等返回True,否则返回False;找不到的报错 Message: no such element: Unable to locate element
# is_selected is a Boolean.所以状态一定 不能输入'is_selected'
# print(ec.element_located_selection_state_to_be(abcd10, 'is_selected')(self.driver)) # 返回 False
# 和显式等待结合后,符合状态判断条件的选项 返回True;实际 显式等待 + 报错的 包括不符合状态判断条件的选项 和不存在的选项
17.alert_is_present 判断页面是否存在alert
# 如果有就切换到alert并返回这个Alert;如果没有就返回False
# 和显式等待结合后,没有alert 就显式等待+报错;有alert 就返回这个Alert
6.presence_of_all_elements_located(locator) 判断是否至少有1个元素存在DOM树中。
# 如果定位到就返回WebElement列表,不存在的元素 返回的是 空列表;
# 和显式等待结合后,定位到就返回WebElement列表,查不到这些元素 显式等待 + 报错
# 和显式等待结合后, 符合 最少存在一个WebElement的 返回符合定位元素条件WebElement的列表,找不到元素的 显式等待+报错
WebDriverWait(driver, 10).until(ec.presence_of_all_elements_located((By.ID, 'su')))
18.visibility_of_any_elements_located 判断页面至少有一个元素可见 visible,
传入locator,一旦定位就返回 the list of located WebElements;不可见(元素隐藏 或是 完全不存在,一个都没有)返回的是 空列表;
和显式等待结合后, 符合 最少存在一个WebElement的 返回符合定位元素条件WebElement的列表,不可见(元素隐藏 或是 完全不存在的)显式等待+报错
19.visibility_of_all_elements_located 判断页面all elements存在且可见 visible
all elements are present and visible;
传入locator,全部符合的 就返回 the list of located and visible WebElements;不能全部符合的返回False;不存在的元素返回 空列表;
和显式等待结合后,符合 全部可见WebElement的 返回符合定位元素条件WebElement的列表,找不到元素的 + WebElement不能全部可见的 显式等待+报错
expected_conditions模块三种关于检查元素存在的类(示例)
class presence_of_element_located(object): """ An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. locator - used to find the element returns the WebElement once it is located """ def __init__(self, locator): self.locator = locator def __call__(self, driver): return _find_element(driver, self.locator) class visibility_of_element_located(object): """ An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. locator - used to find the element returns the WebElement once it is located and visible """ def __init__(self, locator): self.locator = locator def __call__(self, driver): try: return _element_if_visible(_find_element(driver, self.locator)) except StaleElementReferenceException: return False class visibility_of(object): """ An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. element is the WebElement returns the (same) WebElement once it is visible """ def __init__(self, element): self.element = element def __call__(self, ignored): return _element_if_visible(self.element)
注意两个点:
(A).元素可见与否
presence_of_element_located()的注释:
This does not necessarily mean that the element is visible 这并不一定意味着该元素是可见的
visibility_of_element_located()的注释:
Visibility means that the element is not only displayed but also has a height and width that is greater than 0 可见性意味着元素不仅会显示,而且高度和宽度也会大于0
(B).传参及返回值
传参都是locator - used to find the element;返回的是the WebElement(略有不同)
UI的自动化测试的前提是 用户看到这个元素,所以推荐使用的是visibility_of_element_located(),示例:
def test_57j(self): """expected_conditions模块visibility_of_element_located类""" # visibility_of_element_located(locator) 判断某个locator元素是否可见(前提是存在) # 可见代表非隐藏、可显示,并且元素的宽和高都大于0 # 如果定位到就返回WebElement from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support.wait import WebDriverWait self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get("https://www.baidu.com") print('开始', time.ctime()) # 传入driver 返回一个WebElement print(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw'))(self.driver)) # 传入driver 返回一个WebElement 获取tag_name\\获取class属性值 print(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw'))(self.driver).get_attribute('class')) print('1', time.ctime()) try: print(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw111'))(self.driver)) except BaseException as e111: print(e111) # 不存在的元素 报错 Message: no such element: Unable to locate element print('2', time.ctime()) # 和显式等待结合 print(WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw')), '失败')) # 返回WebElement print(WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kw')), '失败').get_attribute('class')) print('3', time.ctime()) try: WebDriverWait(self.driver, 10).until(ec.visibility_of_element_located((By.CSS_SELECTOR, 'input#kkk1111')), '失败') except BaseException as e222: print(e222) # 不存在的元素,显式等待+报错 print('4', time.ctime()) time.sleep(1) self.driver.quit()