python playwright 自动等待和断言详解
作者:一名小测试 时间:2021-04-04 19:39:01
自动等待及元素执行方法
操作元素的一系列方法,只要调用了测试夹函数page,就能引出操作元素的方法:
import pytest
from playwright.sync_api import Page
def test_example(page: Page):
page.goto("https://www.baidu.com")
page.hover('//*[@id="u1"]/*[text()="设置"]')
page.click('//*[@id="s-user-setting-menu"]//*[text()="搜索设置"]')
page.set_checked('//*[text()="全部语言"]', False)
assert page.title() == "百度一下,你就知道"
page.close()
if __name__ == '__main__':
pytest.main(["-v", "test_example.py"])
playwright对元素执行前,会进行一系列可操作性检查,以确保这些行动按预期运行。它会自动等待所有相关检查通过,然后才执行请求的操作。如果所需的检查未在给定的范围内通过则抛出timeout,操作将失败并显示TimeoutError。
执行方法如下:
鼠标双击
page.dblclick()
获取元素焦点
# 获取元素并聚焦它。 如果没有匹配的元素,则方法等待匹配元素出现在 DOM 中。
page.focus('#su')
鼠标悬停
# 就是鼠标放在按钮上,此方法针对那种浮框操作
page.hover('//*[@id="u1"]/*[text()="设置"]')
鼠标点击
page.click('//*[@id="s-user-setting-menu"]//*[text()="搜索设置"]')
设置复选框取消或选中
page.click('//*[@id="s-user-setting-menu"]//*[text()="搜索设置"]')
取消已选中复选框取
# 确保元素是复选框或单选框。如果该元素已取消选中,则此方法立即返回
page.uncheck('//*[text()="仅简体中文"]')
输入参数
# 此方法是聚焦元素,input输入值后触发事件。您也可以传递一个空字符串来清除输入字段。
page.fill("#kw", "秦时明月")
获取元素属性值
# 返回元素属性值
page.get_attribute('#kw', 'name')
获取内部文本
page.inner_text('//*[@id="s-hotsearch-wrapper"]//*[@data-index="2"]//*[@class="title-content-title"]')
获取内部HTML
page.inner_html('//*[@id="s-hotsearch-wrapper"]//*[@data-index="2"]')
获取文本内容
page.text_content('//*[@id="s-hotsearch-wrapper"]//*[@data-index="2"]')
截图
# baidu.png存放至当前文件夹下的cases文件夹种
page.screenshot(path='./cases/baidu.png')
填写文本并触发键盘事件
# 为文本中的每个字符发送一个keydown、keypress/input和keyup事件。
page.type("#kw", "hello")
输入键盘操作
# 获取按钮元素,输入键盘操作
page.press('#su', 'Enter')
设置select下拉选项
# 与值匹配的单个选择
page.select_option(\"select#colors\", \"blue\")
# 与标签匹配的单个选择
page.select_option(\"select#colors\", label=\"blue\")
# 多项选择
page.select_option(\"select#colors\", value=[\"red\", \"green\", \"blue\"])
调度事件
# type可传:"click", "dragstart"
page.dispatch_event('#su', 'click')
检查点(断言)
文字内容断言
# 获取文本内容,进行断言
content = page.text_content('[target="_blank"]:first-child')
assert content == "新闻"
内部文字断言
# 获取内部文字,进行断言
text = page.inner_text('[target="_blank"]:first-child')
assert text == "新闻"
属性断言
# 获取属性值,进行断言
attribute = page.get_attribute('#su', 'value')
assert attribute == "百度一下"
复选框断言
page.hover('//*[@id="u1"]/*[text()="设置"]')
page.click('//*[@id="s-user-setting-menu"]//*[text()="搜索设置"]')
# 复选框状态,进行断言
checked = page.is_checked('//*[text()="全部语言"]')
assert checked
js表达式断言
# JS表达式,进行断言
js_content = page.locator('[data-index="4"]>a>[class="title-content-title"]').text_content()
assert js_content == "长津湖超战狼2成中国影史票房冠军"
内部HTML断言
# 内部 HTML ,进行断言
html = page.inner_html('[class="hot-title"]')
assert "百度热搜" in html
元素可见断言
# 元素可见性 ,进行断言
visible = page.is_visible('#su')
assert visible
启动状态断言
# 启用状态(元素存在可点击) ,进行断言
enabled = page.is_enabled('#su')
assert enabled
直接对比断言
assert page.title() == "百度一下,你就知道"
playwright还提供了自定义断言,这一块我还没有实操过,有兴趣可的可继续研究下:
# 断言本地存储值
user_id = page.evaluate("() => window.localStorage.getItem('user_id')")
assert user_id
# 断言输入元素的值
value = page.locator('#search').input_value()
assert value == 'query'
# 断言计算样式
font_size = page.locator('div').evaluate('el => window.getComputedStyle(el).fontSize')
assert font_size == '16px'
# 断言列表长度
length = page.locator('li.selected').count()
assert length == 3
来源:https://blog.csdn.net/LIFENG0402/article/details/121550064
标签:python,playwright,自动等待,断言
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
我的ImageMagick使用心得
2008-10-21 11:05:00
![](https://img.aspxhome.com/file/UploadPic/200810/21/20081021122020655s.jpg)
微信公众号接入ChatGPT机器人的方法
2023-11-19 22:05:42
![](https://img.aspxhome.com/file/2023/8/112598_0s.jpg)
python读取ini配置文件过程示范
2023-07-02 01:18:36
Python实现聊天机器人的示例代码
2022-11-16 18:10:56
![](https://img.aspxhome.com/file/2023/5/100995_0s.png)
python使用pdfminer解析pdf文件的方法示例
2023-07-31 00:36:47
W3C网页内容无障碍指南2.0(WCAG)
2008-11-20 13:40:00
python运行其他程序的实现方法
2022-11-15 05:18:09
Python使用sftp实现传文件夹和文件
2021-09-30 12:27:37
![](https://img.aspxhome.com/file/2023/1/76881_0s.jpg)
SQLSERVER 本地查询更新操作远程数据库的代码
2023-07-23 21:58:55
Python使用for生成列表实现过程解析
2021-11-28 08:15:30
Django+boostrap 美化admin后台的操作
2022-02-26 16:16:21
![](https://img.aspxhome.com/file/2023/3/71503_0s.jpg)
Python中如何自定义函数
2021-12-12 20:20:58
![](https://img.aspxhome.com/file/2023/2/84972_0s.png)
Python 字符串操作详情
2023-02-04 19:03:59
![](https://img.aspxhome.com/file/2023/6/68976_0s.png)
Python使用pdb调试代码的技巧
2022-11-18 04:36:06
![](https://img.aspxhome.com/file/2023/5/95925_0s.jpg)
CSS双线边框研究
2009-09-03 12:12:00
![](https://img.aspxhome.com/file/UploadPic/20099/3/db_01-71s.jpg)
django+xadmin+djcelery实现后台管理定时任务
2023-12-15 03:05:52
![](https://img.aspxhome.com/file/2023/5/97325_0s.png)
Pycharm插件(Grep Console)自定义规则输出颜色日志的方法
2023-04-21 19:20:50
![](https://img.aspxhome.com/file/2023/1/109951_0s.gif)
python方差检验的含义及用法
2022-08-09 15:53:56
Python根据成绩分析系统浅析
2023-08-02 20:25:07
![](https://img.aspxhome.com/file/2023/4/62024_0s.png)
谈中国站长站的文章干扰码实现方法
2007-10-13 11:13:00
![](https://img.aspxhome.com/file/UploadPic/up/2007101313560193.jpg)