mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
278 lines
7.9 KiB
Markdown
278 lines
7.9 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();
|
|
})();
|
|
```
|
|
|
|
```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();
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
```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)
|
|
```
|
|
|
|
```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();
|
|
|
|
await page.GotoAsync("https://www.microsoft.com");
|
|
// other actions...
|
|
}
|
|
}
|
|
```
|
|
|
|
## property: Playwright.chromium
|
|
- type: <[BrowserType]>
|
|
|
|
This object can be used to launch or connect to Chromium, returning instances of [Browser].
|
|
|
|
## property: Playwright.devices
|
|
* langs: js, python
|
|
- type: <[Object]>
|
|
|
|
Returns a dictionary of devices to be used with [`method: Browser.newContext`] or [`method: Browser.newPage`].
|
|
|
|
```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();
|
|
})();
|
|
```
|
|
|
|
```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)
|
|
```
|
|
|
|
## property: Playwright.devices
|
|
* langs: csharp
|
|
- type: <[IReadOnlyDictionary<string, BrowserNewContextOptions>]>
|
|
|
|
Returns a dictionary of devices to be used with [`method: Browser.newContext`] or [`method: Browser.newPage`].
|
|
|
|
```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();
|
|
await using var context = await browser.NewContextAsync(Playwright.Devices["iPhone 6"]);
|
|
|
|
var page = await context.NewPageAsync();
|
|
await page.GotoAsync("https://www.theverge.com");
|
|
// other actions...
|
|
}
|
|
}
|
|
```
|
|
|
|
## property: Playwright.errors
|
|
* langs: js
|
|
- 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.
|
|
}
|
|
}
|
|
```
|
|
|
|
```python async
|
|
try:
|
|
await page.wait_for_selector(".foo")
|
|
except TimeoutError as e:
|
|
# do something if this is a timeout.
|
|
```
|
|
|
|
```python sync
|
|
try:
|
|
page.wait_for_selector(".foo")
|
|
except TimeoutError as e:
|
|
# 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 [Browser].
|
|
|
|
## async method: Playwright.newRequest
|
|
* langs: js
|
|
- returns: <[FetchRequest]>
|
|
|
|
**experimental** Creates new instances of [FetchRequest].
|
|
|
|
### option: Playwright.newRequest.useragent = %%-context-option-useragent-%%
|
|
|
|
### option: Playwright.newRequest.extraHTTPHeaders = %%-context-option-extrahttpheaders-%%
|
|
|
|
### option: Playwright.newRequest.httpCredentials = %%-context-option-httpcredentials-%%
|
|
|
|
### option: Playwright.newRequest.proxy = %%-browser-option-proxy-%%
|
|
|
|
### option: Playwright.newRequest.timeout
|
|
- `timeout` <[float]>
|
|
|
|
Maximum time in milliseconds to wait for the response. Defaults to
|
|
`30000` (30 seconds). Pass `0` to disable timeout.
|
|
|
|
### option: Playwright.newRequest.ignoreHTTPSErrors = %%-context-option-ignorehttpserrors-%%
|
|
|
|
### option: Playwright.newRequest.baseURL
|
|
- `baseURL` <[string]>
|
|
|
|
When using [`method: FetchRequest.get`], [`method: FetchRequest.post`], [`method: FetchRequest.fetch`] it takes the base URL in consideration by using the [`URL()`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL) constructor for building the corresponding URL. Examples:
|
|
* baseURL: `http://localhost:3000` and sending rquest to `/bar.html` results in `http://localhost:3000/bar.html`
|
|
* baseURL: `http://localhost:3000/foo/` and sending rquest to `./bar.html` results in `http://localhost:3000/foo/bar.html`
|
|
|
|
### option: Playwright.newRequest.storageState
|
|
- `storageState` <[path]|[Object]>
|
|
- `cookies` <[Array]<[Object]>>
|
|
- `name` <[string]>
|
|
- `value` <[string]>
|
|
- `domain` <[string]>
|
|
- `path` <[string]>
|
|
- `expires` <[float]> Unix time in seconds.
|
|
- `httpOnly` <[boolean]>
|
|
- `secure` <[boolean]>
|
|
- `sameSite` <[SameSiteAttribute]<"Strict"|"Lax"|"None">>
|
|
- `origins` <[Array]<[Object]>>
|
|
- `origin` <[string]>
|
|
- `localStorage` <[Array]<[Object]>>
|
|
- `name` <[string]>
|
|
- `value` <[string]>
|
|
|
|
Populates context with given storage state. This option can be used to initialize context with logged-in information
|
|
obtained via [`method: BrowserContext.storageState`] or [`method: FetchRequest.storageState`]. Either a path to the
|
|
file with saved storage, or the value returned by one of [`method: BrowserContext.storageState`] or
|
|
[`method: FetchRequest.storageState`] methods.
|
|
|
|
## property: Playwright.selectors
|
|
- type: <[Selectors]>
|
|
|
|
Selectors can be used to install custom selector engines. See
|
|
[Working with selectors](./selectors.md) for more information.
|
|
|
|
## property: Playwright.webkit
|
|
- type: <[BrowserType]>
|
|
|
|
This object can be used to launch or connect to WebKit, returning instances of [Browser].
|