mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
80 lines
2.6 KiB
Markdown
80 lines
2.6 KiB
Markdown
![]() |
# class: Playwright
|
||
|
|
||
|
Playwright module provides a method to launch a browser instance.
|
||
|
The following is a typical example of using Playwright to drive automation:
|
||
|
```js
|
||
|
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();
|
||
|
})();
|
||
|
```
|
||
|
|
||
|
By default, the `playwright` NPM package automatically downloads browser executables during installation. The `playwright-core` NPM package can be used to skip automatic downloads.
|
||
|
|
||
|
## property: Playwright.chromium
|
||
|
- type: <[BrowserType]>
|
||
|
|
||
|
This object can be used to launch or connect to Chromium, returning instances of [ChromiumBrowser].
|
||
|
|
||
|
## property: Playwright.devices
|
||
|
- type: <[Object]>
|
||
|
|
||
|
Returns a list of devices to be used with [`method: Browser.newContext`] or [`method: Browser.newPage`]. Actual list of devices can be found in [src/server/deviceDescriptors.ts](https://github.com/Microsoft/playwright/blob/master/src/server/deviceDescriptors.ts).
|
||
|
|
||
|
```js
|
||
|
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();
|
||
|
})();
|
||
|
```
|
||
|
|
||
|
## property: Playwright.errors
|
||
|
- type: <[Object]>
|
||
|
- `TimeoutError` <[function]> A class of [TimeoutError].
|
||
|
|
||
|
Playwright methods might throw errors if they are unable to fulfill a request. For example, [`method: Page.waitForSelector`]
|
||
|
might fail if the selector doesn't match any nodes during the given timeframe.
|
||
|
|
||
|
For certain types of errors Playwright uses specific error classes.
|
||
|
These classes are available via [`playwright.errors`](#playwrighterrors).
|
||
|
|
||
|
An example of handling a timeout error:
|
||
|
```js
|
||
|
try {
|
||
|
await page.waitForSelector('.foo');
|
||
|
} catch (e) {
|
||
|
if (e instanceof playwright.errors.TimeoutError) {
|
||
|
// Do something if this is a timeout.
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## property: Playwright.firefox
|
||
|
- type: <[BrowserType]>
|
||
|
|
||
|
This object can be used to launch or connect to Firefox, returning instances of [FirefoxBrowser].
|
||
|
|
||
|
## property: Playwright.selectors
|
||
|
- type: <[Selectors]>
|
||
|
|
||
|
Selectors can be used to install custom selector engines. See [Working with selectors](./selectors.md#working-with-selectors) for more information.
|
||
|
|
||
|
## property: Playwright.webkit
|
||
|
- type: <[BrowserType]>
|
||
|
|
||
|
This object can be used to launch or connect to WebKit, returning instances of [WebKitBrowser].
|