FrameLocator
FrameLocator 表示页面上 iframe
的视图。它封装了用于检索 iframe
并在该 iframe 中定位元素的逻辑。FrameLocator 可以通过 Locator.ContentFrame、Page.FrameLocator() 或 Locator.FrameLocator() 方法创建。
var locator = page.Locator("#my-frame").ContentFrame.GetByText("Submit");
await locator.ClickAsync();
严格性
帧定位器是严格的。这意味着如果给定选择器匹配多个元素,则对帧定位器执行的所有操作都将抛出错误。
// Throws if there are several frames in DOM:
await page.Locator(".result-frame").ContentFrame.GetByRole(AriaRole.Button).ClickAsync();
// Works because we explicitly tell locator to pick the first frame:
await page.Locator(".result-frame").First.ContentFrame.getByRole(AriaRole.Button).ClickAsync();
将 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 logo”查找图片
<img alt='Playwright logo'>
await page.GetByAltText("Playwright logo").ClickAsync();
参数
-
用于定位元素的文本。
-
options
FrameLocatorGetByAltTextOptions?
(可选)
返回值
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").FillAsync("john");
await page.GetByLabel("Password").FillAsync("secret");
参数
-
用于定位元素的文本。
-
options
FrameLocatorGetByLabelOptions?
(可选)
返回值
GetByPlaceholder
新增于: v1.27允许通过 placeholder 文本定位输入元素。
用法
例如,考虑以下 DOM 结构。
<input type="email" placeholder="name@example.com" />
您可以通过 placeholder 文本定位输入框后填充它
await page
.GetByPlaceholder("name@example.com")
.FillAsync("playwright@microsoft.com");
参数
-
用于定位元素的文本。
-
options
FrameLocatorGetByPlaceholderOptions?
(可选)
返回值
GetByRole
新增于: v1.27允许通过元素的 ARIA role、ARIA attributes 和 accessible name 定位元素。
用法
考虑以下 DOM 结构。
<h3>Sign up</h3>
<label>
<input type="checkbox" /> Subscribe
</label>
<br/>
<button>Submit</button>
您可以通过其隐式 role 定位每个元素
await Expect(Page
.GetByRole(AriaRole.Heading, new() { Name = "Sign up" }))
.ToBeVisibleAsync();
await page
.GetByRole(AriaRole.Checkbox, new() { Name = "Subscribe" })
.CheckAsync();
await page
.GetByRole(AriaRole.Button, new() {
NameRegex = new Regex("submit", RegexOptions.IgnoreCase)
})
.ClickAsync();
参数
-
role
enum AriaRole { 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 role。
-
options
FrameLocatorGetByRoleOptions?
(可选)-
通常由
aria-checked
或原生的<input type=checkbox>
控件设置的属性。了解更多关于
aria-checked
的信息。 -
通常由
aria-disabled
或disabled
设置的属性。注意与大多数其他属性不同,
disabled
通过 DOM 层次结构继承。了解更多关于aria-disabled
的信息。 -
是否精确匹配 Name|NameRegex:区分大小写且为整个字符串。默认为 false。当 Name|NameRegex 是正则表达式时忽略。请注意,精确匹配仍会去除空白字符。
-
通常由
aria-expanded
设置的属性。了解更多关于
aria-expanded
的信息。 -
控制是否匹配隐藏元素的选项。默认情况下,role 选择器只匹配非隐藏元素(根据 ARIA 定义)。
了解更多关于
aria-hidden
的信息。 -
通常用于
heading
、listitem
、row
、treeitem
角色的数字属性,对于<h1>-<h6>
元素具有默认值。了解更多关于
aria-level
的信息。 -
Name|NameRegex
string? | Regex? (可选)#匹配 accessible name 的选项。默认情况下,匹配不区分大小写并搜索子字符串,使用 Exact 控制此行为。
了解更多关于 accessible name 的信息。
-
通常由
aria-pressed
设置的属性。了解更多关于
aria-pressed
的信息。 -
通常由
aria-selected
设置的属性。了解更多关于
aria-selected
的信息。
-
返回值
详情
Role 选择器不能替代可访问性审计和一致性测试,它只是提供关于 ARIA 指南的早期反馈。
许多 html 元素都有一个隐式 定义的 role,role 选择器可以识别该角色。您可以在 此处找到所有支持的 role。ARIA 指南不建议通过将 role
和/或 aria-*
属性设置为默认值来重复隐式 role 和属性。
GetByTestId
新增于: v1.27按测试 ID 定位元素。
用法
考虑以下 DOM 结构。
<button data-testid="directions">Itinéraire</button>
您可以按测试 ID 定位元素
await page.GetByTestId("directions").ClickAsync();
参数
返回值
详情
默认情况下,使用 data-testid
属性作为测试 ID。如有必要,使用 Selectors.SetTestIdAttribute() 配置不同的测试 ID 属性。
GetByText
新增于: v1.27允许定位包含给定文本的元素。
另请参阅 Locator.Filter(),该方法允许通过其他条件(如 accessible role)进行匹配,然后按文本内容进行过滤。
用法
考虑以下 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", new() { Exact = true });
// Matches both <div>s
page.GetByText(new Regex("Hello"));
// Matches second <div>
page.GetByText(new Regex("^hello$", RegexOptions.IgnoreCase));
参数
-
用于定位元素的文本。
-
options
FrameLocatorGetByTextOptions?
(可选)
返回值
详情
通过文本匹配总是会规范化空白字符,即使使用精确匹配也是如此。例如,它会将多个空格变成一个,将换行符变成空格,并忽略开头和结尾的空白字符。
类型为 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
FrameLocatorGetByTitleOptions?
(可选)
返回值
Locator
新增于: v1.17此方法在定位器的子树中查找匹配指定选择器的元素。它也接受过滤选项,类似于 Locator.Filter() 方法。
用法
FrameLocator.Locator(selectorOrLocator, options);
参数
-
selectorOrLocator
string | Locator#解析 DOM 元素时使用的选择器或定位器。
-
options
FrameLocatorLocatorOptions?
(可选)-
将方法的结果范围缩小到包含匹配此相对定位器的元素的那些结果。例如,包含
text=Playwright
的article
会匹配<article><div>Playwright</div></article>
。内部定位器必须相对于外部定位器,并且从匹配的外部定位器开始查询,而不是从文档根开始。例如,您可以找到
<article><content><div>Playwright</div></content></article>
中包含div
的content
。然而,查找包含article div
的content
将会失败,因为内部定位器必须是相对的,不应使用content
外部的任何元素。请注意,外部和内部定位器必须属于同一个帧。内部定位器不能包含 FrameLocators。
-
HasNot
Locator? (可选)新增于: v1.33#匹配不包含与内部定位器匹配的元素的那些元素。内部定位器是针对外部定位器进行查询的。例如,不包含
div
的article
会匹配<article><span>Playwright</span></article>
。请注意,外部和内部定位器必须属于同一个帧。内部定位器不能包含 FrameLocators。
-
HasNotText|HasNotTextRegex
string? | Regex? (可选)新增于: v1.33#匹配其内部(可能在子元素或后代元素中)不包含指定文本的元素。当传递 string 时,匹配不区分大小写并搜索子字符串。
-
HasText|HasTextRegex
string? | Regex? (可选)#匹配其内部(可能在子元素或后代元素中)包含指定文本的元素。当传递 string 时,匹配不区分大小写并搜索子字符串。例如,`"Playwright"` 会匹配
<article><div>Playwright</div></article>
。
-
返回值
Owner
新增于: v1.43返回一个指向与此帧定位器相同的 iframe
的 Locator 对象。
当您在某处获得了 FrameLocator 对象,并且稍后想与 iframe
元素进行交互时非常有用。
对于反向操作,请使用 Locator.ContentFrame。
用法
var frameLocator = Page.Locator("iframe[name=\"embedded\"]").ContentFrame;
// ...
var locator = frameLocator.Owner;
await Expect(locator).ToBeVisibleAsync();
返回值
已弃用
First
新增于: v1.17请改用 Locator.First 后跟 Locator.ContentFrame。
返回第一个匹配帧的定位器。
用法
FrameLocator.First
返回值
Last
新增于: v1.17请改用 Locator.Last 后跟 Locator.ContentFrame。
返回最后一个匹配帧的定位器。
用法
FrameLocator.Last
返回值
Nth
新增于: v1.17请改用 Locator.Nth() 后跟 Locator.ContentFrame。
返回第 n 个匹配帧的定位器。它是零基于的,nth(0)
选择第一个帧。
用法
FrameLocator.Nth(index);
参数
返回值