Playwright module provides browser launching
The Playwright module provides a method to launch a browser instance. A typical usage pattern involves requiring the module, launching a browser with chromium.launch(), firefox.launch(), or webkit.launch(), creating a new page, navigating to a URL, performing actions, and closing the browser.
Playwright.by property builds page-free locators
The Playwright.by property (available in JavaScript since v1.63) is of type By and builds page-free locators that are resolved with Page.get(), Frame.get(), or Locator.get(). Example: by.role('button', { name: 'Save' }).
Playwright.chromium property
Playwright.chromium is of type BrowserType and can be used to launch or connect to Chromium, returning instances of Browser.
Playwright.firefox property
Playwright.firefox is of type BrowserType and can be used to launch or connect to Firefox, returning instances of Browser.
Playwright.webkit property
Playwright.webkit is of type BrowserType and can be used to launch or connect to WebKit, returning instances of Browser.
Playwright.devices property returns device dictionary
Playwright.devices is of type Object (JavaScript/Python) or IReadOnlyDictionary<string, BrowserNewContextOptions> (C#). It returns a dictionary of devices to be used with Browser.newContext() or Browser.newPage(). Example devices include 'iPhone 6'.
Playwright.errors property contains TimeoutError
Playwright.errors (JavaScript) is an object that contains specific error classes thrown by Playwright methods. It includes TimeoutError, a class that can be caught when Playwright methods like Locator.waitFor() fail. Example: e instanceof playwright.errors.TimeoutError.
Playwright.selectors property manages selector engines
Playwright.selectors is of type Selectors and can be used to install custom selector engines for extensibility.
Playwright.create() method launches driver process in Java
Playwright.create() (Java, available since v1.10) launches a new Playwright driver process and connects to it. It returns a Playwright instance. Playwright.close() should be called when the instance is no longer needed.
Playwright.create() accepts env option in Java
Playwright.create() in Java accepts an optional 'env' parameter of type Object<string, string> containing additional environment variables that will be passed to the driver process. By default the driver process inherits environment variables of the Playwright process.
Playwright.close() method terminates Java instance
Playwright.close() (Java, available since v1.9) terminates the Playwright instance and closes all created browsers if they are still running.
Playwright.stop() async method terminates Python instance
Playwright.stop() is an async method (Python, available since v1.8) that terminates the Playwright instance in case it was created bypassing the Python context manager. This is useful in REPL applications. It should be called after using playwright.chromium.launch(), browser.new_page(), and related operations outside of the context manager.
JavaScript example of Playwright basic usage
Example showing Playwright basic usage in JavaScript: const { chromium, firefox, webkit } = require('playwright'); (async () => { const browser = await chromium.launch(); const page = await browser.newPage(); await page.goto('http://example.com'); await browser.close(); })();
Java example of Playwright basic usage
Example showing Playwright basic usage in Java: import com.microsoft.playwright.*; try (Playwright playwright = Playwright.create()) { BrowserType chromium = playwright.chromium(); Browser browser = chromium.launch(); Page page = browser.newPage(); page.navigate("http://example.com"); browser.close(); }
Python sync example of Playwright basic usage
Example showing Playwright basic usage in Python sync: from playwright.sync_api import sync_playwright, Playwright; def run(playwright: Playwright): chromium = playwright.chromium; browser = chromium.launch(); page = browser.new_page(); page.goto('http://example.com'); browser.close(); with sync_playwright() as playwright: run(playwright)
C# example of Playwright basic usage
Example showing Playwright basic usage in C#: 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"); } }
JavaScript example of using Playwright.devices
Example showing device usage with Playwright.devices in JavaScript: 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'); await browser.close(); })();
Python sync example of using Playwright.devices
Example showing device usage with Playwright.devices in Python sync: 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"); browser.close(); with sync_playwright() as playwright: run(playwright)
C# example of using Playwright.devices
Example showing device usage with Playwright.devices in C#: 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"); } }
JavaScript example of handling TimeoutError
Example showing error handling with Playwright.errors in JavaScript: try { await page.locator('.foo').waitFor(); } catch (e) { if (e instanceof playwright.errors.TimeoutError) { // Do something if this is a timeout. } }
Python sync example of handling TimeoutError
Example showing error handling with TimeoutError in Python sync: try: page.wait_for_selector(".foo") except TimeoutError as e: pass # do something if this is a timeout.
Java example of Playwright.create()
Example showing Playwright.create() usage in Java: Playwright playwright = Playwright.create(); Browser browser = playwright.webkit().launch(); Page page = browser.newPage(); page.navigate("https://www.w3.org/"); playwright.close();
Python sync example of Playwright.stop()
Example showing Playwright.stop() usage in Python: 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();
JavaScript example of using Playwright.by
Example showing Playwright.by usage in JavaScript: import { by, test } from '@playwright/test'; const saveButton = by.role('button', { name: 'Save' }); test('saves', async ({ page }) => { await page.get(saveButton).click(); });