Skip to content

Lesson 5 of 9 · 27m

Browser Automation Without Theater

Use Playwright-style rendering when JavaScript is truly required, control cost with deterministic workflows, and avoid automating friction you should not bypass.

Browser automation is a power tool with a maintenance bill attached. It renders JavaScript, executes application logic, manages cookies, and gives you the final DOM. It also consumes memory, CPU, bandwidth, and time while inheriting every frontend experiment the site ships. Use it when rendering is necessary, not because it feels more realistic.

Prove rendering is required

Fetch the initial HTML and compare it to the rendered DOM. If the data is already present, plain HTTP wins:

python - <<'PY'
import httpx
html = httpx.get('https://books.toscrape.com/').text
print('price present in html:', '£' in html)
print('bytes:', len(html))
PY

If the page depends on client-side calls, inspect those calls first. Automating the network endpoint is often cheaper and clearer than automating pixels, provided the endpoint is one you may use.

Drive the browser like an application client

Playwright is the practical default for modern browser automation:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page(user_agent="SudoResearchBot/1.0 (+https://sudosecurity.org/contact)")
    page.goto("https://quotes.toscrape.com/js/", wait_until="domcontentloaded")
    page.wait_for_selector(".quote", timeout=10_000)
    print(page.locator(".quote").count())
    browser.close()

Use semantic selectors where possible: roles, labels, stable data attributes. Pixel coordinates and deeply nested generated classes break first.

Wait for application state, not wall-clock time

Bad browser scrapers are full of sleep(3). They are slow in the happy path and wrong in the bad path. Wait for a selector or response event that represents the data you need:

page.wait_for_selector("[data-testid='result-row']", timeout=10_000)

networkidle is useful but not magic; analytics and websockets can keep networks busy forever.

Control browser economics

A browser context can easily cost 100-300 MB of memory and multiple seconds per page. At scale, that is a budget:

HTTP fetch             milliseconds to low seconds, tiny memory
browser page           seconds, high memory, renderer risk
browser with media     more bandwidth, more CPU, more noise
browser per proxy      multiplied operational and ethical complexity

Disable images, fonts, and media only when doing so does not misrepresent your access or interfere with normal operation. Use one context per logical identity, not one browser per page.

There is friction you should not automate around

CAPTCHAs, login walls, MFA challenges, bot review queues, paywalls, and explicit anti-automation interstitials are access decisions. Do not build CAPTCHA-solving pipelines, account rotation, fingerprint spoofing, or proxy rotation to defeat them. The correct engineering response is stop, use an approved API, seek permission, or narrow the project.

Field notes

  • Headless browsers are still browsers; they do not make prohibited automation acceptable.
  • Use storage state only for accounts you own and are permitted to automate.
  • Browser crashes and zombie processes are normal operating conditions; supervise workers and enforce timeouts.
  • Capture screenshots or trace files for failed runs, but redact credentials and personal data.
  • If rendering is only needed for one component, isolate it instead of rendering every page.

Was this lesson helpful?

Sign in to rate