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.
用法
playwright.chromium
类型
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();
})();
用法
playwright.devices
类型
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.
}
}
用法
playwright.errors
类型
- Object
-
TimeoutErrorfunctionTimeoutError 类。
-
firefox
Added before v1.9此对象可用于启动或连接到 Firefox,并返回 Browser 实例。
🌐 This object can be used to launch or connect to Firefox, returning instances of Browser.
用法
playwright.firefox
类型
request
Added in: v1.16公开可用于 Web API 测试的 API。
🌐 Exposes API that can be used for the Web API testing.
用法
playwright.request
类型
selectors
Added before v1.9选择器可用于安装自定义选择器引擎。有关更多信息,请参阅可扩展性。
🌐 Selectors can be used to install custom selector engines. See extensibility for more information.
用法
playwright.selectors
类型
webkit
Added before v1.9此对象可用于启动或连接到 WebKit,并返回 Browser 的实例。
🌐 This object can be used to launch or connect to WebKit, returning instances of Browser.
用法
playwright.webkit
类型