Playwright Core

1.59.1 · active · verified Tue Apr 21

Playwright Core is the foundational JavaScript library for automating web browsers, providing a high-level API to control Chromium, Firefox, and WebKit through a single interface. It is designed for robust end-to-end testing, web scraping, and general browser automation tasks. As of version 1.59.1, the project maintains a rapid release cadence, typically monthly, delivering new features and browser updates. Recent additions include the `page.screencast` API for comprehensive recording capabilities and the introduction of a new CLI mode (`playwright-cli`) for AI agent-friendly, token-efficient operations. Key differentiators of Playwright Core include its auto-waiting functionality, resilient element interaction APIs, and native mobile emulation. While this package provides the raw browser automation API, it commonly serves as the underlying engine for the broader `playwright` package, which integrates a full test runner and pre-built browser binaries. It requires Node.js version 18 or higher to operate.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates launching a headless Chromium browser, navigating to multiple pages, taking a screenshot, interacting with an element, and verifying the URL after navigation using Playwright Core.

import { chromium, type Browser, type Page } from 'playwright-core';
import * as fs from 'fs';

async function runBrowserAutomation() {
  let browser: Browser | undefined;
  try {
    // Launch a headless Chromium browser
    browser = await chromium.launch({ headless: true });
    const page: Page = await browser.newPage();

    // Navigate to a website
    await page.goto('https://www.example.com');
    console.log('Navigated to example.com');

    // Take a screenshot and save it
    const screenshotPath = 'example_screenshot.png';
    await page.screenshot({ path: screenshotPath });
    console.log(`Screenshot saved to ${screenshotPath}`);

    // Get and print the page title
    const title = await page.title();
    console.log(`Page title: "${title}"`);

    // Navigate to another page and interact with an element
    await page.goto('https://playwright.dev');
    const getStartedButton = page.locator('a', { hasText: 'Get started' }).first();
    await getStartedButton.click();
    console.log('Clicked "Get started" on Playwright.dev');

    // Wait for navigation to complete and verify the URL
    await page.waitForURL('**/docs/intro');
    console.log(`Current URL after click: ${page.url()}`);

  } catch (error) {
    console.error('An error occurred during automation:', error);
  } finally {
    // Ensure the browser is closed even if an error occurs
    if (browser) {
      await browser.close();
      console.log('Browser closed.');
    }
  }
}

runBrowserAutomation();

view raw JSON →