Playwright MCP Guide
Rules and usage for Microsoft Playwright MCP server.
Table of Contents
Core
Workflows
- Common Workflows
- Advanced Workflows - Cookie popups, scrolling, expanding items, full page screenshots
Reference
- Element Selection
- Authentication - Persistent profiles, storage state, extension mode
- Troubleshooting - Common issues, flaky tests
- Setup - Installation
Intent Lookup
User wants to...
- Research a topic / read articles → Navigate, dismiss cookie popup, scroll for lazy content, screenshot
- Find a product / compare prices → Navigate, search, extract data with
browser_evaluate
- Fill out a form / submit application → Use
browser_fill for fields, browser_click for submit
- Download file / attachment → First find links with Section 5, then click to download
- Log into a site → Fill credentials, submit; use PLAYWRIGHT_AUTHENTICATION.md to stay logged in
- Do a bank transfer / pay bills → Requires persistent profile for auth; use
browser_snapshot before each action
- Check email / download attachments → Navigate to webmail, expand messages, click attachment links
- Archive a webpage → See PLAYWRIGHT_ADVANCED_WORKFLOWS.md for full page screenshot workflow
- Interact with dynamic content → Scroll to load lazy content, expand collapsed sections, then proceed
UI testing...
- Verify page loads correctly → Navigate,
browser_snapshot, check expected elements present
- Test form validation → Submit empty/invalid data, check error messages appear
- Test navigation flow → Click through menus, verify correct pages load
- Test responsive layout → Resize browser, screenshot at different widths
- Test button states → Hover, click, verify visual/functional changes
- Test modal dialogs → Trigger modal, interact, close, verify dismissed
- Test error states → Force errors (bad URL, timeout), verify error handling
- Test accessibility → Use
browser_snapshot (accessibility tree), check refs have labels
- Compare before/after → Screenshot before change, screenshot after, compare
- Test login/logout → Full auth flow, verify session state
Technical tasks...
- Handle cookie popup → PLAYWRIGHT_ADVANCED_WORKFLOWS.md#1-close-cookie-popups
- Run custom JavaScript →
browser_evaluate(expression: "...")
- Debug failures → PLAYWRIGHT_TROUBLESHOOTING.md
MUST-NOT-FORGET
- Use accessibility tree (not screenshots) for element selection
- Reference elements via
ref=e5 format from browser_snapshot
- Always call
browser_snapshot before clicking to get current refs
- Use
browser_close when done to free resources
- For logged-in sessions: Use persistent user profile or storage state
Configuration
Repository: https://github.com/microsoft/playwright-mcp
Package: @playwright/mcp
Basic (isolated session):
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"]
}
}
}
Persistent profile (remembers logins):
{
"args": ["@playwright/mcp@latest", "--user-data-dir", "[USER_PROFILE_PATH]/.ms-playwright-mcp-profile"]
}
Headless: Add "--headless" to args.
Timeouts: Add "--timeout-action", "10000", "--timeout-navigation", "120000" for slow pages.
Available Tools
Navigation
- browser_navigate -
browser_navigate(url: "https://example.com")
Element Interaction
- browser_snapshot - Get accessibility tree with element refs
- browser_click -
browser_click(element: "Button", ref: "e12")
- browser_type -
browser_type(element: "Input", ref: "e8", text: "query")
- browser_fill -
browser_fill(element: "Email", ref: "e3", value: "user@example.com")
- browser_select -
browser_select(element: "Country", ref: "e15", values: ["USA"])
- browser_hover -
browser_hover(element: "Menu", ref: "e5")
- browser_drag - Drag and drop between elements
- browser_press_key -
browser_press_key(key: "Enter") or browser_press_key(key: "Control+A")
Inspection
- browser_screenshot -
browser_screenshot() or browser_screenshot(fullPage: true)
- browser_console_messages - Get console logs
- browser_evaluate -
browser_evaluate(expression: "document.title")
Timing
- browser_wait_for -
browser_wait_for(time: 2) wait seconds, or browser_wait_for(text: "Loading") wait for text
Session
- browser_close - Close browser and free resources
Common Workflows
Navigate and Click
1. browser_navigate(url: "https://example.com")
2. browser_snapshot()
3. browser_click(element: "Login button", ref: "e12")
Fill Form
1. browser_snapshot()
2. browser_fill(element: "Username", ref: "e3", value: "user@example.com")
3. browser_fill(element: "Password", ref: "e5", value: "password123")
4. browser_click(element: "Submit", ref: "e8")
Wait for Content
After navigation or click, call browser_snapshot() to verify page loaded and get updated refs.
Full Page Screenshot
browser_screenshot(fullPage: true)
For cookie popups, lazy-load scrolling, and expanding collapsed items, see PLAYWRIGHT_ADVANCED_WORKFLOWS.md.
Element Selection
Using Refs from Snapshot
- Call
browser_snapshot() to get current page structure
- Find element in returned accessibility tree
- Use the
ref value in subsequent commands
Example snapshot output:
- banner [ref=e3]:
- link "Home" [ref=e5] [cursor=pointer]
- navigation [ref=e12]:
- link "Docs" [ref=e13]
Selector Priority
When refs unavailable, use stable selectors:
[data-testid="submit"] - Best
getByRole('button', { name: 'Save' }) - Semantic
getByText('Sign in') - User-facing
input[name="email"] - HTML attributes
- Avoid:
.btn-primary, #submit - Classes/IDs change
Requirements
- Node.js 18+ with npx in PATH
- Chrome/Chromium for headed mode
See SETUP.md for installation details.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: ms-playwright-mcp3description: Apply when automating browser interactions, web scraping, or UI testing with AI agents Use when this capability is needed.4---56# Playwright MCP Guide78Rules and usage for Microsoft Playwright MCP server.910## Table of Contents1112**Core**13- [MUST-NOT-FORGET](#must-not-forget)14- [Configuration](#configuration)15- [Available Tools](#available-tools)1617**Workflows**18- [Common Workflows](#common-workflows)19- [Advanced Workflows](PLAYWRIGHT_ADVANCED_WORKFLOWS.md) - Cookie popups, scrolling, expanding items, full page screenshots2021**Reference**22- [Element Selection](#element-selection)23- [Authentication](PLAYWRIGHT_AUTHENTICATION.md) - Persistent profiles, storage state, extension mode24- [Troubleshooting](PLAYWRIGHT_TROUBLESHOOTING.md) - Common issues, flaky tests25- [Setup](SETUP.md) - Installation2627## Intent Lookup2829**User wants to...**30- **Research a topic / read articles** → Navigate, dismiss cookie popup, scroll for lazy content, screenshot31- **Find a product / compare prices** → Navigate, search, extract data with `browser_evaluate`32- **Fill out a form / submit application** → Use `browser_fill` for fields, `browser_click` for submit33- **Download file / attachment** → First find links with [Section 5](PLAYWRIGHT_ADVANCED_WORKFLOWS.md#5-find-and-extract-links), then click to download34- **Log into a site** → Fill credentials, submit; use [PLAYWRIGHT_AUTHENTICATION.md](PLAYWRIGHT_AUTHENTICATION.md) to stay logged in35- **Do a bank transfer / pay bills** → Requires persistent profile for auth; use `browser_snapshot` before each action36- **Check email / download attachments** → Navigate to webmail, expand messages, click attachment links37- **Archive a webpage** → See [PLAYWRIGHT_ADVANCED_WORKFLOWS.md](PLAYWRIGHT_ADVANCED_WORKFLOWS.md) for full page screenshot workflow38- **Interact with dynamic content** → Scroll to load lazy content, expand collapsed sections, then proceed3940**UI testing...**41- **Verify page loads correctly** → Navigate, `browser_snapshot`, check expected elements present42- **Test form validation** → Submit empty/invalid data, check error messages appear43- **Test navigation flow** → Click through menus, verify correct pages load44- **Test responsive layout** → Resize browser, screenshot at different widths45- **Test button states** → Hover, click, verify visual/functional changes46- **Test modal dialogs** → Trigger modal, interact, close, verify dismissed47- **Test error states** → Force errors (bad URL, timeout), verify error handling48- **Test accessibility** → Use `browser_snapshot` (accessibility tree), check refs have labels49- **Compare before/after** → Screenshot before change, screenshot after, compare50- **Test login/logout** → Full auth flow, verify session state5152**Technical tasks...**53- **Handle cookie popup** → [PLAYWRIGHT_ADVANCED_WORKFLOWS.md#1-close-cookie-popups](PLAYWRIGHT_ADVANCED_WORKFLOWS.md#1-close-cookie-popups)54- **Run custom JavaScript** → `browser_evaluate(expression: "...")`55- **Debug failures** → [PLAYWRIGHT_TROUBLESHOOTING.md](PLAYWRIGHT_TROUBLESHOOTING.md)5657## MUST-NOT-FORGET5859- Use accessibility tree (not screenshots) for element selection60- Reference elements via `ref=e5` format from `browser_snapshot`61- Always call `browser_snapshot` before clicking to get current refs62- Use `browser_close` when done to free resources63- For logged-in sessions: Use persistent user profile or storage state6465## Configuration6667**Repository**: https://github.com/microsoft/playwright-mcp68**Package**: `@playwright/mcp`6970**Basic (isolated session):**71```json72{73 "mcpServers": {74 "playwright": {75 "command": "npx",76 "args": ["@playwright/mcp@latest"]77 }78 }79}80```8182**Persistent profile (remembers logins):**83```json84{85 "args": ["@playwright/mcp@latest", "--user-data-dir", "[USER_PROFILE_PATH]/.ms-playwright-mcp-profile"]86}87```8889**Headless:** Add `"--headless"` to args.9091**Timeouts:** Add `"--timeout-action", "10000", "--timeout-navigation", "120000"` for slow pages.9293## Available Tools9495### Navigation96- **browser_navigate** - `browser_navigate(url: "https://example.com")`9798### Element Interaction99- **browser_snapshot** - Get accessibility tree with element refs100- **browser_click** - `browser_click(element: "Button", ref: "e12")`101- **browser_type** - `browser_type(element: "Input", ref: "e8", text: "query")`102- **browser_fill** - `browser_fill(element: "Email", ref: "e3", value: "user@example.com")`103- **browser_select** - `browser_select(element: "Country", ref: "e15", values: ["USA"])`104- **browser_hover** - `browser_hover(element: "Menu", ref: "e5")`105- **browser_drag** - Drag and drop between elements106- **browser_press_key** - `browser_press_key(key: "Enter")` or `browser_press_key(key: "Control+A")`107108### Inspection109- **browser_screenshot** - `browser_screenshot()` or `browser_screenshot(fullPage: true)`110- **browser_console_messages** - Get console logs111- **browser_evaluate** - `browser_evaluate(expression: "document.title")`112113### Timing114- **browser_wait_for** - `browser_wait_for(time: 2)` wait seconds, or `browser_wait_for(text: "Loading")` wait for text115116### Session117- **browser_close** - Close browser and free resources118119## Common Workflows120121### Navigate and Click122```1231. browser_navigate(url: "https://example.com")1242. browser_snapshot()1253. browser_click(element: "Login button", ref: "e12")126```127128### Fill Form129```1301. browser_snapshot()1312. browser_fill(element: "Username", ref: "e3", value: "user@example.com")1323. browser_fill(element: "Password", ref: "e5", value: "password123")1334. browser_click(element: "Submit", ref: "e8")134```135136### Wait for Content137After navigation or click, call `browser_snapshot()` to verify page loaded and get updated refs.138139### Full Page Screenshot140```141browser_screenshot(fullPage: true)142```143144For cookie popups, lazy-load scrolling, and expanding collapsed items, see [PLAYWRIGHT_ADVANCED_WORKFLOWS.md](PLAYWRIGHT_ADVANCED_WORKFLOWS.md).145146## Element Selection147148### Using Refs from Snapshot1491501. Call `browser_snapshot()` to get current page structure1512. Find element in returned accessibility tree1523. Use the `ref` value in subsequent commands153154**Example snapshot output:**155```156- banner [ref=e3]:157 - link "Home" [ref=e5] [cursor=pointer]158 - navigation [ref=e12]:159 - link "Docs" [ref=e13]160```161162### Selector Priority163164When refs unavailable, use stable selectors:1651. `[data-testid="submit"]` - Best1662. `getByRole('button', { name: 'Save' })` - Semantic1673. `getByText('Sign in')` - User-facing1684. `input[name="email"]` - HTML attributes1695. Avoid: `.btn-primary`, `#submit` - Classes/IDs change170171## Requirements172173- Node.js 18+ with npx in PATH174- Chrome/Chromium for headed mode175176See [SETUP.md](SETUP.md) for installation details.177178---179> Converted and distributed by [TomeVault](https://tomevault.io/claim/karstenheld3) — claim your Tome and manage your conversions.180<!-- tomevault:4.0:skill_md:2026-04-13 -->