playwright/docs/src/api/class-framelocator.md
2021-12-08 12:49:50 -08:00

3.1 KiB

class: 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 [method: Page.frameLocator] or [method: Locator.frameLocator] method.

const locator = page.frameLocator('#my-frame').locator('text=Submit');
await locator.click();
Locator locator = page.frameLocator("#my-frame").locator("text=Submit");
locator.click();
locator = page.frame_locator("#my-frame").locator("text=Submit")
await locator.click()
locator = page.frame_locator("my-frame").locator("text=Submit")
locator.click()
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').click();

// Works because we explicitly tell locator to pick the first frame:
await page.frameLocator('.result-frame').first().locator('button').click();
# Throws if there are several frames in DOM:
await page.frame_locator('.result-frame').locator('button').click()

# Works because we explicitly tell locator to pick the first frame:
await page.frame_locator('.result-frame').first.locator('button').click()
# Throws if there are several frames in DOM:
page.frame_locator('.result-frame').locator('button').click()

# Works because we explicitly tell locator to pick the first frame:
page.frame_locator('.result-frame').first.locator('button').click()
// Throws if there are several frames in DOM:
page.frame_locator(".result-frame").locator("button").click();

// Works because we explicitly tell locator to pick the first frame:
page.frame_locator(".result-frame").first().locator("button").click();
// 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();

method: FrameLocator.first

  • returns: <[FrameLocator]>

Returns locator to the first matching frame.

method: FrameLocator.frameLocator

  • returns: <[FrameLocator]>

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

param: FrameLocator.frameLocator.selector = %%-find-selector-%%

method: FrameLocator.last

  • returns: <[FrameLocator]>

Returns locator to the last matching frame.

method: FrameLocator.locator

  • returns: <[Locator]>

The method finds an element matching the specified selector in the FrameLocator's subtree.

param: FrameLocator.locator.selector = %%-find-selector-%%

method: FrameLocator.nth

  • returns: <[FrameLocator]>

Returns locator to the n-th matching frame.

param: FrameLocator.nth.index

  • index <[int]>