Playwright Core
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
-
ERR_REQUIRE_ESM
cause Attempting to import `playwright-core` using CommonJS `require()` syntax in an ES Module context, or generally in newer Node.js versions where Playwright is published as an ESM module.fixChange your import statements from `const { chromium } = require('playwright-core');` to `import { chromium } from 'playwright-core';`. Ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions. -
Error: Browser type 'chromium' is not found. Close all running browsers and try again.
cause The required browser binaries (e.g., Chromium) have not been installed or are corrupted for `playwright-core`.fixRun `npx playwright install chromium` to download and install the Chromium browser binaries. For all browsers, use `npx playwright install`. -
Error: Node.js v16.x is not supported. Please upgrade to Node.js v18 or higher.
cause The installed Node.js version is below the minimum requirement for the Playwright Core package.fixUpgrade your Node.js environment to version 18 or newer. Use NVM (`nvm install 18 && nvm use 18`) or update through your system's package manager. -
TimeoutError: locator.click: Timeout 30000ms exceeded.
cause The specified element could not be found, was not visible, or was not actionable within the default timeout period.fixEnsure the selector is correct and the element is present and visible on the page. Use `page.waitForSelector()` with appropriate state options (`'visible'`, `'attached'`, `'loaded'`) before interacting, or increase the locator's timeout: `page.locator('selector').click({ timeout: 60000 })`.
Warnings
- breaking Playwright Core dropped support for Chromium extension manifest v2. Projects relying on these older manifest versions for browser extensions will need to update their extensions or browser versions.
- gotcha The `playwright-core` package provides only the low-level browser automation API. It does not include the Playwright Test runner or the pre-built browser binaries (Chromium, Firefox, WebKit). Attempting to use `playwright-core` without explicitly installing or managing browser binaries will lead to errors.
- breaking Playwright Core requires Node.js version 18 or higher. Running with older Node.js versions will result in execution errors.
- gotcha The console window when spawning browser processes on Windows was reverted from being hidden, which previously caused regressions in features like `codegen`, `--ui`, and `show` commands.
Install
-
npm install playwright-core -
yarn add playwright-core -
pnpm add playwright-core
Imports
- chromium
const { chromium } = require('playwright-core');import { chromium } from 'playwright-core'; - Browser, Page
import { Browser, Page } from 'playwright-core';import type { Browser, Page } from 'playwright-core'; - firefox, webkit
import { firefox, webkit } from 'playwright-core';
Quickstart
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();