playwright/docs/src/api/class-framelocator.md

254 lines
6.9 KiB
Markdown
Raw Normal View History

# class: FrameLocator
2022-07-06 02:24:50 +02:00
* since: v1.17
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.
```js
const locator = page.frameLocator('#my-frame').getByText('Submit');
await locator.click();
```
```java
Locator locator = page.frameLocator("#my-frame").getByText("Submit");
locator.click();
```
```python async
locator = page.frame_locator("#my-frame").get_by_text("Submit")
await locator.click()
```
```python sync
locator = page.frame_locator("my-frame").get_by_text("Submit")
locator.click()
```
```csharp
var locator = page.FrameLocator("#my-frame").GetByText("Submit");
await locator.ClickAsync();
```
**Strictness**
2022-09-12 17:41:49 +02:00
Frame locators are strict. This means that all operations on frame locators will throw if more than one element matches a given selector.
```js
// 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();
```
```python async
# Throws if there are several frames in DOM:
await page.frame_locator('.result-frame').get_by_role('button').click()
# Works because we explicitly tell locator to pick the first frame:
await page.frame_locator('.result-frame').first.get_by_role('button').click()
```
```python sync
# Throws if there are several frames in DOM:
page.frame_locator('.result-frame').get_by_role('button').click()
# Works because we explicitly tell locator to pick the first frame:
page.frame_locator('.result-frame').first.get_by_role('button').click()
```
```java
// Throws if there are several frames in DOM:
page.frame_locator(".result-frame").getByRole(AriaRole.BUTTON).click();
// Works because we explicitly tell locator to pick the first frame:
page.frame_locator(".result-frame").first().getByRole(AriaRole.BUTTON).click();
```
```csharp
// Throws if there are several frames in DOM:
await page.FrameLocator(".result-frame").GetByRole(AriaRole.Button).ClickAsync();
// Works because we explicitly tell locator to pick the first frame:
await page.FrameLocator(".result-frame").First.getByRole(AriaRole.Button).ClickAsync();
```
**Converting Locator to FrameLocator**
If you have a [Locator] object pointing to an `iframe` it can be converted to [FrameLocator] using [`method: Locator.contentFrame`].
**Converting FrameLocator to Locator**
If you have a [FrameLocator] object it can be converted to [Locator] pointing to the same `iframe` using [`method: FrameLocator.owner`].
## method: FrameLocator.first
2022-07-06 02:24:50 +02:00
* since: v1.17
- returns: <[FrameLocator]>
Returns locator to the first matching frame.
## method: FrameLocator.frameLocator
2022-07-06 02:24:50 +02:00
* since: v1.17
- 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-%%
2022-07-06 02:24:50 +02:00
* since: v1.17
## method: FrameLocator.getByAltText
* since: v1.27
- returns: <[Locator]>
%%-template-locator-get-by-alt-text-%%
### param: FrameLocator.getByAltText.text = %%-locator-get-by-text-text-%%
2022-11-21 18:30:32 +01:00
### option: FrameLocator.getByAltText.exact = %%-locator-get-by-text-exact-%%
## method: FrameLocator.getByLabel
2022-09-29 20:06:58 +02:00
* since: v1.27
- returns: <[Locator]>
%%-template-locator-get-by-label-text-%%
### param: FrameLocator.getByLabel.text = %%-locator-get-by-text-text-%%
2022-09-29 20:06:58 +02:00
2022-11-21 18:30:32 +01:00
### option: FrameLocator.getByLabel.exact = %%-locator-get-by-text-exact-%%
2022-09-29 20:06:58 +02:00
## method: FrameLocator.getByPlaceholder
* since: v1.27
- returns: <[Locator]>
%%-template-locator-get-by-placeholder-text-%%
### param: FrameLocator.getByPlaceholder.text = %%-locator-get-by-text-text-%%
2022-11-21 18:30:32 +01:00
### option: FrameLocator.getByPlaceholder.exact = %%-locator-get-by-text-exact-%%
## method: FrameLocator.getByRole
* since: v1.27
- returns: <[Locator]>
%%-template-locator-get-by-role-%%
### param: FrameLocator.getByRole.role = %%-get-by-role-to-have-role-role-%%
* since: v1.27
2022-11-21 18:30:32 +01:00
### option: FrameLocator.getByRole.-inline- = %%-locator-get-by-role-option-list-v1.27-%%
* since: v1.27
2022-11-21 18:30:32 +01:00
### option: FrameLocator.getByRole.exact = %%-locator-get-by-role-option-exact-%%
## method: FrameLocator.getByTestId
* since: v1.27
- returns: <[Locator]>
%%-template-locator-get-by-test-id-%%
### param: FrameLocator.getByTestId.testId = %%-locator-get-by-test-id-test-id-%%
* since: v1.27
## method: FrameLocator.getByText
* since: v1.27
- returns: <[Locator]>
%%-template-locator-get-by-text-%%
### param: FrameLocator.getByText.text = %%-locator-get-by-text-text-%%
2022-11-21 18:30:32 +01:00
### option: FrameLocator.getByText.exact = %%-locator-get-by-text-exact-%%
## method: FrameLocator.getByTitle
* since: v1.27
- returns: <[Locator]>
%%-template-locator-get-by-title-%%
### param: FrameLocator.getByTitle.text = %%-locator-get-by-text-text-%%
2022-11-21 18:30:32 +01:00
### option: FrameLocator.getByTitle.exact = %%-locator-get-by-text-exact-%%
## method: FrameLocator.last
2022-07-06 02:24:50 +02:00
* since: v1.17
- returns: <[FrameLocator]>
Returns locator to the last matching frame.
## method: FrameLocator.locator
2022-07-06 02:24:50 +02:00
* since: v1.17
- returns: <[Locator]>
2022-09-27 19:29:34 +02:00
%%-template-locator-locator-%%
### param: FrameLocator.locator.selectorOrLocator = %%-find-selector-or-locator-%%
2022-07-06 02:24:50 +02:00
* since: v1.17
2022-11-21 18:30:32 +01:00
2022-07-06 02:24:50 +02:00
### option: FrameLocator.locator.-inline- = %%-locator-options-list-v1.14-%%
* since: v1.17
### option: FrameLocator.locator.hasNot = %%-locator-option-has-not-%%
* since: v1.33
### option: FrameLocator.locator.hasNotText = %%-locator-option-has-not-text-%%
* since: v1.33
## method: FrameLocator.nth
2022-07-06 02:24:50 +02:00
* since: v1.17
- returns: <[FrameLocator]>
Returns locator to the n-th matching frame. It's zero based, `nth(0)` selects the first frame.
### param: FrameLocator.nth.index
2022-07-06 02:24:50 +02:00
* since: v1.17
- `index` <[int]>
## method: FrameLocator.owner
* since: v1.43
- returns: <[Locator]>
Returns a [Locator] object pointing to the same `iframe` as this frame locator.
Useful when you have a [FrameLocator] object obtained somewhere, and later on would like to interact with the `iframe` element.
For a reverse operation, use [`method: Locator.contentFrame`].
**Usage**
```js
const frameLocator = page.frameLocator('iframe[name="embedded"]');
// ...
const locator = frameLocator.owner();
await expect(locator).toBeVisible();
```
```java
FrameLocator frameLocator = page.frameLocator("iframe[name=\"embedded\"]");
// ...
Locator locator = frameLocator.owner();
assertThat(locator).isVisible();
```
```python async
frame_locator = page.frame_locator("iframe[name=\"embedded\"]")
# ...
locator = frame_locator.owner
await expect(locator).to_be_visible()
```
```python sync
frame_locator = page.frame_locator("iframe[name=\"embedded\"]")
# ...
locator = frame_locator.owner
expect(locator).to_be_visible()
```
```csharp
var frameLocator = Page.FrameLocator("iframe[name=\"embedded\"]");
// ...
var locator = frameLocator.Owner;
await Expect(locator).ToBeVisibleAsync();
```