『心善渊』Selenium3.0基础 — 24、Selenium的expected_conditions模块详细介绍

提示:下文中expected_conditions模块有时简称为EC模块。

1、EC模块介绍

  1. expected_conditions是Selenium的一个模块,主要用于对页面元素的加载进行判断,包括元素是否存在,可点击等等。
  2. expected_conditions模块的使用场景一般有两种:
    • 直接在断言中使用 。
    • WebDriverWait配合使用,显示等待页面上元素出现或者消失。
  3. 一般情况下,我们在使用expected_conditions模块时,都会对其进行重命名,通过as关键字对其重命名为EC
    from selenium.webdriver.support import expected_conditions as EC
    

2、EC模块常用类

  1. title_is(title)方法:用于判断网页title是否是特定文本(英文区分大小写),若完全相同则返回True,否则返回False。
  2. title_contains(title)方法:用于判断网页title是否包含特定文本(英文区分大小写),若包含则返回True,不包含返回False。
  3. presence_of_element_located(locator)方法(重点,常用):用于判断一个元素存在于页面DOM树中,存在则返回元素本身,不存在则报错。
    locator为一个(by, path)元祖,这个(by, path)by是Selenium的一个类selenium.webdriver.common.by.By,包括CLASS_NAMECSS_SELECTORIDLINK_TEXT,NAMEPARTIAL_LINK_TEXTTAG_NAMEXPATH,和我们8种基本元素定位中使用的方法相同。
  4. presence_of_all_elements_located(locator)方法:用于判断定位的元素范围内,至少有一个元素存在于页面当中,存在则以list形式返回元素本身,不存在则报错。
  5. visibility_of_element_located(locator)方法:用于判断特定元素是否存在于DOM树中并且可见,可见意为元素的高和宽都大于0,元素存在返回元素本身,否则返回False。
    这个类和presence_of_element_located(locator)有点像,但后者只强调元素存在于DOM树中,可见不可见无所谓,而前者要求必须高和宽必须都大于0,因此后者在性能上会稍微快一点点。
  6. visibility_of(element)方法:visibility_of(element)同上面visibility_of_element_located(locator),不过参数从locator的元组变为元素。
  7. invisibility_of_element_located(locator)方法:判断元素是否隐藏。
  8. text_to_be_present_in_element(locator,text)方法:判断给定文本是否出现在特定元素中,若存在则返回True,不存在返回False。
  9. text_to_be_present_in_element_value(locator,text)方法:判断某文本是否是存在于特定元素的value值中,存在则返回True,不存在则返回False,对于查看没有value值的元素,也会返回False。
  10. frame_to_be_available_and_switch_to_it(locator)方法:判断某个frame是否可以切换过去,若可以则切换到该frame,否则返回False 。
  11. element_to_be_clickable(locator)方法:判断某元素是否可访问并且可启用,比如能够点击,若可以则返回元素本身,否则返回False。
  12. staleness_of(element)方法:判断某个元素是否不再附加于于DOM树中,不再附加的话返回True,依旧存在返回False。可以用于判断页面是否刷新了。
  13. alert_is_present方法:判断alert是否存在,若存在则切换到alert,若不存在则返回False。
  14. element_to_be_selected(element)方法:判断某元素是否被选中。
  15. element_located_to_be_selected(locator) 方法:判断某元素是否被选,locator为一个(by, path)元祖。
  16. element_selection_state_to_be (element, is_selected)方法:判断某元素的选中状态是否与预期相同,相同则返回True,不同则返回False。
  17. element_located_selection_state_to_be(locator, is_selected)方法: 判断某元素是否与预期相同,相同则返回True,不同则返回False,locator为一个(by, path)元祖。

总结:这些方法与WebDriverWait类和 until()until_not()方法组合能使用,够实现很多判断功能,如果能自己灵活封装,将会大大提高脚本的稳定性。

3、EC模块的使用

我们练习expected_conditions模块中两个功能,其他功能参照即可。

# 1.导入selenium
from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC

# 2.打开Chrome浏览器
driver = webdriver.Chrome()

# 3.打开注册A页面
url = "https://www.baidu.com"
driver.get(url)
sleep(3)

# 4.EC模块的使用
"""
1.EC模块单独使用的语法
    EC.方法(参数)(driver) 或 EC.方法(参数).__call__(driver)
2.说明
    我们可以查看title_is()源码
    class title_is(object):
        def __init__(self, title):
            self.title = title

        def __call__(self, driver):
            return self.title == driver.title
    我们可以看到,直接使用EC模块,就需要调用__call__()方法
    而__call__()方法是魔法方法,调用方式,就是上面1所说的两种方式。
"""

# 4.1 EC模块中title_is()的使用
# 判断网页title是否是特定文本
# 正确匹配
title_is_result_True = EC.title_is("百度一下,你就知道")(driver)
print("title_is结果 =", title_is_result_True)
# 错误匹配
title_is_result_False = EC.title_is("百度一下,你就知道1").__call__(driver)
print(f"title_is结果 = {title_is_result_False}")

# 4.2 EC模块中presence_of_element_located(locator)的使用
# presence_of_all_elements_located(locator)同理
# 定位元素单数
# 定位百度首页输入框
# 4.2.1编写locator(定位器)
# 正确定位
input_locator_True = ("id", "kw")
# 错误定位
input_locator_False = ("id", "kw1")

# 4.2.2 执行EC模块方法
element_located_True = EC.presence_of_element_located(input_locator_True).__call__(driver)
print("element_located结果 =", element_located_True)
# 定位不到会报错
# element_located_False = EC.presence_of_element_located(input_locator_False)(driver)


# 5.关闭浏览器
sleep(2)
driver.quit()

4、EC模块综合使用

WebDriverWait类和until()方法和EC模块这种组合的使用方法练习。

(1)title_is(title)示例

这个title_is(title)例子很简单,作为一个入门示例。

"""
1.学习目标
    掌握EC模块中title_is的使用
2.操作步骤(语法)
    2.1 EC模块通用使用方法
        EC模块通常和WebDriverWait配合使用
        WebDriverWait(driver,timeout).until(EC.方法)
    2.2 title_is(指定的标题)
        判断页面标题是否和指定的标题一致,如果一致返回True,不一致返回Palse
        result=WebDriverWait(driver,timeout).until(EC.title_is(指定的标题))
        print(result)
3.需求
    使用title_is判断百度首页的标题title
"""
# 1.导入selenium
from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# 2.打开谷歌浏览器
driver = webdriver.Chrome()

# 3.输入网址
url = "https://www.baidu.com"
driver.get(url)
sleep(3)

# 4.验证页面标题
result = WebDriverWait(driver, 5).until(EC.title_is("百度一下,你就知道"))
print("result =", result)

# 5.关闭浏览器
sleep(2)
driver.quit()
"""
输出结果:
result = True
"""

(2)presence_of_element_located(locator)示例(常用)

这两个方法比较常用:

  • presence_of_element_located(locator): 单个元素定位
  • presence_of_all_elements_located(locator):定位一组元素,复数形式

presence_of_element_located(locator)为例:

"""
1.学习目标:
    必须掌握EC模块中元素定位方法presence_of_element_located(locator)
2.语法
    2.1 presence_of_element_located(locator) 单个元素定位
    2.2 presence_of_all_elements_located(locator)  定位一组元素,复数形式
        其实是将find_element()方法做二次封装
        find_element_by_id("id属性值")
    2.3 locator--定位器是一个数据类型元组
            ("元素定位方式","方式对应的值")
            ("id","id属性值")
            ("class name","class属性值")
            ("xpath","xpath表达式")
            ("partial link text","连接部分文本")
            ("name","name属性值")
        具体可以看selenium.webdriver.common.by.By这个类
        第一个参数可写形式
            By.ID = "id",也就是(By.ID ,"id属性值")
            By.XPATH = "xpath", (By.XPATH,"xpath表达式")
    2.3 总结:
        EC模块中的方法一般和显示等待配合使用
        EC模块使用步骤:
            1.编写定位器locator
            2.配合webdriverWait一起使用

3.需求
    使用EC模块中元素定位方法presence_of_element_located(locator)
    定位百度首页搜索框
"""
# 1.导入selenium
from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# 2.打开Chrome浏览器
driver = webdriver.Chrome()

# 3.打开页面
url = "https://www.baidu.com"
driver.get(url)
sleep(3)

# 4.EC模块的使用
# 4.1 定位单个元素
# 4.1.1 编写locator(定位器)
# 定位百度首页输入框
input_locator = ("id", "kw")
# 定位百度首页"百度一下"
button_loator = ("id", "su")

# 4.1.2 定位元素
# 结合显式等待和EC模块实现元素定位
bd_input = WebDriverWait(driver, 5).until(EC.presence_of_element_located(input_locator))
bd_button = WebDriverWait(driver, 5).until(EC.presence_of_element_located(button_loator))

# 5.操作元素
bd_input.send_keys("『心善渊』Selenium3.0基础")
sleep(2)
bd_button.click()

# 6.关闭浏览器
sleep(5)
driver.quit()

(3)text_to_be_present_in_element(locator,text)示例

巩固练习:

"""
1.学习目标:
    必须掌握EC模块中元素定位方法
    text_to_be_present_in_element(locator,text)
2.语法
    2.1 text_to_be_present_in_element(locator,text)
        判断文本是否在元素中,如果存在返回true,如果不存在返回false
    2.2 text_to_be_present_in_element_value(locator,text)
        判断文本是否存在于元素的value属性值中,如果存在返回true,如果不存在返回false
"""
# 1.导入selenium
from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# 2.打开谷歌浏览器
driver = webdriver.Chrome()

# 3.输入网址
url = "https://www.baidu.com"
driver.get(url)
sleep(2)

# 4.EC模块的使用
# 4.1 判断text是否存在于文本中

# 4.1.1 编写定位器locator
link_loc = ("link text", "hao123")

# 4.1.2 定义期望值
text_link = "hao123"

# 4.1.3 判断文本是否在元素中,如果存在返回true,如果不存在返回false
result = WebDriverWait(driver, 5).until(EC.text_to_be_present_in_element(link_loc, text_link))
print("present_in_element判定结果:", result)

# 4.2 判断text是否存在于元素的value属性中
# 判断百度首页的"百度一下"
# 4.2.1 编写定位器locator
button_loc = ("id", "su")

# 4.2.2 定义期望值
text_button = "百度一下"

# 4.2.3 判断文本是否存在于元素的value属性值中,如果存在返回true,如果不存在返回false
result_val = WebDriverWait(driver, 5).until(EC.text_to_be_present_in_element_value(button_loc, text_button))
print("present_in_element_value判定结果:", result_val)

# 5.关闭浏览器
sleep(2)
driver.quit()

"""
输出结果:
present_in_element判定结果: True
present_in_element_value判定结果: True
"""

(4)注意:(重要)

上面所有的练习,都必须正确找到需求的定位元素,否则会抛TimeoutException异常。

因为WebDriverWait类和until()方法和EC模块这种组合的使用方法

WebDriverWait(driver, 5).until(EC.presence_of_element_located(input_locator))

如果没有定位到元素,会返回until()方法的返回结果,until()方法的返回结果是,在规定时间内没有找到需要定位的元素,就会抛出selenium.common.exceptions.TimeoutException: Message: 异常。

所以我们上边写的例子就是让自己熟悉WebDriverWait类和until()方法和EC模块这组合的用法,以后我们会进行异常处理和封装,让我们的代码更加可读,可复用。

posted @ 2021-07-14 09:18  繁华似锦Fighting  阅读(644)  评论(0编辑  收藏  举报