网站接入百度地图,网站建设公司对父亲节宣传口号,网络营销是什么的产物,网站备案视频快捷导航 定位方式#xff1a;元素操作断言方式自动等待 定位方式#xff1a;
csspage.get_by_role() 通过显式和隐式可访问性属性进行定位。page.get_by_text() 按文本内容定位。page.get_by_label() 通过关联标签的文本定位表单控件。page.get_by_placeholder() 通过占位符… 快捷导航 定位方式元素操作断言方式自动等待 定位方式
csspage.get_by_role() 通过显式和隐式可访问性属性进行定位。page.get_by_text() 按文本内容定位。page.get_by_label() 通过关联标签的文本定位表单控件。page.get_by_placeholder() 通过占位符定位输入。page.get_by_alt_text() 通过文本替代来定位元素通常是图片。page.get_by_title() 通过标题属性来定位元素。page.get_by_test_id() 根据元素的属性定位元素data-testid可以配置其他属性。 CSS选择器 (locator(selector))
Playwright 的 CSS 选择器支持大多数 CSS 选择器功能包括类名、ID、属性选择、子元素等。它是最常用的定位方法之一。
# 定位类名为“menu-link”的元素
page.locator(.menu-link)# 定位ID为“username”的输入框
page.locator(#username)# 组合选择器
page.locator(div.menu a.link-item)文本选择器 (get_by_text())
Playwright 可以通过元素的文本内容来定位元素支持精确匹配和部分匹配。
span提交/span# 定位文本完全为“提交”的按钮
page.get_by_text(提交)# 使用正则表达式进行部分匹配
page.locator(text/提交.*/)
page.get_by_text(re.compile(提交, re.IGNORECASE))# 精准匹配
page.get_by_text(提交, exactTrue)角色选择器 (get_by_role()) 根据页面元素的 ARIA 角色定位元素特别适合用于具有无障碍特性的网站。Playwright 支持大多数标准的 ARIA 角色如 button、link、textbox 等。
# 定位“提交”按钮
page.get_by_role(button, name提交)# 定位导航栏中的链接
page.get_by_role(link, name首页)标签选择器get_by_label
根据页面标签进行定位大多数表单控件 都有专用标签方便与表单进行交互
labelPassword input typepassword //label# 表单字段定位时使用
page.get_by_label(password).fill(123456)测试 ID 选择器 (get_by_test_id())
如果 HTML 元素中有 data-testid 属性可以使用 get_by_test_id() 方法通过此属性进行定位。这种方式特别适合在前端开发中为测试提供稳定的定位。
# 定位data-testid属性为“submit-button”的按钮
page.get_by_test_id(submit-button)标题选择器 (get_by_title())
可以通过元素的 title 属性来定位元素。适用于有 title 提示的元素。
span title用户设置用户设置/span
(
python
# 定位标题为“用户设置”的按钮
page.get_by_title(用户设置)占位符选择器 (get_by_placeholder())
可以根据输入框中的 placeholder 占位符来定位元素。
input typeemail placeholder请输入用户名 /# 定位占位符为“请输入用户名”的输入框
page.get_by_placeholder(请输入用户名)Alt文本选择器 (get_by_alt_text())
根据图片或媒体元素的 alt 属性定位元素适用于图像或媒体内容。
img altplaywright logo src/img/playwright-logo.svg width100 /# 定位alt属性为“logo”的图片
page.get_by_alt_text(playwright logo)相对选择器
Playwright 支持使用 nth 索引、子元素选择、以及 .first()、.last() 等相对定位方法来定位元素适用于定位同类元素中的特定一个。
# 定位所有类名为“item”的元素中的第一个
page.locator(.item).first()# 定位所有类名为“item”的元素中的最后一个
page.locator(.item).last()# 定位索引为2的元素从0开始
page.locator(.item).nth(2)XPath选择器 (locator(xpath...))
XPath 是一种强大的定位方法可以用于定位复杂的节点关系。
# 定位“提交”按钮
page.locator(xpath//button[text()提交])# 定位父元素为 div 且子元素包含 span 的结构
page.locator(xpath//div[span])Frame 内定位 (frame_locator())
如果元素位于 iframe 中可以使用 frame_locator() 方法进入 iframe再在其中定位元素。
# 定位嵌套在 iframe 中的元素
frame page.frame_locator(#iframe-id)
frame.locator(button#submit).click()链式选择器
Playwright 支持链式定位使得在复杂页面结构中逐步定位更加灵活。
# 先找到父级菜单项再找其中的子项链接
page.locator(nav.menu).locator(a.link-item).click()过滤定位器 可以使用 locator.filter() 方法按文本过滤定位器。它将在元素内部的某个位置可能在后代元素中搜索特定字符串不区分大小写。
ullih3Product 1/h3buttonAdd to cart/button/lilih3Product 2/h3buttonAdd to cart/button/li
/ul# 点击第2个商品对应的按钮
page.get_by_role(listitem).filter(has_textProduct 2).get_by_role(button, nameAdd to cart
).click()# 正则
page.get_by_role(listitem).filter(has_textre.compile(Product 2)).get_by_role(button, nameAdd to cart
).click()过滤定位器必须相对于原始定位器并且从原始定位器匹配开始查询而不是从文档根开始。
元素操作
Playwright 可以与 HTML 输入元素进行交互例如文本输入、复选框、单选按钮、选择选项、鼠标单击、键入字符、按键和快捷键以及上传文件和焦点元素。
文本输入 locator.fill()
page.get_by_role(text).fill(123)复选框和单选按钮 locator.set_checked() 选中和取消选中复选框或单选按钮
# Check the checkbox
page.get_by_label(I agree to the terms above).check()选择选项 使用 locator.selectOption() 选择 元素中的一个或多个选项
# 选1个
page.get_by_label(Choose a color).select_option(blue)
# 选多个
page.get_by_label(Choose multiple colors).select_option([red, green, blue])鼠标点击
# 单次点击
page.get_by_role(button).click()# 双击
page.get_by_text(Item).dblclick()# 右键点击
page.get_by_text(Item).click(buttonright)# Shift click
page.get_by_text(Item).click(modifiers[Shift])# 悬浮在某个元素上
page.get_by_text(Item).hover()# 点击指定位置
page.get_by_text(Item).click(position{ x: 0, y: 0})# 强制点击
page.get_by_role(button).click(forceTrue)上传文件 可以使用 locator.set_input_files() 方法选择要上传的输入文件
# Select one file
page.get_by_label(Upload file).set_input_files(myfile.pdf)# Select multiple files
page.get_by_label(Upload files).set_input_files([file1.txt, file2.txt])# Select a directory
page.get_by_label(Upload directory).set_input_files(mydir)# Remove all the selected files
page.get_by_label(Upload file).set_input_files([])# Upload buffer from memory
page.get_by_label(Upload file).set_input_files(files[{name: test.txt, mimeType: text/plain, buffer: bthis is a test}],
)断言方式
Playwright 以 expect 函数的形式包含测试断言
断言方法描述expect(locator).to_be_checked()复选框被选中expect(locator).to_be_visible()元素是否可见expect(locator).to_have_text()元素匹配文本内容expect(page).to_have_title()页面有title
expect(page.get_by_text(Name), ABC).to_be_visible()超时时长 可以为全局或每个断言指定超时时长 默认为5s
全局
# conftest.py
expect.set_options(timeout10_000)单独设置
def test(page):expect(page.get_by_text(123)).to_be_visible(timeout10_100)自动等待
Playwright 在执行操作之前对元素执行一系列可操作性检查以确保这些操作按预期运行。它会自动等待所有相关检查通过然后才执行请求的操作。如果所需的检查未在给定的时间内通过timeout则操作失败并显示TimeoutError。 对于locator.click()Playwright 将确保 定位器解析为一个元素元素可见元素处于稳定状态即非动画或动画已完成元素接收事件不会被其他元素遮挡元素已启用