Skip to content
AgenticAIWiki
  1. Learn
  2. Part 9
  3. 53. Specialised agents: coding, research and browser
  4. 53.3

Lesson 53.3 · 3 of 5 · 7 min read · Reviewed 24 Nov 2026

Browser agents with Playwright

By the end, you can

  • Explain how a browser agent drives a real browser through Playwright.
  • Prefer role and label locators over pixel clicks, and verify every step.

Chapter 39 covered agents that work from screenshots. Many browser agentsBrowser agent: An agent that navigates websites by reading pages and clicking, typing and scrolling. take a more structured route: they drive a real browser through an automation library such as Playwright (Selenium and Puppeteer are other examples). The model chooses actions; Playwright performs them.

The pieces

model  --(tool call: click "Add to basket")-->  your code  --(Playwright)-->  browser
model  <--(page snapshot: roles, labels, text)--  your code  <--------------  browser

Your code exposes a few tools, each a thin wrapper around Playwright:

Tool Playwright underneath
goto(url) page.goto(url)
click(role, name) page.get_by_role(role, name=name).click()
fill(label, text) page.get_by_label(label).fill(text)
read() Visible text, or an accessibility snapshot
screenshot() page.screenshot() for layout questions

Open-source wrappers exist, such as the Playwright MCPModel Context Protocol (MCP): An open protocol that standardises how AI applications connect to tools, data and prompts through servers. server, which offers these actions to any MCP client.

A worked example

Task: “Find the opening hours of the Leeds branch on the company site.”

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://shop.example/branches")
    page.get_by_role("link", name="Leeds").click()
    hours = page.get_by_role("region", name="Opening hours").inner_text()
    print(hours)
    browser.close()

In an agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done., the model would pick each of those lines one at a time, after reading the page snapshot: first the list of branch links, then the branch page.

Why locators beat coordinates

A locator describes what to click (“the link named Leeds”), not where. Playwright then waits until that element is attached, visible, stable and enabled before acting. That removes most timing bugs, and it keeps working when the layout shifts.

Snapshots built from the accessibility tree (39.2) give the model the same names your locators use, so the model can say click("button", "Submit") and your code can do exactly that.

Verify every step

After each action, read the page again and check the expected change happened: a new heading, a confirmation message, the field now holding the value. If not, report it to the model as a tool error rather than carrying on blind.

Common mistakes

  • Fixed sleeps instead of locator waits: slow and still flaky.
  • Feeding the full DOM to the model. Trim to visible, interactive elements, or the context fills fast.
  • Logged-in personal browsers. Use a fresh browser profile in a sandboxSandbox: An isolated environment that limits what code or an agent can reach, such as files, network or credentials. (53.4), with only the accounts the task needs.
  • No confirmation for risky clicks. Buying, sending and deleting still need a person (39.4).

Key points

  • Playwright is a browser automation library: code opens pages, finds elements and clicks, types and reads.
  • A browser agent wraps Playwright actions as tools and lets the model choose the next action from a snapshot of the page.
  • Locators by role, label or text are far more reliable than coordinates, and Playwright waits for elements to be ready.
  • Check the page after every action, and keep risky actions behind a confirmation step.
Hands-on labOn your own machine

Drive a real page with Playwright locators

Install Playwright on your own machine and script a short browser task using role locators and checks after each step.

You will have: A Python script that opens a page, clicks by role, verifies the result and saves a screenshot, plus a note on one flaky step you removed.

Steps

  1. Create a virtual environment, then run: pip install playwright, then: playwright install chromium.
  2. Write a script that opens https://playwright.dev, clicks the link named 'Get started' with page.get_by_role, and reads the main heading.
  3. After the click, check that the heading text contains 'Installation' and print PASS or FAIL.
  4. Replace any time.sleep calls with locator waits or expect(...).to_be_visible(), and run the script five times.
  5. Wrap goto, click and read as three small functions: these are the tools an agent would call.

Done when

  • The script prints PASS on five runs in a row.
  • No fixed sleeps remain in the script.
  • A screenshot file is saved after the final step.

Docs: Playwright for Python: installation · Locators (Playwright docs) · Auto-waiting (Playwright docs)

Quick checkA browser agent's 'click Submit' works locally but fails in CI with element not found. The CI page loads more slowly. What is the best fix?
How sure are you?
Pick an answer and how sure you are