Skip to content

Latest commit

 

History

History
293 lines (234 loc) · 7.41 KB

class-playwright.md

File metadata and controls

293 lines (234 loc) · 7.41 KB

class: Playwright

  • since: v1.8

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();
})();
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();
    }
  }
}
import asyncio
from playwright.async_api import async_playwright, Playwright

async def run(playwright: 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())
from playwright.sync_api import sync_playwright, Playwright

def run(playwright: 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)
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

  • since: v1.8
  • type: <[BrowserType]>

This object can be used to launch or connect to Chromium, returning instances of [Browser].

property: Playwright.devices

  • since: v1.8
  • langs: js, python
  • type: <[Object]>

Returns a dictionary of devices to be used with [method: Browser.newContext] or [method: 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();
})();
import asyncio
from playwright.async_api import async_playwright, Playwright

async def run(playwright: 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())
from playwright.sync_api import sync_playwright, Playwright

def run(playwright: 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

  • since: v1.8
  • langs: csharp
  • type: <[IReadOnlyDictionary<string, BrowserNewContextOptions>]>

Returns a dictionary of devices to be used with [method: Browser.newContext] or [method: Browser.newPage].

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

  • since: v1.8
  • 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: Locator.waitFor] 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.

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.
  }
}
try:
  await page.wait_for_selector(".foo")
except TimeoutError as e:
  pass
  # do something if this is a timeout.
try:
  page.wait_for_selector(".foo")
except TimeoutError as e:
  pass
  # do something if this is a timeout.

property: Playwright.firefox

  • since: v1.8
  • type: <[BrowserType]>

This object can be used to launch or connect to Firefox, returning instances of [Browser].

property: Playwright.request

  • since: v1.16
  • langs:
    • alias-csharp: APIRequest
  • type: <[APIRequest]>

Exposes API that can be used for the Web API testing.

property: Playwright.selectors

  • since: v1.8
  • type: <[Selectors]>

Selectors can be used to install custom selector engines. See extensibility for more information.

property: Playwright.webkit

  • since: v1.8
  • type: <[BrowserType]>

This object can be used to launch or connect to WebKit, returning instances of [Browser].

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.

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.

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.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()

browser = playwright.chromium.launch()
page = browser.new_page()
page.goto("https://playwright.dev/")
page.screenshot(path="example.png")
browser.close()

playwright.stop()