mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
128 lines
3.3 KiB
Markdown
128 lines
3.3 KiB
Markdown
![]() |
---
|
||
|
id: frames
|
||
|
title: "Pages and frames"
|
||
|
---
|
||
|
|
||
|
<!-- TOC -->
|
||
|
|
||
|
## Frames
|
||
|
|
||
|
A [Page] can have one or more [Frame] objects attached to it. Each page has a main frame and page-level interactions
|
||
|
(like `click`) are assumed to operate in the main frame.
|
||
|
|
||
|
A page can have additional frames attached with the `iframe` HTML tag. These frames can be accessed for interactions
|
||
|
inside the frame.
|
||
|
|
||
|
```js
|
||
|
// Locate element inside frame
|
||
|
const username = await page.frameLocator('.frame-class').locator('#username-input');
|
||
|
await username.fill('John');
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
// Locate element inside frame
|
||
|
Locator username = page.frameLocator(".frame-class").locator("#username-input");
|
||
|
username.fill("John");
|
||
|
```
|
||
|
|
||
|
```python async
|
||
|
# Locate element inside frame
|
||
|
username = await page.frame_locator('.frame-class').locator('#username-input')
|
||
|
await username.fill('John')
|
||
|
```
|
||
|
|
||
|
```python sync
|
||
|
# Locate element inside frame
|
||
|
# Get frame using any other selector
|
||
|
username = page.frame_locator('.frame-class').locator('#username-input')
|
||
|
username.fill('John')
|
||
|
```
|
||
|
|
||
|
```csharp
|
||
|
// Locate element inside frame
|
||
|
var username = await page.FrameLocator(".frame-class").Locator("#username-input");
|
||
|
await username.FillAsync("John");
|
||
|
```
|
||
|
|
||
|
## Frame objects
|
||
|
|
||
|
One can access frame objects using the [`method: Page.frame`] API:
|
||
|
|
||
|
```js
|
||
|
// Get frame using the frame's name attribute
|
||
|
const frame = page.frame('frame-login');
|
||
|
|
||
|
// Get frame using frame's URL
|
||
|
const frame = page.frame({ url: /.*domain.*/ });
|
||
|
|
||
|
// Get frame using any other selector
|
||
|
const frameElementHandle = await page.$('.frame-class');
|
||
|
const frame = await frameElementHandle.contentFrame();
|
||
|
|
||
|
// Interact with the frame
|
||
|
await frame.fill('#username-input', 'John');
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
// Get frame using the frame"s name attribute
|
||
|
Frame frame = page.frame("frame-login");
|
||
|
|
||
|
// Get frame using frame"s URL
|
||
|
Frame frame = page.frameByUrl(Pattern.compile(".*domain.*"));
|
||
|
|
||
|
// Get frame using any other selector
|
||
|
ElementHandle frameElementHandle = page.querySelector(".frame-class");
|
||
|
Frame frame = frameElementHandle.contentFrame();
|
||
|
|
||
|
// Interact with the frame
|
||
|
frame.fill("#username-input", "John");
|
||
|
```
|
||
|
|
||
|
```python async
|
||
|
# Get frame using the frame's name attribute
|
||
|
frame = page.frame('frame-login')
|
||
|
|
||
|
# Get frame using frame's URL
|
||
|
frame = page.frame(url=r'.*domain.*')
|
||
|
|
||
|
# Get frame using any other selector
|
||
|
frame_element_handle = await page.query_selector('.frame-class')
|
||
|
frame = await frame_element_handle.content_frame()
|
||
|
|
||
|
# Interact with the frame
|
||
|
await frame.fill('#username-input', 'John')
|
||
|
```
|
||
|
|
||
|
```python sync
|
||
|
# Get frame using the frame's name attribute
|
||
|
frame = page.frame('frame-login')
|
||
|
|
||
|
# Get frame using frame's URL
|
||
|
frame = page.frame(url=r'.*domain.*')
|
||
|
|
||
|
# Get frame using any other selector
|
||
|
frame_element_handle = page.query_selector('.frame-class')
|
||
|
frame = frame_element_handle.content_frame()
|
||
|
|
||
|
# Interact with the frame
|
||
|
frame.fill('#username-input', 'John')
|
||
|
```
|
||
|
|
||
|
```csharp
|
||
|
// Create a page.
|
||
|
var page = await context.NewPageAsync();
|
||
|
|
||
|
// Get frame using the frame's name attribute
|
||
|
var frame = page.Frame("frame-login");
|
||
|
|
||
|
// Get frame using frame's URL
|
||
|
var frame = page.FrameByUrl("*domain.");
|
||
|
|
||
|
// Get frame using any other selector
|
||
|
var frameElementHandle = await page.QuerySelectorAsync(".frame-class");
|
||
|
var frame = await frameElementHandle.ContentFrameAsync();
|
||
|
|
||
|
// Interact with the frame
|
||
|
await frame.FillAsync("#username-input", "John");
|
||
|
```
|