2021-11-08 18:58:24 +01:00
# class: FrameLocator
2022-07-06 02:24:50 +02:00
* since: v1.17
2021-11-08 18:58:24 +01:00
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
2022-10-04 02:02:46 +02:00
const locator = page.frameLocator('#my-frame').getByText('Submit');
2021-11-08 18:58:24 +01:00
await locator.click();
```
```java
2022-10-27 00:30:22 +02:00
Locator locator = page.frameLocator("#my-frame").getByText("Submit");
2021-11-08 18:58:24 +01:00
locator.click();
```
```python async
2022-10-27 00:30:22 +02:00
locator = page.frame_locator("#my-frame").get_by_text("Submit")
2021-11-08 18:58:24 +01:00
await locator.click()
```
```python sync
2022-10-27 00:30:22 +02:00
locator = page.frame_locator("my-frame").get_by_text("Submit")
2021-11-08 18:58:24 +01:00
locator.click()
```
```csharp
2022-10-27 00:30:22 +02:00
var locator = page.FrameLocator("#my-frame").GetByText("Submit");
2021-11-08 18:58:24 +01:00
await locator.ClickAsync();
```
2021-11-09 23:14:20 +01:00
**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.
2021-11-09 23:14:20 +01:00
```js
// Throws if there are several frames in DOM:
2022-10-04 02:02:46 +02:00
await page.frameLocator('.result-frame').getByRole('button').click();
2021-11-09 23:14:20 +01:00
// Works because we explicitly tell locator to pick the first frame:
2022-10-04 02:02:46 +02:00
await page.frameLocator('.result-frame').first().getByRole('button').click();
2021-11-09 23:14:20 +01:00
```
```python async
# Throws if there are several frames in DOM:
2022-10-04 02:02:46 +02:00
await page.frame_locator('.result-frame').get_by_role('button').click()
2021-11-09 23:14:20 +01:00
# Works because we explicitly tell locator to pick the first frame:
2022-10-04 02:02:46 +02:00
await page.frame_locator('.result-frame').first.get_by_role('button').click()
2021-11-09 23:14:20 +01:00
```
```python sync
# Throws if there are several frames in DOM:
2022-10-04 02:02:46 +02:00
page.frame_locator('.result-frame').get_by_role('button').click()
2021-11-09 23:14:20 +01:00
# Works because we explicitly tell locator to pick the first frame:
2022-10-04 02:02:46 +02:00
page.frame_locator('.result-frame').first.get_by_role('button').click()
2021-11-09 23:14:20 +01:00
```
```java
// Throws if there are several frames in DOM:
2022-10-04 02:02:46 +02:00
page.frame_locator(".result-frame").getByRole("button").click();
2021-11-09 23:14:20 +01:00
// Works because we explicitly tell locator to pick the first frame:
2022-10-04 02:02:46 +02:00
page.frame_locator(".result-frame").first().getByRole("button").click();
2021-11-09 23:14:20 +01:00
```
```csharp
// Throws if there are several frames in DOM:
2022-10-04 02:02:46 +02:00
await page.FrameLocator(".result-frame").GetByRole("button").ClickAsync();
2021-11-09 23:14:20 +01:00
// Works because we explicitly tell locator to pick the first frame:
2022-10-04 02:02:46 +02:00
await page.FrameLocator(".result-frame").First.getByRole("button").ClickAsync();
2021-11-09 23:14:20 +01:00
```
2021-12-10 00:21:04 +01:00
**Converting Locator to FrameLocator**
If you have a [Locator] object pointing to an `iframe` it can be converted to [FrameLocator] using [`:scope` ](https://developer.mozilla.org/en-US/docs/Web/CSS/:scope ) CSS selector:
```js
const frameLocator = locator.frameLocator(':scope');
```
```java
Locator frameLocator = locator.frameLocator(':scope');
```
```python async
frameLocator = locator.frame_locator(":scope");
```
```python sync
frameLocator = locator.frame_locator(":scope");
```
```csharp
var frameLocator = locator.FrameLocator(":scope");
```
2021-11-09 23:14:20 +01:00
## method: FrameLocator.first
2022-07-06 02:24:50 +02:00
* since: v1.17
2021-11-09 23:14:20 +01:00
- returns: < [FrameLocator]>
Returns locator to the first matching frame.
2021-11-08 18:58:24 +01:00
## method: FrameLocator.frameLocator
2022-07-06 02:24:50 +02:00
* since: v1.17
2021-11-08 18:58:24 +01:00
- 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
2021-11-08 18:58:24 +01:00
2022-09-30 06:45:44 +02:00
## method: FrameLocator.getByAltText
* since: v1.27
- returns: < [Locator]>
%%-template-locator-get-by-alt-text-%%
### param: FrameLocator.getByAltText.text = %%-locator-get-by-text-text-%%
### option: FrameLocator.getByAltText.exact = %%-locator-get-by-text-exact-%%
2022-10-04 19:29:26 +02:00
## method: FrameLocator.getByLabel
2022-09-29 20:06:58 +02:00
* since: v1.27
- returns: < [Locator]>
%%-template-locator-get-by-label-text-%%
2022-10-04 19:29:26 +02:00
### param: FrameLocator.getByLabel.text = %%-locator-get-by-text-text-%%
### option: FrameLocator.getByLabel.exact = %%-locator-get-by-text-exact-%%
2022-09-29 20:06:58 +02:00
2022-10-04 19:29:26 +02:00
## method: FrameLocator.getByPlaceholder
2022-09-30 03:12:49 +02:00
* since: v1.27
- returns: < [Locator]>
%%-template-locator-get-by-placeholder-text-%%
2022-10-04 19:29:26 +02:00
### param: FrameLocator.getByPlaceholder.text = %%-locator-get-by-text-text-%%
### option: FrameLocator.getByPlaceholder.exact = %%-locator-get-by-text-exact-%%
2022-09-30 03:12:49 +02:00
2022-09-28 01:13:56 +02:00
## method: FrameLocator.getByRole
* since: v1.27
- returns: < [Locator]>
%%-template-locator-get-by-role-%%
### param: FrameLocator.getByRole.role = %%-locator-get-by-role-role-%%
### option: FrameLocator.getByRole.-inline- = %%-locator-get-by-role-option-list-v1.27-%%
* since: v1.27
2022-09-28 06:06:07 +02:00
## 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
2022-09-28 01:13:56 +02:00
## method: FrameLocator.getByText
* since: v1.27
- returns: < [Locator]>
%%-template-locator-get-by-text-%%
### param: FrameLocator.getByText.text = %%-locator-get-by-text-text-%%
### option: FrameLocator.getByText.exact = %%-locator-get-by-text-exact-%%
2022-09-30 06:45:44 +02:00
## method: FrameLocator.getByTitle
* since: v1.27
- returns: < [Locator]>
%%-template-locator-get-by-title-%%
### param: FrameLocator.getByTitle.text = %%-locator-get-by-text-text-%%
### option: FrameLocator.getByTitle.exact = %%-locator-get-by-text-exact-%%
2021-11-09 23:14:20 +01:00
## method: FrameLocator.last
2022-07-06 02:24:50 +02:00
* since: v1.17
2021-11-09 23:14:20 +01:00
- returns: < [FrameLocator]>
Returns locator to the last matching frame.
2021-11-08 18:58:24 +01:00
## method: FrameLocator.locator
2022-07-06 02:24:50 +02:00
* since: v1.17
2021-11-08 18:58:24 +01:00
- returns: < [Locator]>
2022-09-27 19:29:34 +02:00
%%-template-locator-locator-%%
2021-11-08 18:58:24 +01:00
### param: FrameLocator.locator.selector = %%-find-selector-%%
2022-07-06 02:24:50 +02:00
* since: v1.17
### option: FrameLocator.locator.-inline- = %%-locator-options-list-v1.14-%%
* since: v1.17
2021-11-09 23:14:20 +01:00
## method: FrameLocator.nth
2022-07-06 02:24:50 +02:00
* since: v1.17
2021-11-09 23:14:20 +01:00
- returns: < [FrameLocator]>
2022-03-14 21:06:44 +01:00
Returns locator to the n-th matching frame. It's zero based, `nth(0)` selects the first frame.
2021-11-09 23:14:20 +01:00
### param: FrameLocator.nth.index
2022-07-06 02:24:50 +02:00
* since: v1.17
2021-11-09 23:14:20 +01:00
- `index` < [int]>