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.
1---2name: ms-playwright-mcp3description: Apply when automating browser interactions, web scraping, or UI testing with AI agents4---5
6# Playwright MCP Guide
7
8Rules and usage for Microsoft Playwright MCP server.
9
10## Table of Contents
11
12**Core**
13- [MUST-NOT-FORGET](#must-not-forget)
14- [Configuration](#configuration)
15- [Available Tools](#available-tools)
16
17**Workflows**
18- [Common Workflows](#common-workflows)
19- [Advanced Workflows](PLAYWRIGHT_ADVANCED_WORKFLOWS.md) - Cookie popups, scrolling, expanding items, full page screenshots
20
21**Reference**
22- [Element Selection](#element-selection)
23- [Authentication](PLAYWRIGHT_AUTHENTICATION.md) - Persistent profiles, storage state, extension mode
24- [Troubleshooting](PLAYWRIGHT_TROUBLESHOOTING.md) - Common issues, flaky tests
25- [Setup](SETUP.md) - Installation
26
27## Intent Lookup
28
29**User wants to...**
30- **Research a topic / read articles** → Navigate, dismiss cookie popup, scroll for lazy content, screenshot
31- **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 submit
33- **Download file / attachment** → First find links with [Section 5](PLAYWRIGHT_ADVANCED_WORKFLOWS.md#5-find-and-extract-links), then click to download
34- **Log into a site** → Fill credentials, submit; use [PLAYWRIGHT_AUTHENTICATION.md](PLAYWRIGHT_AUTHENTICATION.md) to stay logged in
35- **Do a bank transfer / pay bills** → Requires persistent profile for auth; use `browser_snapshot` before each action
36- **Check email / download attachments** → Navigate to webmail, expand messages, click attachment links
37- **Archive a webpage** → See [PLAYWRIGHT_ADVANCED_WORKFLOWS.md](PLAYWRIGHT_ADVANCED_WORKFLOWS.md) for full page screenshot workflow
38- **Interact with dynamic content** → Scroll to load lazy content, expand collapsed sections, then proceed
39
40**UI testing...**
41- **Verify page loads correctly** → Navigate, `browser_snapshot`, check expected elements present
42- **Test form validation** → Submit empty/invalid data, check error messages appear
43- **Test navigation flow** → Click through menus, verify correct pages load
44- **Test responsive layout** → Resize browser, screenshot at different widths
45- **Test button states** → Hover, click, verify visual/functional changes
46- **Test modal dialogs** → Trigger modal, interact, close, verify dismissed
47- **Test error states** → Force errors (bad URL, timeout), verify error handling
48- **Test accessibility** → Use `browser_snapshot` (accessibility tree), check refs have labels
49- **Compare before/after** → Screenshot before change, screenshot after, compare
50- **Test login/logout** → Full auth flow, verify session state
51
52**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)
56
57## MUST-NOT-FORGET
58
59- Use accessibility tree (not screenshots) for element selection
60- Reference elements via `ref=e5` format from `browser_snapshot`
61- Always call `browser_snapshot` before clicking to get current refs
62- Use `browser_close` when done to free resources
63- For logged-in sessions: Use persistent user profile or storage state
64
65## Configuration
66
67**Repository**: https://github.com/microsoft/playwright-mcp
68**Package**: `@playwright/mcp`
69
70**Basic (isolated session):**
71```json
72{
73 "mcpServers": {
74 "playwright": {
75 "command": "npx",
76 "args": ["@playwright/mcp@latest"]
77 }
78 }
79}
80```
81
82**Persistent profile (remembers logins):**
83```json
84{
85 "args": ["@playwright/mcp@latest", "--user-data-dir", "[USER_PROFILE_PATH]/.ms-playwright-mcp-profile"]
86}
87```
88
89**Headless:** Add `"--headless"` to args.
90
91**Timeouts:** Add `"--timeout-action", "10000", "--timeout-navigation", "120000"` for slow pages.
92
93## Available Tools
94
95### Navigation
96- **browser_navigate** - `browser_navigate(url: "https://example.com")`
97
98### Element Interaction
99- **browser_snapshot** - Get accessibility tree with element refs
100- **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 elements
106- **browser_press_key** - `browser_press_key(key: "Enter")` or `browser_press_key(key: "Control+A")`
107
108### Inspection
109- **browser_screenshot** - `browser_screenshot()` or `browser_screenshot(fullPage: true)`
110- **browser_console_messages** - Get console logs
111- **browser_evaluate** - `browser_evaluate(expression: "document.title")`
112
113### Timing
114- **browser_wait_for** - `browser_wait_for(time: 2)` wait seconds, or `browser_wait_for(text: "Loading")` wait for text
115
116### Session
117- **browser_close** - Close browser and free resources
118
119## Common Workflows
120
121### Navigate and Click
122```
1231. browser_navigate(url: "https://example.com")
1242. browser_snapshot()
1253. browser_click(element: "Login button", ref: "e12")
126```
127
128### Fill Form
129```
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```
135
136### Wait for Content
137After navigation or click, call `browser_snapshot()` to verify page loaded and get updated refs.
138
139### Full Page Screenshot
140```
141browser_screenshot(fullPage: true)
142```
143
144For cookie popups, lazy-load scrolling, and expanding collapsed items, see [PLAYWRIGHT_ADVANCED_WORKFLOWS.md](PLAYWRIGHT_ADVANCED_WORKFLOWS.md).
145
146## Element Selection
147
148### Using Refs from Snapshot
149
1501. Call `browser_snapshot()` to get current page structure
1512. Find element in returned accessibility tree
1523. Use the `ref` value in subsequent commands
153
154**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```
161
162### Selector Priority
163
164When refs unavailable, use stable selectors:
1651. `[data-testid="submit"]` - Best
1662. `getByRole('button', { name: 'Save' })` - Semantic
1673. `getByText('Sign in')` - User-facing
1684. `input[name="email"]` - HTML attributes
1695. Avoid: `.btn-primary`, `#submit` - Classes/IDs change
170
171## Requirements
172
173- Node.js 18+ with npx in PATH
174- Chrome/Chromium for headed mode
175
176See [SETUP.md](SETUP.md) for installation details.