Skip to main content

Playwright 库

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

Playwright module provides a method to launch a browser instance. The following is a typical example of using Playwright to drive automation:

const { chromium, firefox, webkit } = require('playwright');

(async () => {
const browser = await chromium.launch(); // Or 'firefox' or 'webkit'.
const page = await browser.newPage();
await page.goto('http://example.com');
// other actions...
await browser.close();
})();

属性

Properties

chromium

Added in: v1.8 playwright.chromium

该对象可用于启动或连接到 Chromium,返回 浏览器 的实例。

This object can be used to launch or connect to Chromium, returning instances of Browser.

用法

Usage

playwright.chromium

类型

Type


devices

Added in: v1.8 playwright.devices

返回与 browser.newContext()browser.newPage() 一起使用的设备字典。

Returns a dictionary of devices to be used with browser.newContext() or browser.newPage().

const { webkit, devices } = require('playwright');
const iPhone = devices['iPhone 6'];

(async () => {
const browser = await webkit.launch();
const context = await browser.newContext({
...iPhone
});
const page = await context.newPage();
await page.goto('http://example.com');
// other actions...
await browser.close();
})();

用法

Usage

playwright.devices

类型

Type


errors

Added in: v1.8 playwright.errors

如果 Playwright 方法无法满足请求,则可能会引发错误。例如,如果选择器在给定时间范围内未匹配任何节点,则 locator.waitFor() 可能会失败。

Playwright methods might throw errors if they are unable to fulfill a request. For example, locator.waitFor() might fail if the selector doesn't match any nodes during the given timeframe.

对于某些类型的错误,Playwright 使用特定的错误类别。这些类可通过 playwright.errors 获得。

For certain types of errors Playwright uses specific error classes. These classes are available via playwright.errors.

处理超时错误的示例:

An example of handling a timeout error:

try {
await page.locator('.foo').waitFor();
} catch (e) {
if (e instanceof playwright.errors.TimeoutError) {
// Do something if this is a timeout.
}
}

用法

Usage

playwright.errors

类型

Type

TimeoutError 班。

A class of TimeoutError.


firefox

Added in: v1.8 playwright.firefox

该对象可用于启动或连接到 Firefox,返回 浏览器 的实例。

This object can be used to launch or connect to Firefox, returning instances of Browser.

用法

Usage

playwright.firefox

类型

Type


request

Added in: v1.16 playwright.request

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

Exposes API that can be used for the Web API testing.

用法

Usage

playwright.request

类型

Type


selectors

Added in: v1.8 playwright.selectors

选择器可用于安装自定义选择器引擎。请参阅 extensibility 了解更多信息。

Selectors can be used to install custom selector engines. See extensibility for more information.

用法

Usage

playwright.selectors

类型

Type


webkit

Added in: v1.8 playwright.webkit

该对象可用于启动或连接到 WebKit,返回 浏览器 的实例。

This object can be used to launch or connect to WebKit, returning instances of Browser.

用法

Usage

playwright.webkit

类型

Type