跳至主要内容

Playwright

Playwright 模块提供了一种启动浏览器实例的方法。以下是使用 Playwright 驱动自动化的典型示例

using Microsoft.Playwright;
using System.Threading.Tasks;

class PlaywrightExample
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();

await page.GotoAsync("https://www.microsoft.com");
// other actions...
}
}

属性

APIRequest

新增于:v1.16 playwright.APIRequest

公开了可用于 Web API 测试的 API。

用法

Playwright.APIRequest

类型


Chromium

新增于 v1.9 之前 playwright.Chromium

此对象可用于启动或连接到 Chromium,并返回 Browser 的实例。

用法

Playwright.Chromium

类型


设备

新增于 v1.9 之前 playwright.Devices

返回一个设备字典,用于 Browser.NewContextAsync()Browser.NewPageAsync()

using Microsoft.Playwright;
using System.Threading.Tasks;

class PlaywrightExample
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Webkit.LaunchAsync();
await using var context = await browser.NewContextAsync(playwright.Devices["iPhone 6"]);

var page = await context.NewPageAsync();
await page.GotoAsync("https://www.theverge.com");
// other actions...
}
}

用法

Playwright.Devices

类型


Firefox

新增于 v1.9 之前 playwright.Firefox

此对象可用于启动或连接到 Firefox,并返回 Browser 的实例。

用法

Playwright.Firefox

类型


选择器

新增于 v1.9 之前 playwright.Selectors

选择器可用于安装自定义选择器引擎。有关更多信息,请参阅 可扩展性

用法

Playwright.Selectors

类型


Webkit

新增于 v1.9 之前 playwright.Webkit

此对象可用于启动或连接到 WebKit,并返回 Browser 的实例。

用法

Playwright.Webkit

类型