FrameLocator
FrameLocator表示页面上一个iframe
的视图。它包含了检索该iframe
并在其中定位元素所需的逻辑。FrameLocator可以通过locator.contentFrame()、page.frameLocator() 或 locator.frameLocator() 方法创建。
const locator = page.locator('#my-frame').contentFrame().getByText('Submit');
await locator.click();
严格模式
帧定位器是严格的。这意味着如果给定选择器匹配到多个元素,则所有对帧定位器的操作都会抛出错误。
// Throws if there are several frames in DOM:
await page.locator('.result-frame').contentFrame().getByRole('button').click();
// Works because we explicitly tell locator to pick the first frame:
await page.locator('.result-frame').contentFrame().first().getByRole('button').click();
将Locator转换为FrameLocator
如果您有一个指向iframe
的Locator对象,可以使用locator.contentFrame()将其转换为FrameLocator。
将FrameLocator转换为Locator
如果您有一个FrameLocator对象,可以使用frameLocator.owner()将其转换为指向同一iframe
的Locator。
方法
frameLocator
添加于: v1.17当使用iframes时,您可以创建一个帧定位器,它将进入该iframe并允许在该iframe中选择元素。
用法
frameLocator.frameLocator(selector);
参数
返回值
getByAltText
添加于: v1.27允许通过元素的alt文本来定位元素。
用法
例如,此方法将通过alt文本“Playwright标志”找到该图片
<img alt='Playwright logo'>
await page.getByAltText('Playwright logo').click();
参数
-
用于定位元素的文本。
-
options
Object (可选)
返回值
getByLabel
添加于: v1.27允许通过相关联的<label>
或aria-labelledby
元素的文本,或通过aria-label
属性来定位输入元素。
用法
例如,此方法将在以下DOM中通过标签“Username”和“Password”找到输入框
<input aria-label="Username">
<label for="password-input">Password:</label>
<input id="password-input">
await page.getByLabel('Username').fill('john');
await page.getByLabel('Password').fill('secret');
参数
-
用于定位元素的文本。
-
options
Object (可选)
返回值
getByPlaceholder
添加于: v1.27允许通过占位符文本定位输入元素。
用法
例如,考虑以下DOM结构。
<input type="email" placeholder="name@example.com" />
您可以通过占位符文本定位后填充输入框
await page
.getByPlaceholder('name@example.com')
.fill('playwright@microsoft.com');
参数
-
用于定位元素的文本。
-
options
Object (可选)
返回值
getByRole
添加于: v1.27允许通过元素的ARIA角色、ARIA属性和可访问名称来定位元素。
用法
考虑以下DOM结构。
<h3>Sign up</h3>
<label>
<input type="checkbox" /> Subscribe
</label>
<br/>
<button>Submit</button>
您可以通过其隐式角色定位每个元素
await expect(page.getByRole('heading', { name: 'Sign up' })).toBeVisible();
await page.getByRole('checkbox', { name: 'Subscribe' }).check();
await page.getByRole('button', { name: /submit/i }).click();
参数
-
role
"alert" | "alertdialog" | "application" | "article" | "banner" | "blockquote" | "button" | "caption" | "cell" | "checkbox" | "code" | "columnheader" | "combobox" | "complementary" | "contentinfo" | "definition" | "deletion" | "dialog" | "directory" | "document" | "emphasis" | "feed" | "figure" | "form" | "generic" | "grid" | "gridcell" | "group" | "heading" | "img" | "insertion" | "link" | "list" | "listbox" | "listitem" | "log" | "main" | "marquee" | "math" | "meter" | "menu" | "menubar" | "menuitem" | "menuitemcheckbox" | "menuitemradio" | "navigation" | "none" | "note" | "option" | "paragraph" | "presentation" | "progressbar" | "radio" | "radiogroup" | "region" | "row" | "rowgroup" | "rowheader" | "scrollbar" | "search" | "searchbox" | "separator" | "slider" | "spinbutton" | "status" | "strong" | "subscript" | "superscript" | "switch" | "tab" | "table" | "tablist" | "tabpanel" | "term" | "textbox" | "time" | "timer" | "toolbar" | "tooltip" | "tree" | "treegrid" | "treeitem"#必需的aria角色。
-
options
Object (可选)-
通常由
aria-checked
或原生<input type=checkbox>
控件设置的属性。了解更多关于
aria-checked
的信息。 -
通常由
aria-disabled
或disabled
设置的属性。注意与大多数其他属性不同,
disabled
是通过DOM层级继承的。了解更多关于aria-disabled
的信息。 -
是否精确匹配name:区分大小写和完整字符串。默认为false。当name是正则表达式时忽略。请注意,精确匹配仍会去除空白字符。
-
通常由
aria-expanded
设置的属性。了解更多关于
aria-expanded
的信息。 -
控制是否匹配隐藏元素的选项。默认情况下,角色选择器仅匹配由ARIA定义的非隐藏元素。
了解更多关于
aria-hidden
的信息。 -
通常出现在
heading
、listitem
、row
、treeitem
角色上的数字属性,对于<h1>-<h6>
元素有默认值。了解更多关于
aria-level
的信息。 -
用于匹配可访问名称的选项。默认情况下,匹配不区分大小写,并搜索子字符串,使用exact控制此行为。
了解更多关于可访问名称的信息。
-
通常由
aria-pressed
设置的属性。了解更多关于
aria-pressed
的信息。 -
通常由
aria-selected
设置的属性。了解更多关于
aria-selected
的信息。
-
返回值
详细信息
角色选择器不能替代可访问性审计和一致性测试,但可以对ARIA指南提供早期反馈。
许多html元素具有隐式定义的角色,这些角色可以被角色选择器识别。您可以在此处找到所有支持的角色。ARIA指南不推荐通过将role
和/或aria-*
属性设置为默认值来重复隐式角色和属性。
getByTestId
添加于: v1.27通过测试ID定位元素。
用法
考虑以下DOM结构。
<button data-testid="directions">Itinéraire</button>
您可以通过其测试ID定位元素
await page.getByTestId('directions').click();
参数
返回值
详细信息
默认情况下,使用data-testid
属性作为测试ID。如有必要,使用selectors.setTestIdAttribute()配置不同的测试ID属性。
// Set custom test id attribute from @playwright/test config:
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
testIdAttribute: 'data-pw'
},
});
getByText
添加于: v1.27允许定位包含给定文本的元素。
另请参见locator.filter(),它允许通过其他条件(如可访问角色)进行匹配,然后按文本内容进行过滤。
用法
考虑以下DOM结构
<div>Hello <span>world</span></div>
<div>Hello</div>
您可以通过文本子字符串、精确字符串或正则表达式进行定位
// Matches <span>
page.getByText('world');
// Matches first <div>
page.getByText('Hello world');
// Matches second <div>
page.getByText('Hello', { exact: true });
// Matches both <div>s
page.getByText(/Hello/);
// Matches second <div>
page.getByText(/^hello$/i);
参数
-
用于定位元素的文本。
-
options
Object (可选)
返回值
详细信息
按文本匹配总是规范化空白字符,即使是精确匹配。例如,它将多个空格变成一个,将换行符变成空格,并忽略开头和结尾的空白字符。
类型为button
和submit
的输入元素通过其value
而不是文本内容匹配。例如,通过文本"Log in"
定位会匹配<input type=button value="Log in">
。
getByTitle
添加于: v1.27允许通过元素的title属性定位元素。
用法
考虑以下DOM结构。
<span title='Issues count'>25 issues</span>
您可以通过title文本定位后检查issues数量
await expect(page.getByTitle('Issues count')).toHaveText('25 issues');
参数
-
用于定位元素的文本。
-
options
Object (可选)
返回值
locator
添加于: v1.17此方法在定位器的子树中查找匹配指定选择器的元素。它也接受过滤选项,类似于locator.filter()方法。
用法
frameLocator.locator(selectorOrLocator);
frameLocator.locator(selectorOrLocator, options);
参数
-
selectorOrLocator
string | Locator#用于解析DOM元素的选择器或定位器。
-
options
Object (可选)-
将方法的结果缩小到那些包含匹配此相对定位器的元素的元素。例如,包含
text=Playwright
的article
匹配<article><div>Playwright</div></article>
。内部定位器必须相对于外部定位器,并且从外部定位器的匹配开始查询,而不是从文档根部开始。例如,您可以在
<article><content><div>Playwright</div></content></article>
中找到包含div
的content
。但是,查找包含article div
的content
会失败,因为内部定位器必须是相对的,并且不应使用content
之外的任何元素。请注意,外部和内部定位器必须属于同一个帧。内部定位器不能包含FrameLocator。
-
hasNot
Locator (可选)添加于: v1.33#匹配不包含与内部定位器匹配的元素的元素。内部定位器针对外部定位器进行查询。例如,不包含
div
的article
匹配<article><span>Playwright</span></article>
。请注意,外部和内部定位器必须属于同一个帧。内部定位器不能包含FrameLocator。
-
hasNotText
string | RegExp (可选)添加于: v1.33#匹配不包含指定文本的元素,该文本可能位于其内部、子元素或后代元素中。当传入string时,匹配不区分大小写并搜索子字符串。
-
匹配包含指定文本的元素,该文本可能位于其内部、子元素或后代元素中。当传入string时,匹配不区分大小写并搜索子字符串。例如,
"Playwright"
匹配<article><div>Playwright</div></article>
。
-
返回值
owner
添加于: v1.43返回指向与此帧定位器相同iframe
的Locator对象。
当您在某处获得了FrameLocator对象,稍后想与iframe
元素进行交互时非常有用。
对于反向操作,使用locator.contentFrame()。
用法
const frameLocator = page.locator('iframe[name="embedded"]').contentFrame();
// ...
const locator = frameLocator.owner();
await expect(locator).toBeVisible();
返回值
已废弃
first
添加于: v1.17返回第一个匹配帧的定位器。
用法
frameLocator.first();
返回值
last
添加于: v1.17返回最后一个匹配帧的定位器。
用法
frameLocator.last();
返回值
nth
添加于: v1.17返回第n个匹配帧的定位器。它是从零开始的,nth(0)
选择第一个帧。
用法
frameLocator.nth(index);
参数
返回值