Skip to main content
Glama
SJMakin

even-better-playwright-mcp

by SJMakin

browser_execute

Execute Playwright code to automate browser interactions, inspect page accessibility, and manage web automation tasks within a controlled environment.

Instructions

Execute Playwright code with these in scope:

  • page - Current Playwright page

  • context - Browser context, access all pages via context.pages()

  • state - Persistent object across calls (e.g., state.myPage = await context.newPage())

  • $('e5') - Shorthand for page.locator('aria-ref=e5')

  • accessibilitySnapshot() - Get current page snapshot

  • require - Load Node.js modules (path, url, crypto, buffer, util, assert, os, fs)

  • Node.js globals: setTimeout, setInterval, fetch, URL, Buffer, crypto, etc.

Rules

  • Multiple calls: Use multiple execute calls for complex logic - helps understand intermediate state and isolate failures

  • Never close: Never call browser.close() or context.close(). Only close pages you created or if user asks

  • No bringToFront: Never call unless user asks - it's disruptive and unnecessary

  • Check state after actions: Always verify page state after clicking/submitting (see next section)

  • Clean up listeners: Call page.removeAllListeners() at end to prevent leaks

  • Wait for load: Use page.waitForLoadState('domcontentloaded') not page.waitForEvent('load') - waitForEvent times out if already loaded

  • Avoid timeouts: Prefer proper waits over page.waitForTimeout() - there are better ways

Checking Page State

After any action (click, submit, navigate), verify what happened:

console.log('url:', page.url()); console.log(await accessibilitySnapshot().then(x => x.split('\n').slice(0, 30).join('\n')));

For visually complex pages (grids, galleries, dashboards), use screenshotWithAccessibilityLabels({ page }) instead.

Accessibility Snapshots

await accessibilitySnapshot()  // Full snapshot
await accessibilitySnapshot({ search: /button|submit/i })  // Filter results
await accessibilitySnapshot({ showDiffSinceLastCall: true })  // Show changes

Example output:

- banner [ref=e3]:
    - link "Home" [ref=e5] [cursor=pointer]:
        - /url: /
    - navigation [ref=e12]:
        - link "Docs" [ref=e13] [cursor=pointer]

Use aria-ref to interact - NO quotes around the ref value:

await page.locator('aria-ref=e13').click()  // or: await $('e13').click()

For pagination: (await accessibilitySnapshot()).split('\n').slice(0, 50).join('\n')

Choosing snapshot method:

  • Use accessibilitySnapshot for simple pages, text search, token efficiency

  • Use screenshotWithAccessibilityLabels for complex visual layouts, spatial position matters

Selector Best Practices

For unknown sites: use accessibilitySnapshot() with aria-ref For development (with source access), prefer:

  1. [data-testid="submit"] - explicit test attributes

  2. getByRole('button', { name: 'Save' }) - semantic

  3. getByText('Sign in'), getByLabel('Email') - user-facing

  4. input[name="email"] - semantic HTML

  5. Avoid: classes/IDs that change frequently

If locator matches multiple elements (strict mode violation), use .first(), .last(), or .nth(n):

await page.locator('button').first().click()
await page.locator('li').nth(3).click()  // 4th item (0-indexed)

Working with Pages

const pages = context.pages().filter(x => x.url().includes('localhost'));
state.newPage = await context.newPage(); await state.newPage.goto('https://example.com');

Navigation

await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });
await waitForPageLoad({ page, timeout: 5000 });

Common Patterns

Popups: const [popup] = await Promise.all([page.waitForEvent('popup'), page.click('a[target=_blank]')]); await popup.waitForLoadState(); Downloads: const [download] = await Promise.all([page.waitForEvent('download'), page.click('button.download')]); await download.saveAs('/tmp/' + download.suggestedFilename()); iFrames: const frame = page.frameLocator('#my-iframe'); await frame.locator('button').click(); Dialogs: page.on('dialog', async d => { await d.accept(); }); await page.click('button'); Load files: const fs = require('fs'); const content = fs.readFileSync('./data.txt', 'utf-8'); await page.locator('textarea').fill(content);

page.evaluate

Code inside page.evaluate() runs in the browser - use plain JavaScript only. console.log inside evaluate runs in browser, not visible here:

const title = await page.evaluate(() => document.title);
console.log('Title:', title);  // Log outside evaluate

Utility Functions

  • getLatestLogs({ page?, count?, search? }) - Get browser console logs

  • getCleanHTML({ locator, search?, showDiffSinceLastCall?, includeStyles? }) - Get cleaned HTML

  • waitForPageLoad({ page, timeout? }) - Smart load detection (ignores analytics/ads)

  • getCDPSession() - Get CDP session for raw Chrome DevTools Protocol commands

  • getLocatorStringForElement(locator) - Get stable selector from ephemeral aria-ref

  • getReactSource({ locator }) - Get React component source location (dev mode only)

  • getStylesForLocator({ locator, cdp }) - Inspect CSS styles (read styles-api resource first)

  • createDebugger({ cdp }) - Set breakpoints, step through code (read debugger-api resource first)

  • createEditor({ cdp }) - View/edit page scripts and CSS (read editor-api resource first)

  • screenshotWithAccessibilityLabels({ page }) - Screenshot with Vimium-style visual labels (yellow=links, orange=buttons, coral=inputs)

Network Interception

For scraping/reverse-engineering APIs, intercept network instead of scrolling DOM:

state.requests = []; state.responses = [];
page.on('request', req => { if (req.url().includes('/api/')) state.requests.push({ url: req.url(), method: req.method(), headers: req.headers() }); });
page.on('response', async res => { if (res.url().includes('/api/')) { try { state.responses.push({ url: res.url(), status: res.status(), body: await res.json() }); } catch {} } });

Then trigger actions and analyze: console.log('Captured', state.responses.length, 'API calls'); Clean up when done: page.removeAllListeners('request'); page.removeAllListeners('response');

IMPORTANT: After navigation, refs are stale - call snapshot tool again.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesPlaywright code with {page, context, state, $} in scope. Should be concise - use ; for multiple statements.
timeoutNoTimeout in milliseconds (default: 30000)
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It thoroughly describes behavioral traits, including rules for multiple calls, cleanup (e.g., removeAllListeners), load waiting strategies, timeout avoidance, and state verification after actions. It also covers navigation handling, common patterns, and utility functions, providing comprehensive operational context.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness2/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is excessively long and poorly structured, with extensive code examples, utility function listings, and detailed best practices that could be better organized or referenced elsewhere. It lacks front-loading of critical information, burying key usage guidelines in sections like 'Rules' and 'Checking Page State', making it less efficient for quick understanding.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of the tool (executing Playwright code with multiple behavioral nuances) and the absence of annotations and output schema, the description is highly complete. It covers purpose, usage guidelines, behavioral transparency, parameter semantics through examples, and extensive operational details, ensuring the agent has all necessary context to use the tool effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, so the baseline is 3. The description adds significant value by explaining the semantics of the 'code' parameter in detail, such as the available in-scope objects (page, context, state, $) and examples of usage patterns. However, it does not explicitly mention the 'timeout' parameter, which is documented in the schema, leaving a minor gap.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description starts with a clear verb ('Execute') and resource ('Playwright code'), specifying exactly what the tool does. It distinguishes itself from siblings like browser_network_requests, browser_search_snapshot, screenshot, and snapshot by focusing on code execution rather than network monitoring, searching, or visual capture.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool versus alternatives, such as recommending multiple execute calls for complex logic, avoiding browser.close() unless specified, and using accessibilitySnapshot for simple pages versus screenshotWithAccessibilityLabels for complex layouts. It also contrasts with siblings by emphasizing code execution over other browser interactions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/SJMakin/even-better-playwright-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server