跳到主要内容

FrameLocator

FrameLocator 表示页面中 iframe 的视图。它封装了足够逻辑来检索 iframe 并在该 iframe 中定位元素。FrameLocator 可以通过 locator.contentFrame()page.frameLocator()locator.frameLocator() 方法创建。

const locator = page.locator('#my-frame').contentFrame().getByText('Submit');
await locator.click();

严格性

Frame locators 是严格的。这意味着如果多个元素匹配给定的选择器,所有对 frame locators 的操作都会抛出异常。

// 如果 DOM 中有多个 frame 会抛出异常:
await page.locator('.result-frame').contentFrame().getByRole('button').click();

// 可以正常工作,因为我们明确告诉 locator 选择第一个 frame:
await page.locator('.result-frame').contentFrame().first().getByRole('button').click();

将 Locator 转换为 FrameLocator

如果你有一个指向 iframeLocator 对象,可以使用 locator.contentFrame() 将其转换为 FrameLocator

将 FrameLocator 转换为 Locator

如果你有一个 FrameLocator 对象,可以使用 frameLocator.owner() 将其转换为指向相同 iframeLocator


方法

frameLocator

v1.17 版本新增 frameLocator.frameLocator

在处理 iframe 时,您可以创建一个 frame locator 来进入 iframe 并选择其中的元素。

用法

frameLocator.frameLocator(selector);

参数

  • selector string#

    用于解析 DOM 元素的选择器。

返回值


getByAltText

添加于: v1.27 frameLocator.getByAltText

允许通过元素的 alt 文本定位元素。

用法

例如,这个方法会通过 alt 文本 "Playwright logo" 找到图片:

<img alt='Playwright logo'>
await page.getByAltText('Playwright logo').click();

参数

  • text string | RegExp#

    用于定位元素的文本。

  • options Object (可选)

    • exact boolean (可选)#

      是否查找完全匹配:区分大小写且全字符串匹配。默认为 false。使用正则表达式定位时忽略此选项。注意完全匹配仍会修剪空白字符。

返回


getByLabel

添加于: v1.27 frameLocator.getByLabel

允许通过关联的 <label> 元素文本、aria-labelledby 元素或 aria-label 属性来定位输入元素。

用法

例如,以下方法将通过标签 "Username" 和 "Password" 在以下 DOM 中找到输入框:

<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');

参数

  • text string | RegExp#

    用于定位元素的文本

  • options Object (可选)

    • exact boolean (可选)#

      是否查找完全匹配:区分大小写且全字符串匹配。默认为 false。使用正则表达式定位时忽略此选项。注意完全匹配仍会去除空白字符。

返回


getByPlaceholder

添加于: v1.27 frameLocator.getByPlaceholder

允许通过占位符文本定位输入元素。

用法

例如,考虑以下 DOM 结构:

<input type="email" placeholder="name@example.com" />

您可以通过占位符文本定位输入框后填写内容:

await page
.getByPlaceholder('name@example.com')
.fill('playwright@microsoft.com');

参数

  • text string | RegExp#

    用于定位元素的文本。

  • options Object (可选)

    • exact boolean (可选)#

      是否查找完全匹配:区分大小写且全字符串匹配。默认为 false。使用正则表达式定位时此选项会被忽略。注意完全匹配仍会修剪空白字符。

返回值


getByRole

添加于: v1.27 frameLocator.getByRole

允许通过元素的 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 (可选)

    • checked boolean (可选)#

      通常由 aria-checked 或原生 <input type=checkbox> 控件设置的属性。

      了解更多关于 aria-checked

    • disabled boolean (可选)#

      通常由 aria-disableddisabled 设置的属性。

      :::注意

      与大多数其他属性不同,disabled 会通过 DOM 层次结构继承。了解更多关于 aria-disabled。 :::

    • exact boolean (可选) 添加于: v1.28#

      name 是否精确匹配:区分大小写且全字符串匹配。默认为 false。当 name 是正则表达式时忽略此选项。注意精确匹配仍会修剪空白字符。

    • expanded boolean (可选)#

      通常由 aria-expanded 设置的属性。

      了解更多关于 aria-expanded

    • includeHidden boolean (可选)#

      控制是否匹配隐藏元素的选项。默认情况下,角色选择器只匹配 ARIA 定义 的非隐藏元素。

      了解更多关于 aria-hidden

    • level number (可选)#

      通常存在于 headinglistitemrowtreeitem 角色的数字属性,对于 <h1>-<h6> 元素有默认值。

      了解更多关于 aria-level

    • name string | RegExp (可选)#

      匹配 可访问名称 的选项。默认情况下,匹配不区分大小写且搜索子字符串,使用 exact 来控制此行为。

      了解更多关于 可访问名称

    • pressed boolean (可选)#

      通常由 aria-pressed 设置的属性。

      了解更多关于 aria-pressed

    • selected boolean (可选)#

      通常由 aria-selected 设置的属性。

      了解更多关于 aria-selected

返回

详细信息

角色选择器 不能替代 可访问性审计和一致性测试,而是提供关于 ARIA 指南的早期反馈。

许多 HTML 元素都有 隐式定义的角色,这些角色可以被角色选择器识别。您可以在此找到所有 支持的 ARIA 角色。ARIA 指南 不建议 通过设置 role 和/或 aria-* 属性来复制隐式角色和属性的默认值。

getByTestId

添加于: v1.27 frameLocator.getByTestId

通过测试 ID 定位元素。

用法

考虑以下 DOM 结构:

<button data-testid="directions">Itinéraire</button>

您可以通过测试 ID 定位元素:

await page.getByTestId('directions').click();

参数

返回值

详细信息

默认情况下,使用 data-testid 属性作为测试 ID。如果需要,可以使用 selectors.setTestIdAttribute() 配置不同的测试 ID 属性。

// 从 @playwright/test 配置中设置自定义测试 ID 属性:
import { defineConfig } from '@playwright/test';

export default defineConfig({
use: {
testIdAttribute: 'data-pw'
},
});

getByText

添加于: v1.27 frameLocator.getByText

用于定位包含指定文本的元素。

另请参阅 locator.filter(),它允许通过其他条件(如可访问角色)进行匹配,然后按文本内容过滤。

用法

考虑以下 DOM 结构:

<div>Hello <span>world</span></div>
<div>Hello</div>

您可以通过文本子串、精确字符串或正则表达式进行定位:

// 匹配 <span>
page.getByText('world');

// 匹配第一个 <div>
page.getByText('Hello world');

// 匹配第二个 <div>
page.getByText('Hello', { exact: true });

// 匹配两个 <div>
page.getByText(/Hello/);

// 匹配第二个 <div>
page.getByText(/^hello$/i);

参数

  • text string | RegExp#

    用于定位元素的文本。

  • options Object (可选)

    • exact boolean (可选)#

      是否查找精确匹配:区分大小写且全字符串匹配。默认为 false。使用正则表达式定位时忽略此选项。注意精确匹配仍会修剪空白字符。

返回

详细信息

按文本匹配时总会规范化空白字符,即使是精确匹配。例如,会将多个空格转换为一个,将换行符转换为空格,并忽略前导和尾随空白字符。

类型为 buttonsubmit 的输入元素按其 value 值而非文本内容匹配。例如,通过文本 "Log in" 定位会匹配 <input type=button value="Log in">


getByTitle

添加于: v1.27 frameLocator.getByTitle

允许通过元素的 title 属性定位元素。

用法

考虑以下 DOM 结构:

<span title='Issues count'>25 issues</span>

您可以通过 title 文本定位后检查问题数量:

await expect(page.getByTitle('Issues count')).toHaveText('25 issues');

参数

  • text string | RegExp#

    用于定位元素的文本。

  • options Object (可选)

    • exact boolean (可选)#

      是否查找完全匹配:区分大小写且全字符串匹配。默认为 false。使用正则表达式定位时忽略此选项。注意完全匹配仍会修剪空白字符。

返回


locator

添加于: v1.17 frameLocator.locator

该方法在定位器的子树中查找与指定选择器匹配的元素。它也接受过滤选项,类似于 locator.filter() 方法。

了解更多关于定位器的信息

用法

frameLocator.locator(selectorOrLocator);
frameLocator.locator(selectorOrLocator, options);

参数

  • selectorOrLocator string | Locator#

    用于解析 DOM 元素的选择器或定位器。

  • options Object (可选)

    • has Locator (可选)#

      将方法结果缩小到包含匹配此相对定位器的元素。例如,article 包含 text=Playwright 会匹配 <article><div>Playwright</div></article>

      内部定位器必须相对于外部定位器,并且从外部定位器匹配处开始查询,而不是文档根目录。例如,你可以找到 <article><content><div>Playwright</div></content></article> 中包含 divcontent。但是查找包含 article divcontent 会失败,因为内部定位器必须是相对的,不应使用 content 之外的元素。

      注意外部和内部定位器必须属于同一个 frame。内部定位器不能包含 FrameLocator

    • hasNot Locator (可选) 添加于: v1.33#

      匹配不包含内部定位器元素的元素。内部定位器相对于外部定位器进行查询。例如,article 不包含 div 会匹配 <article><span>Playwright</span></article>

      注意外部和内部定位器必须属于同一个 frame。内部定位器不能包含 FrameLocator

    • hasNotText string | RegExp (可选) 添加于: v1.33#

      匹配不包含指定文本的元素(可能在子元素或后代元素中)。当传入 string 时,匹配不区分大小写并搜索子字符串。

    • hasText string | RegExp (可选)#

      匹配包含指定文本的元素(可能在子元素或后代元素中)。当传入 string 时,匹配不区分大小写并搜索子字符串。例如,"Playwright" 会匹配 <article><div>Playwright</div></article>

返回


owner

Added in: v1.43 frameLocator.owner

返回一个指向与此 frame 定位器相同的 iframeLocator 对象。

当你从某处获取了一个 FrameLocator 对象后,后续需要与该 iframe 元素交互时非常有用。

反向操作请使用 locator.contentFrame()

用法

const frameLocator = page.locator('iframe[name="embedded"]').contentFrame();
// ...
const locator = frameLocator.owner();
await expect(locator).toBeVisible();

返回值


已弃用

first

Added in: v1.17 frameLocator.first
已弃用

返回指向第一个匹配 frame 的定位器。

用法

frameLocator.first();

返回值


last

新增于: v1.17 frameLocator.last
已弃用

返回最后一个匹配 frame 的定位器。

用法

frameLocator.last();

返回值


nth

新增于: v1.17 frameLocator.nth
已弃用

返回第 n 个匹配 frame 的定位器。索引从零开始,nth(0) 表示选择第一个 frame。

用法

frameLocator.nth(index);

参数

返回值