selenium3.0基础—selenium的expected_conditions模块详细介绍(代码片段)

测试baby 测试baby     2022-12-06     534

关键词:

在这里插入图片描述

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

1、EC模块介绍

expected_conditions是Selenium的一个模块,主要用于对页面元素的加载进行判断,包括元素是否存在,可点击等等。

expected_conditions模块的使用场景一般有两种:

直接在断言中使用 。

与WebDriverWait配合使用,显示等待页面上元素出现或者消失。

一般情况下,我们在使用expected_conditions模块时,都会对其进行重命名,通过as关键字对其重命名为EC。

python
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_NAME,CSS_SELECTOR,ID,LINK_TEXT,NAME,PARTIAL_LINK_TEXT,TAG_NAME和XPATH,和我们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模块中两个功能,其他功能参照即可。

python
# 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)例子很简单,作为一个入门示例。

python
"""
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)为例:

python
"""
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)示例

巩固练习:

python
"""
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模块这种组合的使用方法

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

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

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

这里给大家整理了一份《软件测试工程师进阶的技术栈》,包含了诸多技术栈,希望能帮助在升级打怪中提供中坚力量。

给大家推荐下我自己建的软件测试交流学习群: 902061117 ,群里都是搞软件测试的,如果你正在学习测试 ,小编欢迎你加入,大家都是测试党,群内不定期分享干货(都是软件测试相关的),包括我自己整理的一份2021最新的进阶自动化资料。

在这里插入图片描述

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你

关注我的微信公众号【伤心的辣条】免费获取~

送上一句话:

世界的模样取决于你凝视它的目光,自己的价值取决于你的追求和心态,一切美好的愿望,不在等待中拥有,而是在奋斗中争取。

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!

在这里插入图片描述

好文推荐:

转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!

测试岗反复跳槽,跳着跳着就跳没了…

软件测试人员该学习 Python 的七个理由

App公共测试用例梳理

面试经:一线城市搬砖!又面软件测试岗,5000就知足了…

35岁之后软件测试工程师靠什么养家?我能继续做测试!

selenium3.0基础—unittest中测试套件的使用(代码片段)

1、测试套件的作用在我们实际工作,使用unittest框架会有两个问题:我们知道测试用例的执行顺序是根据测试用例名称顺序执行的,在不改变用例名称的情况下,我们怎么来控制用例执行的顺序。一个测试文件... 查看详情

selenium3.0自动化测试

...个圣诞节发布。  转眼的三年过去了,目前已经发布到Selenium3.0beta4版本,这将会是Selenium3.0正式版本前的最后一个测试版本。  尽管我对Selenium3.0比较失望(本以为它会集成移动端的自动化测试)。但是,它还是做了一些变... 查看详情

selenium3.0火狐环境搭建

selenium2操作火狐浏览器时不需要驱动,3.0之后版本需要安装驱动,由于会有驱动与浏览器版本兼容性问题,配置好环境之后不一定能执行下面提供笔者成功执行的版本:火狐:en版49.00 各版本下载地址 http://ftp.mozilla.org/pub/fir... 查看详情

selenium3.0键盘事件+强制结束chromedriver进程代码

selenium自动化测试常常用到键盘操作,一下是键盘操作的详细操作,和部分代码。截图来自于虫师的自动化相关书籍。   publicstaticvoidmain(String[]args)throwsInterruptedException{ System.setProperty("webdriver.chrome.driver","D:/chromedriver_ 查看详情

启动火狐和谷歌在selenium3.0上的启动

参考地址:http://www.cnblogs.com/fnng/p/5932224.html        https://github.com/mozilla/geckodriver【火狐浏览器】火狐浏览器的驱动下载地址:https://github.com/mozilla/geckodriver/releases 查看详情

selenium3.0与2.0打开火狐浏览器的区别

3.0的selenium需要引入gecko.driver驱动,因为没有在系统环境path中配置相关路径,因此需要特别指出,为了方便使用,建议直接和火狐安装包中的.exe文件放在同一目录下。2.0的selenium有自带驱动,并不需要额外引入,只需要指明firefo... 查看详情

selenium3.0远程模式

准备工作:1.安装chrome浏览器2.下载selnium-server-standalone-3.0.1.jar步骤:1.java-jarselnium-server-standalone-3.0.1.jar (默认ip:localhost、port:4444)  PS:作为单个server启动时,千万不要画蛇添足加个-rolehub/node2. 测试脚 查看详情

带有 HTMLUnitDriver 的 Selenium 3.0.x

】带有HTMLUnitDriver的Selenium3.0.x【英文标题】:Selenium3.0.xwithHTMLUnitDriver【发布时间】:2017-03-2210:10:23【问题描述】:我的Maven项目中有selenium-java3.0.1。我读过这个版本没有与HTMLUnitDriver一起打包。所以,我在我的pom中单独包含了sele... 查看详情

selenium3自动化测试浏览器driver的安装(firefox)

...就围绕常用的FireFox、Chrome与Selenium的结合进行讲解。由于Selenium3.0调用FireFox48(含48)以上的版本,需要先安装浏览器的驱动driver,因此小节讲解下浏览器驱动driver的安装。FireFox浏览 查看详情

selenium3.0调用chrome报这个--ignore-certificate-errors怎么解决?求高手

试了网上说的方法。包括升级浏览器啥的都没有解决这个问题.....快疯了参考技术Achrome_options = Options()chrome_options.add_argument('--disable-extensions')chrome_options.add_experimental_option('excludeSwitches',['ignore-certificate-errors&... 查看详情

selenium-1(代码片段)

...至此,Selenium2.0出现了(Selenium2.0=Selenium+WebDriver)阶段4Selenium3.0诞生,实现了把核心API跟客户端driver进行分离,同时去掉用的越来越少的SeleniumRC功能Selenium1.0测试步骤Selenium1.0(SeleniumRC或RemoteControl)工具的核心部分是基于JavaScr... 查看详情

带有 IE11 的 Selenium 3.0.1 在 Windows 10 上找不到元素(在带有 IE11 的 Windows 7 上运行良好)

】带有IE11的Selenium3.0.1在Windows10上找不到元素(在带有IE11的Windows7上运行良好)【英文标题】:Selenium3.0.1withIE11notfindingelementsonWindows10(WorksfineonWindows7withIE11)【发布时间】:2017-07-2110:05:20【问题描述】:操作系统:Windows10浏览器:... 查看详情

Selenium 脚本在 Firefox 浏览器上失败

...发布时间】:2017-06-0319:56:38【问题描述】:我正在使用-Selenium3.0.1与java火狐版本:50.1.0geckodriver-v0.13.0-win64我已经为我的Web应用程序创建了一套完整的测试套件。当我在chrome浏览器上运行它时,所有测试都运行顺利。除了浏览器... 查看详情

使用 FF49 和 Python 的 Selenium 3.0.1 的 Firefox 驱动程序无法启动

】使用FF49和Python的Selenium3.0.1的Firefox驱动程序无法启动【英文标题】:Firefoxdrivercan\'tstartforSelenium3.0.1withFF49andPython【发布时间】:2017-03-2005:32:38【问题描述】:我有使用Python的SeleniumWebdriver脚本。但我得到了错误:fromseleniumimportw... 查看详情

Firefox 错误:使用 Java 使用 Selenium 3.0.1 启动驱动程序时出现“您的连接不安全”

】Firefox错误:使用Java使用Selenium3.0.1启动驱动程序时出现“您的连接不安全”【英文标题】:FirefoxError:"Yourconnectionisnotsecure"whilelaunchingdriverwithSelenium3.0.1usingJava【发布时间】:2017-03-2022:20:54【问题描述】:我的Firefox版本... 查看详情

selenium学习selenium总结

...m简介Selenium经历了三个大版本,Selenium1.0、Selenium2.0和Selenium3.0。Selenium不是由单独一个工具构成的,而是由一些插件和类库组成的,这些插件和类库有其各自的特点和应用场景。Selenium1.0家族关系如下图所示。1.1 Seleni... 查看详情

如何防止 Selenium 3.0 (Geckodriver) 创建临时 Firefox 配置文件?

】如何防止Selenium3.0(Geckodriver)创建临时Firefox配置文件?【英文标题】:HowtoPreventSelenium3.0(Geckodriver)fromCreatingTemporaryFirefoxProfiles?【发布时间】:2021-08-0605:33:27【问题描述】:我正在使用Geckodriver运行最新版本的SeleniumWebDriver。我想... 查看详情

使用python+selenium实现第一个自动化测试脚本

python3.6+selenium3.0环境:windows10,64位一、安装pythonpython官方下载地址:https://www.python.org/downloads/进入页面就有两个版本的下载选择,2.x版本和3.x版本,或者根据系统选择对应版本。点击Windows,跳转到Windows版本页面:点选Python3.6.0... 查看详情