Skip to main content

FrameLocator

FrameLocator 表示页面上 iframe 的视图。它捕获足以检索 iframe 并定位该 iframe 中的元素的逻辑。FrameLocator 可以使用 page.frameLocator()locator.frameLocator() 方法创建。

FrameLocator represents a view to the iframe on the page. It captures the logic sufficient to retrieve the iframe and locate elements in that iframe. FrameLocator can be created with either page.frameLocator() or locator.frameLocator() method.

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

严格

Strictness

框架定位器很严格。这意味着如果多个元素与给定选择器匹配,则帧定位器上的所有操作都将抛出异常。

Frame locators are strict. This means that all operations on frame locators will throw if more than one element matches a given selector.

// Throws if there are several frames in DOM:
await page.frameLocator('.result-frame').getByRole('button').click();

// Works because we explicitly tell locator to pick the first frame:
await page.frameLocator('.result-frame').first().getByRole('button').click();

将定位器转换为帧定位器

Converting Locator to FrameLocator

如果你有一个指向 iframe 的 [定位器][Locator] 对象,则可以使用 :scope CSS 选择器将其转换为 [FrameLocator][FrameLocator]:

If you have a Locator object pointing to an iframe it can be converted to FrameLocator using :scope CSS selector:

const frameLocator = locator.frameLocator(':scope');

方法

Methods

first

Added in: v1.17 frameLocator.first

返回第一个匹配帧的定位器。

Returns locator to the first matching frame.

用法

Usage

frameLocator.first();

返回

Returns

  • [FrameLocator]#

frameLocator

Added in: v1.17 frameLocator.frameLocator

使用 iframe 时,你可以创建一个框架定位器,该定位器将进入 iframe 并允许选择该 iframe 中的元素。

When working with iframes, you can create a frame locator that will enter the iframe and allow selecting elements in that iframe.

用法

Usage

frameLocator.frameLocator(selector);

参数

Arguments

  • selector [string]#

解析 DOM 元素时使用的选择器。

A selector to use when resolving DOM element.

返回

Returns

  • [FrameLocator]#

getByAltText

Added in: v1.27 frameLocator.getByAltText

允许通过替代文本定位元素。

Allows locating elements by their alt text.

用法

Usage

例如,此方法将通过替代文本 "Playwright 标志" 查找图片:

For example, this method will find the image by alt text "Playwright logo":

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

参数

Arguments

  • text [string]|[RegExp]#

用于定位元素的文本。

Text to locate the element for.

  • options [Object] (optional)

    • exact [boolean] (optional)#

是否查找精确匹配:区分大小写和整个字符串。默认为 false。通过正则表达式定位时被忽略。请注意,完全匹配仍然会修剪空格。

Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

返回

Returns

  • [Locator]#

getByLabel

Added in: v1.27 frameLocator.getByLabel

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

Allows locating input elements by the text of the associated <label> or aria-labelledby element, or by the aria-label attribute.

用法

Usage

例如,此方法将在以下 DOM 中查找标签 "用户名" 和 "密码" 的输入:

For example, this method will find inputs by label "Username" and "Password" in the following 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');

参数

Arguments

  • text [string]|[RegExp]#

用于定位元素的文本。

Text to locate the element for.

  • options [Object] (optional)

    • exact [boolean] (optional)#

是否查找精确匹配:区分大小写和整个字符串。默认为 false。通过正则表达式定位时被忽略。请注意,完全匹配仍然会修剪空格。

Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

返回

Returns

  • [Locator]#

getByPlaceholder

Added in: v1.27 frameLocator.getByPlaceholder

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

Allows locating input elements by the placeholder text.

用法

Usage

例如,考虑以下 DOM 结构。

For example, consider the following DOM structure.

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

你可以在通过占位符文本找到输入后填充输入:

You can fill the input after locating it by the placeholder text:

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

参数

Arguments

  • text [string]|[RegExp]#

用于定位元素的文本。

Text to locate the element for.

  • options [Object] (optional)

    • exact [boolean] (optional)#

是否查找精确匹配:区分大小写和整个字符串。默认为 false。通过正则表达式定位时被忽略。请注意,完全匹配仍然会修剪空格。

Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

返回

Returns

  • [Locator]#

getByRole

Added in: v1.27 frameLocator.getByRole

允许通过 ARIA 角色ARIA 属性可访问的名称 定位元素。

Allows locating elements by their ARIA role, ARIA attributes and accessible name.

用法

Usage

考虑以下 DOM 结构。

Consider the following DOM structure.

<h3>Sign up</h3>
<label>
<input type="checkbox" /> Subscribe
</label>
<br/>
<button>Submit</button>

你可以通过每个元素的隐式角色来定位它:

You can locate each element by it's implicit role:

await expect(page.getByRole('heading', { name: 'Sign up' })).toBeVisible();

await page.getByRole('checkbox', { name: 'Subscribe' }).check();

await page.getByRole('button', { name: /submit/i }).click();

参数

Arguments

  • 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"#

    所需的咏叹调角色。

    Required aria role.

  • options [Object] (optional)

    • checked [boolean] (optional)#

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

An attribute that is usually set by aria-checked or native <input type=checkbox> controls.

了解有关 aria-checked 的更多信息。

Learn more about aria-checked.

  • disabled [boolean] (optional)#

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

An attribute that is usually set by aria-disabled or disabled.

note

disabled is inherited through the DOM hierarchy. Learn more about aria-disabled. :::与大多数其他属性不同,

  • exact boolean (optional) Added in: v1.28#

name 是否完全匹配:区分大小写和整个字符串。默认为 false。当 name 是正则表达式时被忽略。请注意,完全匹配仍然会修剪空格。

Whether name is matched exactly: case-sensitive and whole-string. Defaults to false. Ignored when name is a regular expression. Note that exact match still trims whitespace.

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

An attribute that is usually set by aria-expanded.

了解有关 aria-expanded 的更多信息。

Learn more about aria-expanded.

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

Option that controls whether hidden elements are matched. By default, only non-hidden elements, as defined by ARIA, are matched by role selector.

了解有关 aria-hidden 的更多信息。

Learn more about aria-hidden.

通常为角色 headinglistitemrowtreeitem 提供的数字属性,并为 <h1>-<h6> 元素提供默认值。

A number attribute that is usually present for roles heading, listitem, row, treeitem, with default values for <h1>-<h6> elements.

了解有关 aria-level 的更多信息。

Learn more about aria-level.

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

Option to match the accessible name. By default, matching is case-insensitive and searches for a substring, use exact to control this behavior.

了解有关 可访问的名称 的更多信息。

Learn more about accessible name.

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

An attribute that is usually set by aria-pressed.

了解有关 aria-pressed 的更多信息。

Learn more about aria-pressed.

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

An attribute that is usually set by aria-selected.

了解有关 aria-selected 的更多信息。

Learn more about aria-selected.

返回

Returns

细节

Details

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

Role selector does not replace accessibility audits and conformance tests, but rather gives early feedback about the ARIA guidelines.

许多 html 元素都有一个隐式的 明确的角色,可以被角色选择器识别。你可以找到所有的 此处支持的角色。ARIA 指南不建议通过将 role 和/或 aria-* 属性设置为默认值来复制隐式角色和属性。

Many html elements have an implicitly defined role that is recognized by the role selector. You can find all the supported roles here. ARIA guidelines do not recommend duplicating implicit roles and attributes by setting role and/or aria-* attributes to default values.


getByTestId

Added in: v1.27frameLocator.getByTestId

通过测试 ID 定位元素。

Locate element by the test id.

用法

Usage

考虑以下 DOM 结构。

Consider the following DOM structure.

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

你可以通过元素的测试 ID 来定位该元素:

You can locate the element by it's test id:

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

参数

Arguments

用于定位元素的 ID。

Id to locate the element by.

返回

Returns

细节

Details

默认情况下,data-testid 属性用作测试 ID。如有必要,请使用 selectors.setTestIdAttribute() 配置不同的测试 ID 属性。

By default, the data-testid attribute is used as a test id. Use selectors.setTestIdAttribute() to configure a different test id attribute if necessary.

// Set custom test id attribute from @playwright/test config:
import { defineConfig } from '@playwright/test';

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

getByText

Added in: v1.27frameLocator.getByText

允许定位包含给定文本的元素。

Allows locating elements that contain given text.

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

See also locator.filter() that allows to match by another criteria, like an accessible role, and then filter by the text content.

用法

Usage

考虑以下 DOM 结构:

Consider the following DOM structure:

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

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

You can locate by text substring, exact string, or a regular expression:

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

参数

Arguments

用于定位元素的文本。

Text to locate the element for.

是否查找精确匹配:区分大小写和整个字符串。默认为 false。通过正则表达式定位时被忽略。请注意,完全匹配仍然会修剪空格。

Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

返回

Returns

细节

Details

按文本匹配始终会标准化空格,即使是完全匹配也是如此。例如,它将多个空格变成一个,将换行符变成空格,并忽略前导和尾随空白。

Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.

buttonsubmit 类型的输入元素通过其 value 而不是文本内容进行匹配。例如,通过文本 "Log in" 查找匹配 <input type=button value="Log in">

Input elements of the type button and submit are matched by their value instead of the text content. For example, locating by text "Log in" matches <input type=button value="Log in">.


getByTitle

Added in: v1.27frameLocator.getByTitle

允许通过标题属性定位元素。

Allows locating elements by their title attribute.

用法

Usage

考虑以下 DOM 结构。

Consider the following DOM structure.

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

你可以通过标题文本找到问题后查看问题数:

You can check the issues count after locating it by the title text:

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

参数

Arguments

用于定位元素的文本。

Text to locate the element for.

是否查找精确匹配:区分大小写和整个字符串。默认为 false。通过正则表达式定位时被忽略。请注意,完全匹配仍然会修剪空格。

Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.

返回

Returns


last

Added in: v1.17frameLocator.last

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

Returns locator to the last matching frame.

用法

Usage

frameLocator.last();

返回

Returns


locator

Added in: v1.17frameLocator.locator

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

The method finds an element matching the specified selector in the locator's subtree. It also accepts filter options, similar to locator.filter() method.

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

Learn more about locators.

用法

Usage

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

参数

Arguments

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

A selector or locator to use when resolving DOM element.

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

Narrows down the results of the method to those which contain elements matching this relative locator. For example, article that has text=Playwright matches <article><div>Playwright</div></article>.

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

Inner locator must be relative to the outer locator and is queried starting with the outer locator match, not the document root. For example, you can find content that has div in <article><content><div>Playwright</div></content></article>. However, looking for content that has article div will fail, because the inner locator must be relative and should not use any elements outside the content.

请注意,外部和内部定位器必须属于同一框架。内部定位器不得包含 FrameLocator

Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.

  • hasNot Locator (optional) Added in: v1.33#

匹配不包含与内部定位器匹配的元素的元素。内部定位器是针对外部定位器进行查询的。例如,没有 divarticle<article><span>Playwright</span></article> 相匹配。

Matches elements that do not contain an element that matches an inner locator. Inner locator is queried against the outer one. For example, article that does not have div matches <article><span>Playwright</span></article>.

请注意,外部和内部定位器必须属于同一框架。内部定位器不得包含 FrameLocator

Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.

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

Matches elements that do not contain specified text somewhere inside, possibly in a child or a descendant element. When passed a string, matching is case-insensitive and searches for a substring.

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

Matches elements containing specified text somewhere inside, possibly in a child or a descendant element. When passed a string, matching is case-insensitive and searches for a substring. For example, "Playwright" matches <article><div>Playwright</div></article>.

返回

Returns


nth

Added in: v1.17frameLocator.nth

返回第 n 个匹配帧的定位器。它是从零开始的,nth(0) 选择第一帧。

Returns locator to the n-th matching frame. It's zero based, nth(0) selects the first frame.

用法

Usage

frameLocator.nth(index);

参数

Arguments

返回

Returns