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 before v1.9该对象可用于启动或连接到 Chromium,返回 Browser 的实例。
¥This object can be used to launch or connect to Chromium, returning instances of Browser.
用法
¥Usage
playwright.chromium
类型
¥Type
devices
Added before v1.9返回与 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 before v1.9如果 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 before v1.9该对象可用于启动或连接到 Firefox,返回 Browser 的实例。
¥This object can be used to launch or connect to Firefox, returning instances of Browser.
用法
¥Usage
playwright.firefox
类型
¥Type
request
Added in: v1.16公开可用于 Web API 测试的 API。
¥Exposes API that can be used for the Web API testing.
用法
¥Usage
playwright.request
类型
¥Type
selectors
Added before v1.9选择器可用于安装自定义选择器引擎。请参阅 extensibility 了解更多信息。
¥Selectors can be used to install custom selector engines. See extensibility for more information.
用法
¥Usage
playwright.selectors
类型
¥Type
webkit
Added before v1.9该对象可用于启动或连接到 WebKit,返回 Browser 的实例。
¥This object can be used to launch or connect to WebKit, returning instances of Browser.
用法
¥Usage
playwright.webkit
类型
¥Type