Skip to main content

Playwright Library

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

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();
})();

playwright.chromium

Added in: v1.8

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

playwright.devices

Added in: v1.8

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

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();
})();

playwright.errors

Added in: v1.8

Playwright 方法如果无法满足请求可能会抛出错误。例如,如果选取器在给定的时间范围内不匹配任何节点,locator.waitFor([options]) 可能会失败。

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

处理超时错误的示例:

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

playwright.firefox

Added in: v1.8

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

playwright.request

Added in: v1.16

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

playwright.selectors

Added in: v1.8

选取器可用于安装自定义选取器引擎。有关更多信息,请参阅使用选取器

playwright.webkit

Added in: v1.8

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