#Why AI agents use Playwright
AddedAn AI agent that "uses a browser" is a language model in a loop: it looks at the page, decides on one action, the action happens, it looks again. The model never touches the browser itself. Something has to turn the page into something a model can read, and turn the model's decision into a reliable action. Playwright does both, which is why most browser agents (and the tools in this chapter) are built on it.
There are two ways to show a model a page. Pixels: send a screenshot and let a vision model guess where to click, by coordinates. Structure: send the page's accessibility tree, the same tree a screen reader uses, as text: roles, names, states, each element tagged with a short id. Playwright's agent tools use structure by default. A text snapshot is far smaller than an image, the model reads exact names instead of guessing from pixels, and "click e14" can't miss by ten pixels or land on a banner that slid in.
The mental model: giving directions over the phone. A screenshot is like describing what you see out of the window ("the green button near the top"); an accessibility snapshot is like reading out the street signs ("the button called Sign in, number 14"). The second is shorter and the listener can't misunderstand it. And because each action is a normal Playwright action, it auto-waits for the element to be visible, stable and enabled (Chapter 1), so the agent gets the same protection against timing problems that your tests get.
Here's exactly what an agent sees of the shop's sign-in page, and how a ref becomes an action:
import { test, expect } from "@playwright/test";
test("what an AI agent sees, and how it acts on it", async ({ page }) => {
await page.goto("/practice/");
// The aria snapshot a test asserts on (Chapter 10)...
console.log(await page.ariaSnapshot());
console.log("---- the same page in 'ai' mode ----");
// ...and the version made for models (1.59+): every element gets a ref it can point at.
console.log(await page.ariaSnapshot({ mode: "ai" }));
// The model answers "fill e10 with rohit", "fill e13 …", "click e14". getByRef (1.64+) turns a ref into a locator.
await page.getByRef("e10").fill("rohit");
await page.getByRef("e13").fill("playwright");
await page.getByRef("e14").click();
await expect(page.getByRole("heading", { name: "Products" })).toBeVisible();
});Running 1 test using 1 worker
- banner:
- link "Practice Shop":
- /url: /practice/
- main:
- heading "Sign in" [level=1]
- text: Username
- textbox "Username"
- text: Password
- textbox "Password"
- button "Sign in"
- paragraph:
- text: A practice app for the Playwright study site. Sign in as
- code: rohit
- text: or
- code: ana
- text: with the password
- code: playwright
- text: .
- code: locked
- text: is an account that can't sign in. Everything is saved only in this browser.
---- the same page in 'ai' mode ----
- generic [active] [ref=e1]:
- banner [ref=e2]:
- link "Practice Shop" [ref=e3] [cursor=pointer]:
- /url: /practice/
- main [ref=e4]:
- generic [ref=e5]:
- heading "Sign in" [level=1] [ref=e6]
- generic [ref=e7]:
- generic [ref=e8]:
- generic [ref=e9]: Username
- textbox "Username" [ref=e10]
- generic [ref=e11]:
- generic [ref=e12]: Password
- textbox "Password" [ref=e13]
- button "Sign in" [ref=e14] [cursor=pointer]
- paragraph [ref=e15]:
- text: A practice app for the Playwright study site. Sign in as
- code [ref=e16]: rohit
- text: or
- code [ref=e17]: ana
- text: with the password
- code [ref=e18]: playwright
- text: .
- code [ref=e19]: locked
- text: is an account that can't sign in. Everything is saved only in this browser.
✓ 1 [chromium] › tests/what-agents-see.spec.ts:3:1 › what an AI agent sees, and how it acts on it (999ms)
1 passed (1.5s)page.ariaSnapshot()(1.59+) walks the page's accessibility tree and prints it as YAML: each line is a role (textbox,button,heading), its accessible name in quotes, and states like[level=1]. Plain<div>wrappers and styling are gone; what's left is what a person using a screen reader would hear.mode: "ai"prints the same tree with two differences that matter to an agent: every node gets a ref ([ref=e10]), and the plain wrapper elements appear too, asgenericnodes with refs of their own. It also adds hints like[cursor=pointer](something you can click) and[active](where the focus is).- The model reads that text and answers with an action on a ref. In an agent that's a tool call (the next topic); in a test,
page.getByRef("e10")(1.64+) makes a locator from it. The refs resolve against the latest snapshot taken in that frame, so an agent must snapshot again after the page changes. - The three actions are ordinary Playwright actions:
fillandclickwait for the element to be ready, and the click submits the form. The shop takes half a second to sign in, and the final assertion waits for the Products heading, so nothing here needs a sleep. - The whole sign-in page is 25 short lines of text. A screenshot of it would be an image the model has to interpret, and still wouldn't tell it that the second box is the password field: the snapshot says so in words.
The options on page.ariaSnapshot() and locator.ariaSnapshot() were added for agents, release by release (all from the release notes):
mode: "ai"(1.59+): refs and iframe contents, as above.locator.ariaSnapshot()also takesmodeanddepthsince 1.59, so an agent can look at one part of the page instead of all of it.depthlimits how deep the tree goes, which keeps big pages small.boxes: true(1.60+) appends[box=x,y,width,height]to each element, for agents that combine the tree with a screenshot.page.ariaSnapshotJSON()andlocator.ariaSnapshotJSON()(1.63+) return the same tree as JSON instead of YAML.- Traces can record aria snapshots too (1.63+:
snapshots: { aria: true }/ the trace viewer's "Display Aria" mode), and since 1.60 a failedexpectcan include the receiver's aria snapshot in its error context. That's theerror-context.mdnamed at the bottom of every failure on this site, and opening one shows who it's for: it starts with "# Instructions" ("Following Playwright test failed. Explain why, be concise…"), then the error, the aria snapshot of the element (for a failedtoHaveTexton the Sign in button,- button "Sign in") and the test source. It's a ready-made prompt: paste it into an assistant, or let an agent read it. - 1.65 changed what counts: text the browser doesn't render no longer contributes to accessible names or snapshots. If an aria snapshot test broke on upgrading to 1.65, that's the likely reason.
This page has a "Place order" <div> with a click handler and cursor: pointer, a real "Place order" <button>, an icon button with aria-label="Close", and an <img> with no alt. What does the agent see, and how many buttons named "Place order" does getByRole find?
import { test, expect } from "@playwright/test";
test("what an agent sees of two 'buttons'", async ({ page }) => {
await page.setContent(`
<style>.fake { cursor: pointer; padding: 8px; background: #2a6; }</style>
<div class="fake" onclick="this.textContent = 'Done'">Place order</div>
<button onclick="this.textContent = 'Done'">Place order</button>
<button aria-label="Close"><svg width="16" height="16"><path d="M0 0L16 16"/></svg></button>
<img src="photo.png">
`);
console.log(await page.ariaSnapshot({ mode: "ai" }));
await expect(page.getByRole("button", { name: "Place order" })).toHaveCount(1);
});Show answer
Running 1 test using 1 worker
- generic [active] [ref=e1]:
- generic [ref=e2] [cursor=pointer]: Place order
- button "Place order" [ref=e3]
- button "Close" [ref=e4]
✓ 1 [chromium] › tests/fake-button.spec.ts:3:1 › what an agent sees of two 'buttons' (84ms)
1 passed (529ms)- The
<div>is justgeneric … [cursor=pointer]: Place order. Nothing says it's a button; the only clue is the pointer cursor, which the AI mode adds as a hint. A model may or may not guess it's clickable, and a screen reader user gets no hint at all. - The real
<button>isbutton "Place order": role and name, unmistakable.getByRole("button", { name: "Place order" })finds exactly 1, the real one. - The icon button has no text, but
aria-label="Close"gives it a name, so it appears asbutton "Close". Without the label it would be a nameless button that no agent (or test, or person) could pick out. - The
<img>withoutaltdoesn't appear at all. - The lesson for a team: an app that's accessible is an app agents (and
getByRoletests) can use. Fixing roles and labels helps users, tests and agents at once.