Glossary
Playwright: modern browser automation
Playwright is an open source library from Microsoft for automating Chromium, Firefox and WebKit, with official packages for JavaScript and TypeScript, Python, Java and .NET.
What sets it apart
Playwright arrived after Selenium and Puppeteer and folded in many lessons from both. Three features shape how you write scripts with it.
- Browser contexts. One browser process can host many contexts, each with its own cookies, storage and cache, like separate incognito profiles. Creating a context takes milliseconds, so you get isolation per job without launching a new browser.
- Auto waiting. Actions such as click and fill wait until the element is attached, visible, stable and enabled. Most explicit waits disappear from your code.
- Network control. You can intercept, block, modify or mock any request, and read every response the page receives.
Locators
A locator describes how to find an element and is re-evaluated at each action, so it survives re-renders. Prefer user facing locators (role, label, text) for tests, and CSS for scraping when you need precision.
import { chromium } from "playwright";
const browser = await chromium.launch();
const ctx = await browser.newContext({ locale: "en-US" });
const tab = await ctx.newPage();
await tab.goto("https://example.com/register");
const keyAttr = await tab.locator("[data-sitekey]").first().getAttribute("data-sitekey");
console.log("site key:", keyAttr);
await browser.close();
Reading data from the network
Many pages load their data as JSON. Instead of parsing the DOM, wait for the response you want and read its body directly. This combines the convenience of a browser (it handles cookies and scripts) with the clean data of a hidden API. Blocking images and fonts through routing also speeds up scraping runs considerably.
Detection and challenges
Automated Playwright sessions are recognizable through navigator.webdriver, protocol side effects and environment details, the same family of signals covered under headless browser detection. On sites with bot management, expect a Turnstile widget, a reCAPTCHA or a full challenge page at some point.
The pattern for widgets: read the key with a locator, create a token task, wait, then write the token into the page and submit. For a full Cloudflare challenge, create an Cloudflare Challenge task with your proxy, then start a context that uses the same proxy and user agent and add the returned cf_clearance cookie with context.addCookies(). The cookie only works from the IP that earned it. Turnstile tokens typically take under 3 seconds; the clearance cookie takes under 10 seconds.
Proxies per context
Each context can carry its own proxy, so one browser process can browse from several exits at once. That matters for challenge cookies, which are often bound to the IP that earned them: keep a context and its proxy together for the life of a session, and do not move a cookie from one context into another that leaves through a different address.
Testing your own site
For your own forms, use the test site keys that CAPTCHA vendors publish for staging, so end to end tests stay deterministic and free. Keep real solving for monitoring third party pages you are allowed to automate.
Tooling around it
Playwright ships a test runner with parallel workers, retries and HTML reports, a trace viewer that records every action with screenshots and network logs, and a code generator that writes a script while you click. The trace viewer alone saves hours when a run fails only in CI. See the Playwright integration guide for full solving examples and Playwright scraping for data collection patterns.
Common questions
Which browsers does Playwright control?
Chromium, Firefox and WebKit, the engine behind Safari, using builds that Playwright downloads for you.
What is a browser context in Playwright?
An isolated session inside one browser with its own cookies and storage, cheap to create and close.
Does Playwright wait for elements automatically?
Yes. Actions wait until the element is visible, stable and enabled, which removes most manual waits.
How do I use a cf_clearance cookie in Playwright?
Create the context with the same proxy and user agent the task used, then add the cookie with addCookies.
Is Playwright better than Puppeteer?
They are close relatives. Playwright adds more browsers, more languages and a test runner; Puppeteer stays lean and Chrome focused.
Keep reading
- PlaywrightPlaywright in Node.js and Python: read widget keys from the DOM or network, inject tokens, and add answers through route interception.
- Web scraping with PlaywrightUse Playwright contexts, readable locators and captured JSON responses to scrape script-heavy pages from Python without guesswork.
- Why your Playwright bot is detectedThe automation tells a stock Playwright launch exposes, how sites read them, and practical changes for calmer test runs.
- PuppeteerThe Node.js library that drives Chrome and Chromium over the DevTools Protocol, popular for scraping, screenshots and PDFs.