2021-01-07 11:46:05 -08:00
|
|
|
# class: Playwright
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
Playwright module provides a method to launch a browser instance. The following is a typical example of using Playwright
|
|
|
|
to drive automation:
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
```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();
|
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
2021-02-25 22:03:39 -08:00
|
|
|
```java
|
|
|
|
import com.microsoft.playwright.*;
|
|
|
|
|
|
|
|
public class Example {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
try (Playwright playwright = Playwright.create()) {
|
|
|
|
BrowserType chromium = playwright.chromium();
|
|
|
|
Browser browser = chromium.launch();
|
|
|
|
Page page = browser.newPage();
|
|
|
|
page.navigate("http://example.com");
|
|
|
|
// other actions...
|
|
|
|
browser.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
```python async
|
|
|
|
import asyncio
|
|
|
|
from playwright.async_api import async_playwright
|
|
|
|
|
|
|
|
async def run(playwright):
|
|
|
|
chromium = playwright.chromium # or "firefox" or "webkit".
|
|
|
|
browser = await chromium.launch()
|
|
|
|
page = await browser.new_page()
|
|
|
|
await page.goto("http://example.com")
|
|
|
|
# other actions...
|
|
|
|
await browser.close()
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with async_playwright() as playwright:
|
|
|
|
await run(playwright)
|
|
|
|
asyncio.run(main())
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
|
|
|
|
def run(playwright):
|
|
|
|
chromium = playwright.chromium # or "firefox" or "webkit".
|
|
|
|
browser = chromium.launch()
|
|
|
|
page = browser.new_page()
|
|
|
|
page.goto("http://example.com")
|
|
|
|
# other actions...
|
|
|
|
browser.close()
|
|
|
|
|
|
|
|
with sync_playwright() as playwright:
|
|
|
|
run(playwright)
|
|
|
|
```
|
|
|
|
|
2021-05-13 19:19:14 +02:00
|
|
|
```csharp
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
class PlaywrightExample
|
|
|
|
{
|
|
|
|
public static async Task Main()
|
|
|
|
{
|
|
|
|
using var playwright = await Playwright.CreateAsync();
|
|
|
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
|
|
|
var page = await browser.NewPageAsync();
|
|
|
|
|
2021-05-13 11:57:02 -07:00
|
|
|
await page.GotoAsync("https://www.microsoft.com");
|
2021-05-13 19:19:14 +02:00
|
|
|
// other actions...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## property: Playwright.chromium
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
- type: <[BrowserType]>
|
|
|
|
|
2021-04-02 09:47:14 +08:00
|
|
|
This object can be used to launch or connect to Chromium, returning instances of [Browser].
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
## property: Playwright.devices
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-02-11 12:26:10 -08:00
|
|
|
* langs: js, python
|
2021-01-07 11:46:05 -08:00
|
|
|
- type: <[Object]>
|
|
|
|
|
2021-01-12 12:14:27 -08:00
|
|
|
Returns a dictionary of devices to be used with [`method: Browser.newContext`] or [`method: Browser.newPage`].
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
```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();
|
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
```python async
|
|
|
|
import asyncio
|
|
|
|
from playwright.async_api import async_playwright
|
|
|
|
|
|
|
|
async def run(playwright):
|
|
|
|
webkit = playwright.webkit
|
|
|
|
iphone = playwright.devices["iPhone 6"]
|
|
|
|
browser = await webkit.launch()
|
|
|
|
context = await browser.new_context(**iphone)
|
|
|
|
page = await context.new_page()
|
|
|
|
await page.goto("http://example.com")
|
|
|
|
# other actions...
|
|
|
|
await browser.close()
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with async_playwright() as playwright:
|
|
|
|
await run(playwright)
|
|
|
|
asyncio.run(main())
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
|
|
|
|
def run(playwright):
|
|
|
|
webkit = playwright.webkit
|
|
|
|
iphone = playwright.devices["iPhone 6"]
|
|
|
|
browser = webkit.launch()
|
|
|
|
context = browser.new_context(**iphone)
|
|
|
|
page = context.new_page()
|
|
|
|
page.goto("http://example.com")
|
|
|
|
# other actions...
|
|
|
|
browser.close()
|
|
|
|
|
|
|
|
with sync_playwright() as playwright:
|
|
|
|
run(playwright)
|
|
|
|
```
|
|
|
|
|
2021-05-12 12:09:47 -03:00
|
|
|
## property: Playwright.devices
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-05-12 12:09:47 -03:00
|
|
|
* langs: csharp
|
2021-05-18 23:33:52 -07:00
|
|
|
- type: <[IReadOnlyDictionary<string, BrowserNewContextOptions>]>
|
2021-05-12 12:09:47 -03:00
|
|
|
|
|
|
|
Returns a dictionary of devices to be used with [`method: Browser.newContext`] or [`method: Browser.newPage`].
|
|
|
|
|
2021-05-13 19:19:14 +02:00
|
|
|
```csharp
|
|
|
|
using Microsoft.Playwright;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
class PlaywrightExample
|
|
|
|
{
|
|
|
|
public static async Task Main()
|
|
|
|
{
|
|
|
|
using var playwright = await Playwright.CreateAsync();
|
|
|
|
await using var browser = await playwright.Webkit.LaunchAsync();
|
2021-10-12 00:29:48 +03:00
|
|
|
await using var context = await browser.NewContextAsync(playwright.Devices["iPhone 6"]);
|
2021-05-13 19:19:14 +02:00
|
|
|
|
|
|
|
var page = await context.NewPageAsync();
|
2021-05-13 11:57:02 -07:00
|
|
|
await page.GotoAsync("https://www.theverge.com");
|
2021-05-13 19:19:14 +02:00
|
|
|
// other actions...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## property: Playwright.errors
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 16:12:25 -08:00
|
|
|
* langs: js
|
2021-01-07 11:46:05 -08:00
|
|
|
- type: <[Object]>
|
|
|
|
- `TimeoutError` <[function]> A class of [TimeoutError].
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
Playwright methods might throw errors if they are unable to fulfill a request. For example,
|
2021-11-10 11:30:25 -08:00
|
|
|
[`method: Locator.waitFor`] might fail if the selector doesn't match any nodes during the given timeframe.
|
2021-01-07 11:46:05 -08:00
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
For certain types of errors Playwright uses specific error classes. These classes are available via
|
2022-12-20 13:17:52 +01:00
|
|
|
[`playwright.errors`](#playwright-errors).
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
An example of handling a timeout error:
|
2021-01-14 07:48:56 -08:00
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
```js
|
|
|
|
try {
|
2021-11-10 11:30:25 -08:00
|
|
|
await page.locator('.foo').waitFor();
|
2021-01-07 11:46:05 -08:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof playwright.errors.TimeoutError) {
|
|
|
|
// Do something if this is a timeout.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
```python async
|
|
|
|
try:
|
2023-06-29 18:26:19 +02:00
|
|
|
await page.wait_for_selector(".foo")
|
2021-01-14 07:48:56 -08:00
|
|
|
except TimeoutError as e:
|
2023-06-29 18:26:19 +02:00
|
|
|
pass
|
|
|
|
# do something if this is a timeout.
|
2021-01-14 07:48:56 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
try:
|
2023-06-29 18:26:19 +02:00
|
|
|
page.wait_for_selector(".foo")
|
2021-01-14 07:48:56 -08:00
|
|
|
except TimeoutError as e:
|
2023-06-29 18:26:19 +02:00
|
|
|
pass
|
|
|
|
# do something if this is a timeout.
|
2021-01-14 07:48:56 -08:00
|
|
|
```
|
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## property: Playwright.firefox
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
- type: <[BrowserType]>
|
|
|
|
|
2021-04-02 09:47:14 +08:00
|
|
|
This object can be used to launch or connect to Firefox, returning instances of [Browser].
|
2021-01-07 11:46:05 -08:00
|
|
|
|
2021-10-05 17:53:19 -08:00
|
|
|
## property: Playwright.request
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.16
|
2022-05-23 22:12:57 +03:00
|
|
|
* langs:
|
|
|
|
- alias-csharp: APIRequest
|
2021-10-19 07:38:27 -07:00
|
|
|
- type: <[APIRequest]>
|
2021-10-05 13:56:34 -07:00
|
|
|
|
2021-10-05 17:53:19 -08:00
|
|
|
Exposes API that can be used for the Web API testing.
|
2021-10-05 13:56:34 -07:00
|
|
|
|
2021-01-07 11:46:05 -08:00
|
|
|
## property: Playwright.selectors
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
- type: <[Selectors]>
|
|
|
|
|
2021-01-14 07:48:56 -08:00
|
|
|
Selectors can be used to install custom selector engines. See
|
2022-12-02 21:48:37 -08:00
|
|
|
[extensibility](../extensibility.md) for more information.
|
2021-01-07 11:46:05 -08:00
|
|
|
|
|
|
|
## property: Playwright.webkit
|
2022-07-05 16:24:50 -08:00
|
|
|
* since: v1.8
|
2021-01-07 11:46:05 -08:00
|
|
|
- type: <[BrowserType]>
|
|
|
|
|
2021-04-02 09:47:14 +08:00
|
|
|
This object can be used to launch or connect to WebKit, returning instances of [Browser].
|
2023-02-09 18:24:32 -08:00
|
|
|
|
2023-02-10 15:14:28 -08:00
|
|
|
## method: Playwright.close
|
|
|
|
* since: v1.9
|
|
|
|
* langs: java
|
|
|
|
|
|
|
|
Terminates this instance of Playwright, will also close all created browsers if they are still running.
|
|
|
|
|
|
|
|
## method: Playwright.create
|
|
|
|
* since: v1.10
|
|
|
|
* langs: java
|
|
|
|
- returns: <[Playwright]>
|
|
|
|
|
|
|
|
Launches new Playwright driver process and connects to it. [`method: Playwright.close`] should be called when the instance is no longer needed.
|
|
|
|
|
|
|
|
```java
|
|
|
|
Playwright playwright = Playwright.create();
|
|
|
|
Browser browser = playwright.webkit().launch();
|
|
|
|
Page page = browser.newPage();
|
|
|
|
page.navigate("https://www.w3.org/");
|
|
|
|
playwright.close();
|
|
|
|
```
|
|
|
|
|
|
|
|
### option: Playwright.create.env
|
|
|
|
* since: v1.13
|
|
|
|
* langs: java
|
|
|
|
- `env` <[Object]<[string], [string]>>
|
|
|
|
|
|
|
|
Additional environment variables that will be passed to the driver process. By default driver
|
|
|
|
process inherits environment variables of the Playwright process.
|
|
|
|
|
2023-02-09 18:24:32 -08:00
|
|
|
## async method: Playwright.stop
|
|
|
|
* since: v1.8
|
|
|
|
* langs: python
|
|
|
|
|
|
|
|
Terminates this instance of Playwright in case it was created bypassing the Python context manager. This is useful in REPL applications.
|
|
|
|
|
|
|
|
```py
|
2023-06-29 18:26:19 +02:00
|
|
|
from playwright.sync_api import sync_playwright
|
2023-02-09 18:24:32 -08:00
|
|
|
|
2023-06-29 18:26:19 +02:00
|
|
|
playwright = sync_playwright().start()
|
2023-02-09 18:24:32 -08:00
|
|
|
|
2023-06-29 18:26:19 +02:00
|
|
|
browser = playwright.chromium.launch()
|
|
|
|
page = browser.new_page()
|
|
|
|
page.goto("https://playwright.dev/")
|
|
|
|
page.screenshot(path="example.png")
|
|
|
|
browser.close()
|
2023-02-09 18:24:32 -08:00
|
|
|
|
2023-06-29 18:26:19 +02:00
|
|
|
playwright.stop()
|
2023-02-09 18:24:32 -08:00
|
|
|
```
|