Skip to main content

框架

介绍

¥Introduction

Page 可以附加一个或多个 Frame 对象。每个页面都有一个主框架,并且假设页面级交互(如 click)在主框架中操作。

¥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.

页面可以附加带有 iframe HTML 标记的附加框架。可以访问这些框架以在框架内进行交互。

¥A page can have additional frames attached with the iframe HTML tag. These frames can be accessed for interactions inside the frame.

// Locate element inside frame
var username = await page.FrameLocator(".frame-class").GetByLabel("User Name");
await username.FillAsync("John");

框架对象

¥Frame objects

可以使用 Page.Frame() API 访问框架对象:

¥One can access frame objects using the Page.Frame() API:

// 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.EvaluateAsync("window.frames[1]");
var frame = await frameElementHandle.ContentFrameAsync();

// Interact with the frame
await frame.FillAsync("#username-input", "John");