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(selector) or Locator.FrameLocator(selector) method.
var locator = page.FrameLocator("#my-frame").Locator("text=Submit");
await locator.ClickAsync();
Strictness
Frame locators are strict. This means that all operations on frame locators will throw if more than one element matches given selector.
// Throws if there are several frames in DOM:
await page.FrameLocator(".result-frame").Locator("button").ClickAsync();
// Works because we explicitly tell locator to pick the first frame:
await page.FrameLocator(".result-frame").First.Locator("button").ClickAsync();
Converting Locator to FrameLocator
If you have a Locator object pointing to an iframe
it can be converted to FrameLocator using :scope
CSS selector:
var frameLocator = locator.FrameLocator(":scope");
- FrameLocator.First
- FrameLocator.FrameLocator(selector)
- FrameLocator.Last
- FrameLocator.Locator(selector, options)
- FrameLocator.Nth(index)
FrameLocator.First
- returns:FrameLocator># <
Returns locator to the first matching frame.
FrameLocator.FrameLocator(selector)
selector
<string> A selector to use when resolving DOM element. See working with selectors for more details.#- returns:FrameLocator># <
When working with iframes, you can create a frame locator that will enter the iframe and allow selecting elements in that iframe.
FrameLocator.Last
- returns:FrameLocator># <
Returns locator to the last matching frame.
FrameLocator.Locator(selector, options)
selector
<string> A selector to use when resolving DOM element. See working with selectors for more details.#options
<FrameLocatorLocatorOptions?
>Has
<Locator?> Matches elements containing an element that matches an inner locator. Inner locator is queried against the outer one. For example,article
that hastext=Playwright
matches<article><div>Playwright</div></article>
.#Note that outer and inner locators must belong to the same frame. Inner locator must not contain FrameLocators.
HasText
<string?|Regex?> 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>
.#
- <
The method finds an element matching the specified selector in the FrameLocator's subtree.
FrameLocator.Nth(index)
index
<int>#- returns:FrameLocator># <
Returns locator to the n-th matching frame. It's zero based, nth(0)
selects the first frame.