Selenium基础 — Selenium中的expected_conditions模块(二)
3、expected_conditions模块独立使用
我们练习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、expected_conditions模块综合使用
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
模块这组合的用法,以后我们会进行异常处理和封装,让我们的代码更加可读,可复用。
最后我这里给你们分享一下我所积累和真理的文档和学习资料有需要是领取就可以了
1、学习思路和方法
这个大纲涵盖了目前市面上企业百分之99的技术,这个大纲很详细的写了你该学习什么内容,企业会用到什么内容。总共十个专题足够你学习
2、想学习却无从下手,该如何学习?
这里我准备了对应上面的每个知识点的学习资料、可以自学神器,已经项目练手。
3、软件测试/自动化测试【全家桶装】学习中的工具、安装包、插件....
4、有了安装包和学习资料,没有项目实战怎么办,我这里都已经准备好了往下看
最后送上一句话:
世界的模样取决于你凝视它的目光,自己的价值取决于你的追求和心态,一切美好的愿望,不在等待中拥有,而是在奋斗中争取。
如果我的博客对你有帮助、如果你喜欢我的文章内容,请 “点赞” “评论” “收藏” 一键三连哦
相关文章
- 【REACT NATIVE 系列教程之三】函数绑定与FLEXBOX是用好REACT的基础
- 【Linux基础】tr命令替换和删除字符
- 4-03python语法基础-内置模块-time模块
- 4-05python语法基础-内置模块-json模块
- 【基础入门题025】求二次方程的根
- 零基础学Python(第十七章 模块import)
- 100天精通Python(基础篇)——第32天:日志logging模块基础+代码实战
- 100天精通Python(基础篇)——第21天:模块和包
- 〖Python零基础入门篇㉓〗- 列表的索引与切片
- 〖Python零基础入门篇㊼〗- 包与模块
- 〖Python零基础入门篇㊿〗- Python中的 sys 模块
- 〖Python零基础入门篇(56)〗- Python的加密模块 - hashlib 与 base64
- 前端基础(国税)
- python基础===zmail,收发邮件的模块
- python基础===monkeytype可以自动添加注释的模块!
- python基础===中文手册,可查询各个模块
- 一天掌握C51单片机基础1-计算机数值与MCS51单片机
- 试题 基础练习 特殊的数字
- Linux基础入门到精通之虚拟机管理技巧(快照、克隆)