跳至主要内容

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

属性

chromium

Added before v1.9 playwright.chromium

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

用法

playwright.chromium

类型


设备

Added before v1.9 playwright.devices

返回一个设备字典,用于browser.newContext()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();
})();

用法

playwright.devices

类型


错误

Added before v1.9 playwright.errors

如果Playwright方法无法完成请求,可能会抛出错误。例如,locator.waitFor()可能会失败,如果在给定的时间范围内选择器没有匹配到任何节点。

对于某些类型的错误,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.errors

类型


firefox

Added before v1.9 playwright.firefox

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

用法

playwright.firefox

类型


请求

Added in: v1.16 playwright.request

提供可用于Web API测试的API接口。

用法

playwright.request

类型


选择器

Added before v1.9 playwright.selectors

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

用法

playwright.selectors

类型


webkit

Added before v1.9 playwright.webkit

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

用法

playwright.webkit

类型