GuidesGuide

AI browser automation with Puppeteer

Pair a headless browser with an LLM and automation stops breaking when pages change. The pattern, the hardening and the costs.

TL;DR

Selector-based automation breaks when a page changes its markup. Pair Puppeteer with an LLM: send the page content, get structured output back. The automation understands the page. At about $0.02 per page it beats selector maintenance.

  • Core loop: extract the page, send it with a strict JSON schema, parse, act.
  • Semantic extraction survives redesigns: "Sold Out" and "Currently Unavailable" mean the same.
  • The same pattern powers form filling and natural-language navigation.
  • Production needs retries, JSON validation, ~50 model calls a minute and 3-5 pooled browsers.
  • About $0.02 per page at Claude Sonnet rates. Send simple pages to cheaper models.

Why AI changes browser automation

Selenium, Playwright and Puppeteer scripts click buttons, fill forms and extract data. They work until the page structure changes, a captcha appears or the workflow needs judgment. Then someone updates selectors, again.

An LLM changes the failure mode. Automation that understands content adapts and decides from context:

  • A scraper that reads the page instead of hunting CSS classes.
  • A form filler that finds the right field even with obfuscated IDs.
  • Navigation driven by a described goal instead of CSS paths.
  • Teams already run this for lead generation and competitive intelligence.

The core pattern: page in, structured JSON out

Every technique in this guide is one pattern. Get this loop solid once and scraping, form filling and navigation are just different prompts.

  • Load the page with Puppeteer.
  • Extract visible text without scripts and styles, capped in length. Add structure if needed.
  • Prompt with exact JSON fields and ask for valid JSON only.
  • Parse, act and handle the parse failure path, because it will happen.
  • Pre-filter hard so you pay for signal instead of boilerplate.
  • Validate every response before anything downstream trusts it.

Scraping that survives redesigns

Traditional scraping binds to markup. When the price class changes, the script silently breaks. Semantic extraction binds to meaning: ask for name, price, currency, availability and rating as JSON, whatever the markup.

The model knows "$1,299.00", "1299 USD" and "From $1,299" are the same price. That replaces a wall of conditionals.

It extracts articles too: title, author, date, full text and topics from any layout. That is the input an agent pipeline or lead-scoring model wants.

Forms and natural-language navigation

Forms hurt selector automation most, because every site names fields differently. Extract each field's type, name, label and options. Give the model that structure plus your data and ask for fill actions. It maps "First Name", "Given Name" and "Prenom" on its own.

Navigation generalizes the idea. Give the model the clickable elements and a goal like "open the inquiry form". It returns one action at a time (click, type, navigate or done) in a capped loop.

One loop works across thousands of sites with zero site-specific code.

Hardening it for production

The demo takes an afternoon. Production is harder, because you now compose two unreliable systems: the network and the model.

  • Retry model calls with exponential backoff on rate limits, with a hard cap.
  • Validate JSON on every response. Treat a parse failure as retryable.
  • Put explicit timeouts on every navigation, click and wait.
  • Separate recoverable from fatal errors. Reload the page instead of restarting everything.
  • Keep screenshots at key steps. They show what really happened.

Scale: rate limits, browser pools and memory

At volume, the limits are model rate limits and browser RAM. Both yield to standard engineering.

  • Rate-limit model calls with a sliding window, around 50 requests per minute.
  • Pool 3-5 browser instances and reuse them. One browser per page leaks memory.
  • Block images, fonts and media when you only need text.
  • Run headless with heap limits, close pages fast, wait for DOM content over network idle.

The cost math and when selectors still win

At Claude Sonnet API rates ($3 per million input tokens, $15 per million output), a typical extraction costs about $0.02 per page. That assumes ~5,000 input and 500 output tokens. Cheap against selector maintenance, a real line item at scraping scale.

Selectors still win on a stable page you scrape daily. AI pays off with variety and churn. If redesigns cost you hours of selector fixes, the $0.02 pays for itself.

  • Cache responses for identical inputs.
  • Route simple extractions to smaller, cheaper models.
  • Batch similar requests.
  • Pre-filter content before it reaches the model.

Browser automation inside an agent fleet

Everything above is a script you own, host and debug. Long-running agents like OpenClaw ship browser control built in. "Check this supplier's portal every morning" becomes an instruction to an agent.

At fleet scale the browser becomes infrastructure: logins that last for days, captchas, geo-gated sites, dozens of agents browsing at once. Molted manages that layer with logged-in browser profiles, captcha solving and geo-aware proxies, plus self-healing.

Build the Puppeteer pipeline when scraping is your product. Hand browsing to agents when it is one job among many.

FAQ

The short version.

01

Does this work with Playwright instead of Puppeteer?

Yes, without meaningful changes. The pattern (extract context, ask for JSON, execute) does not care which library drives the browser. The prompts, validation and retries transfer as is.

02

How much does AI-powered browser automation cost?

About $0.02 per page for a typical extraction at Claude Sonnet API rates, roughly 5,000 input and 500 output tokens. Caching, smaller models and pre-filtering push it down. Compare it to the hours you spend fixing selectors.

03

How do I stop the model returning invalid JSON?

Reduce it, then handle the rest. Use an explicit schema, ask for JSON only with no markdown and keep temperature low. Then validate every response and retry parse failures a bounded number of times.

04

Is AI browser automation reliable enough for production?

Yes, with the hardening: backoff and retries, JSON validation, timeouts on every action, rate limiting and pooled browsers. Teams run it for lead generation today. It still needs monitoring and an error budget.

05

Do I need to build this myself if I run OpenClaw?

Not for most jobs. OpenClaw ships browser control built in, so browsing becomes an instruction to the agent. A Puppeteer pipeline still wins for high-volume, single-purpose scraping where you control every request.

Deploy agents that browse the web in production, with managed browsers and self-healing.