Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

You can use Playwright with an AI agent to collect specific content from a public web page, but neither “zero-cost” nor a 45-second run is guaranteed. This reproducible example uses Playwright’s library with a fixed script: the script opens a page, finds a uniquely identified heading, and saves it with the page URL as JSON. It assumes Node.js and the required browser are already installed; the 45-second figure is not a verified benchmark.

Choose the Playwright interface that fits the task

Playwright is a browser automation framework for Chromium, Firefox, and WebKit. Its official project offers agent-oriented interfaces, including Playwright CLI and Playwright MCP, as well as a library that can be used in scraping scripts. These are different ways to control a browser, not interchangeable steps in one setup. This example uses the library because it makes the extraction logic explicit and repeatable. Playwright project

  • Library: Best suited to a defined extraction task where you want the script to decide what fields to collect and how to save them.
  • CLI or MCP: Agent-facing options for workflows where a coding agent or AI-driven automation interacts with the browser. The CLI setup documented by Playwright requires Node.js 20 or newer and a coding agent. Playwright CLI documentation

A model is not necessary for this fixed example: ordinary script logic selects the target. If you add an AI model to interpret pages or choose actions, that changes the architecture and may add API or hosted-service costs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What “zero-cost” and “45 seconds” can—and cannot—mean

The Playwright software can be used without a license fee, but that does not make every end-to-end run free. Browser binaries must be downloaded, and system dependencies may need installation. The machine, network, storage, and any model or hosted browser service also have costs. Playwright ties browser builds to its releases, so an update may require reinstalling the browser. Playwright browser installation documentation

This walkthrough’s timing boundary starts after Node.js, the package, and browser are installed. It does not establish that the task finishes in 45 seconds: that depends on the site, computer, browser startup, page behavior, and any AI configuration. Call a run “45 seconds” only if you time that particular setup and report what the timer includes.

Check the target page before scraping it

No target site is specified here, so there is no basis for claiming that a particular collection is permitted. Before running the example, check the site’s terms and access requirements for the page and data you intend to collect. Browser automation is not a way to evade access controls.

Choose a public page whose heading you are allowed to collect, then replace the example URL and heading text below. The script records the URL and heading only; it does not crawl links or attempt to bypass restrictions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Install Playwright and its browser

With Node.js installed, create a project and install Playwright. Browser installation may download files and is separate from the timed extraction itself. Follow the current setup instructions if your operating system needs additional dependencies.

  1. In a new project directory, run npm init -y.

  2. Install the library with npm install playwright.

  3. Install Chromium with npx playwright install chromium.

  4. Create a file named scrape.mjs and add the script in the next section.

  5. Run it with node scrape.mjs.

Check Playwright’s current browser documentation for supported versions and any operating-system dependencies before setup. Browser downloads and dependencies

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run a small, auditable extraction

The example uses a fresh browser context so the run has separate cookies and storage from other contexts. That isolation helps make runs independent; it does not provide anonymity or permission to access a site. The locator uses the page’s user-facing heading role and visible text rather than a long CSS or XPath chain.

import { chromium } from 'playwright';
import { writeFile } from 'node:fs/promises';

const url = 'https://example.com/';
const expectedHeading = 'Example Domain';

const browser = await chromium.launch();
const context = await browser.newContext();

try {
  const page = await context.newPage();
  await page.goto(url);

  const heading = page.getByRole('heading', {
    name: expectedHeading,
    exact: true
  });

  await heading.waitFor({ state: 'visible' });
  const result = {
    source: page.url(),
    heading: await heading.innerText()
  };

  await writeFile('result.json', JSON.stringify(result, null, 2));
  console.log(result);
} finally {
  await context.close();
  await browser.close();
}

Replace https://example.com/ and Example Domain with the permitted target page and the exact heading you expect there. If the page has multiple matching headings, make the locator more specific only after verifying which one is intended. Playwright locators are strict for single-target operations and are reevaluated against the current page; its documentation calls locators “the central piece of Playwright’s auto-waiting and retry-ability.” Playwright locator documentation

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Wait for the data, not just a browser action

Playwright automatically checks actionability before actions such as clicking. For a click, those checks include whether the target is unique, visible, stable, enabled, and able to receive events. They do not prove that the content your scraper needs has appeared. The script therefore waits for the target heading to become visible before reading it. Playwright actionability documentation

For dynamic pages, wait for the specific locator or content you intend to extract. Do not treat network-idle as a universal signal that a page is ready; Playwright’s Frame API guidance discourages it in testing contexts. Playwright Frame API documentation

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inspect the saved result and handle common failures

On success, the terminal prints an object and the script writes the same data to result.json, for example:

{
  "source": "https://example.com/",
  "heading": "Example Domain"
}
  • Browser executable missing: Run npx playwright install chromium for the installed Playwright version.
  • Heading timeout or no match: Confirm the target URL and expected text, then check whether the page renders that heading only after another user-visible step. Add a content-specific wait or interaction where appropriate.
  • More than one match: Refine the locator using verified visible text or another user-facing attribute; do not select an arbitrary match.
  • Page access denied: Stop and review the site’s access rules rather than trying to circumvent its controls.

Close the context and browser even when extraction fails; the finally block does this. For a containerized scraping or crawling deployment, Playwright’s Docker guidance recommends using a separate user and a seccomp profile. That is deployment guidance, not a prerequisite for this local demonstration. Playwright Docker documentation

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.