playwright-mcp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePlaywright MCP Browser Automation
Playwright MCP 浏览器自动化
You are an expert at using the Playwright MCP server for live browser interaction. This skill teaches you to navigate pages, inspect elements, fill forms, and debug web UIs through direct browser control.
你是使用Playwright MCP服务器进行实时浏览器交互的专家。本技能将教你通过直接控制浏览器来访问页面、检查元素、填写表单以及调试Web UI。
Critical: These Are Direct Tool Calls
重要提示:这些是直接工具调用
MCP tools are direct tool calls — exactly like , , or . They are NOT CLI commands.
ReadGrepBashCORRECT — call the tool directly:
Tool: mcp__playwright__browser_navigate
Parameters: { "url": "http://localhost:3010" }WRONG — do NOT shell out:
Bash: claude mcp call playwright browser_navigate ... # This does not workAll Playwright MCP tools use the prefix.
mcp__playwright__MCP工具是直接工具调用——和、或者完全一样,它们不是CLI命令。
ReadGrepBash正确用法——直接调用工具:
Tool: mcp__playwright__browser_navigate
Parameters: { "url": "http://localhost:3010" }错误用法——不要通过Shell调用:
Bash: claude mcp call playwright browser_navigate ... # 这种方式无效所有Playwright MCP工具都使用前缀。
mcp__playwright__Critical: Always Snapshot Before Interacting
重要提示:交互前务必生成快照
browser_snapshotrefrefbrowser_snapshotrefrefCritical: Output Size Awareness
重要提示:注意输出大小
| Tool | Output Size | Notes |
|---|---|---|
| Medium-Large | Full accessibility tree — scales with page complexity |
| Large | Base64 image — use sparingly |
| Variable | Can be very large on noisy apps |
| Variable | Can be very large on API-heavy pages |
| Small | Returns page title only |
| Small | Returns snapshot after click |
| Small | Returns snapshot after typing |
Prefer over for understanding page structure. Screenshots are for visual verification only.
browser_snapshotbrowser_take_screenshot| 工具 | 输出大小 | 说明 |
|---|---|---|
| 中到大型 | 完整无障碍树——大小随页面复杂度变化 |
| 大型 | Base64图片——谨慎使用 |
| 不固定 | 日志较多的应用中输出会非常大 |
| 不固定 | API密集的页面中输出会非常大 |
| 小型 | 仅返回页面标题 |
| 小型 | 返回点击后的快照 |
| 小型 | 返回输入后的快照 |
如需了解页面结构,优先使用而非,截图仅用于视觉验证。
browser_snapshotbrowser_take_screenshotWorkflow 1: Navigate & Inspect
工作流1:访问与检查
Trigger: User says "open this page", "what's on the page?", "inspect the UI", "check the layout"
触发场景:用户说「打开这个页面」、「页面上有什么?」、「检查UI」、「查看布局」
Steps
步骤
-
Navigate to the page:
browser_navigate({ url: "http://localhost:3010/home/team-slug/settings" }) -
Get page structure (always do this before interacting):
browser_snapshot → accessibility tree with ref values for all elements -
Analyze the snapshot: Identify interactive elements (buttons, links, inputs) by theirvalues. Report page structure to user.
ref
-
访问目标页面:
browser_navigate({ url: "http://localhost:3010/home/team-slug/settings" }) -
获取页面结构(交互前必须执行):
browser_snapshot → 包含所有元素ref值的无障碍树 -
分析快照: 通过值识别可交互元素(按钮、链接、输入框),向用户反馈页面结构。
ref
Decision: Snapshot vs Screenshot
决策:快照 vs 截图
| Need | Use |
|---|---|
| Understand page structure, find elements | |
| Visual appearance, layout bugs, CSS issues | |
| Both structure and appearance | Snapshot first, then screenshot if visual check needed |
| 需求 | 使用工具 |
|---|---|
| 了解页面结构、查找元素 | |
| 查看视觉效果、布局bug、CSS问题 | |
| 同时需要结构和外观 | 先拍快照,如需视觉检查再截图 |
Workflow 2: Form Interaction
工作流2:表单交互
Trigger: User says "fill out the form", "submit the login", "type in the field", "select an option"
触发场景: 用户说「填写表单」、「提交登录」、「在输入框输入内容」、「选择选项」
Steps
步骤
-
Snapshot to find form fields:
browser_snapshot → identify input refs and their labels -
Fill fields (choose based on complexity):
- Single field:
browser_type({ ref: "input-ref", text: "value" }) - Multiple fields:
browser_fill_form({ fields: [{ ref: "ref1", value: "val1" }, { ref: "ref2", value: "val2" }] }) - Dropdown:
browser_select_option({ ref: "select-ref", values: ["option-value"] })
- Single field:
-
Submit the form:
browser_click({ ref: "submit-button-ref" }) -
Verify result:
browser_snapshot → check for success message, error states, or navigation
-
快照查找表单字段:
browser_snapshot → 识别输入框ref及其标签 -
填写字段(根据复杂度选择):
- 单字段:
browser_type({ ref: "input-ref", text: "value" }) - 多字段:
browser_fill_form({ fields: [{ ref: "ref1", value: "val1" }, { ref: "ref2", value: "val2" }] }) - 下拉框:
browser_select_option({ ref: "select-ref", values: ["option-value"] })
- 单字段:
-
提交表单:
browser_click({ ref: "submit-button-ref" }) -
验证结果:
browser_snapshot → 检查成功提示、错误状态或页面跳转
Key Patterns
核心模式
- is faster than multiple
browser_fill_formcalls for multi-field formsbrowser_type - Use as alternative to clicking submit
browser_press_key({ key: "Enter" }) - After submission, wait if needed: then snapshot
browser_wait_for({ text: "Success" })
- 多字段表单使用比多次调用
browser_fill_form更快browser_type - 可以用替代点击提交按钮
browser_press_key({ key: "Enter" }) - 提交后如需等待:执行后再生成快照
browser_wait_for({ text: "Success" })
Workflow 3: Debug Investigation
工作流3:调试排查
Trigger: User says "debug the page", "check for errors", "what API calls are happening?", "why isn't it working?"
触发场景: 用户说「调试页面」、「检查错误」、「正在调用什么API?」、「为什么它不工作?」
Steps
步骤
-
Navigate to the problematic page:
browser_navigate({ url: "..." }) -
Run these in parallel (they are independent):
browser_console_messages → JavaScript errors, warnings, logs browser_network_requests → API calls, failed requests, status codes -
Snapshot the page:
browser_snapshot → current UI state, error messages, loading states -
Investigate further:
browser_evaluate({ expression: "document.querySelectorAll('.error').length" }) → run custom JS
-
访问有问题的页面:
browser_navigate({ url: "..." }) -
并行运行以下工具(相互独立):
browser_console_messages → JavaScript错误、警告、日志 browser_network_requests → API调用、失败请求、状态码 -
生成页面快照:
browser_snapshot → 当前UI状态、错误信息、加载状态 -
进一步排查:
browser_evaluate({ expression: "document.querySelectorAll('.error').length" }) → 运行自定义JS
Workflow 4: Multi-Step Navigation
工作流4:多步骤导航
Trigger: User says "go through the flow", "test the signup process", "walk through the wizard"
触发场景: 用户说「走完流程」、「测试注册流程」、「遍历引导流程」
Steps
步骤
-
Navigate to starting page:
browser_navigate({ url: "..." }) -
For each step:
browser_snapshot → find the next action browser_click({ ref: "..." }) or browser_type({ ref: "...", text: "..." }) browser_wait_for({ text: "expected content" }) → if page loads async -
Handle dialogs if they appear:
browser_handle_dialog({ accept: true }) → confirm/alert/prompt -
Go back if needed:
browser_navigate_back
-
访问起始页面:
browser_navigate({ url: "..." }) -
逐个完成步骤:
browser_snapshot → 找到下一步操作 browser_click({ ref: "..." }) 或 browser_type({ ref: "...", text: "..." }) browser_wait_for({ text: "expected content" }) → 页面异步加载时使用 -
处理弹出的对话框:
browser_handle_dialog({ accept: true }) → 确认/告警/提示框 -
如需返回上一页:
browser_navigate_back
Tool Reference
工具参考
Navigation
导航类
| Tool | Purpose | Parameters |
|---|---|---|
| Go to URL | |
| Go back | None |
| Wait for text or time | |
| List/switch tabs | None or |
| 工具 | 用途 | 参数 |
|---|---|---|
| 访问指定URL | |
| 返回上一页 | 无 |
| 等待指定文本出现或等待指定时长 | |
| 列出/切换标签页 | 无或指定切换的 |
Inspection
检查类
| Tool | Purpose | Parameters |
|---|---|---|
| Accessibility tree with refs | None |
| Visual screenshot | None |
| JS console output | None |
| Network activity | None |
| 工具 | 用途 | 参数 |
|---|---|---|
| 获取含ref值的无障碍树 | 无 |
| 获取可视化截图 | 无 |
| 获取JS控制台输出 | 无 |
| 获取网络活动记录 | 无 |
Interaction
交互类
| Tool | Purpose | Parameters |
|---|---|---|
| Click element | |
| Type text | |
| Fill multiple fields | |
| Select dropdown | |
| Hover element | |
| Drag and drop | |
| Keyboard input | |
| Upload file | |
| Confirm/dismiss dialog | |
| 工具 | 用途 | 参数 |
|---|---|---|
| 点击元素 | |
| 输入文本 | |
| 填写多个字段 | |
| 选择下拉框选项 | |
| 悬停在元素上 | |
| 拖拽元素 | |
| 模拟键盘输入 | |
| 上传文件 | |
| 确认/关闭对话框 | |
Advanced
高级功能
| Tool | Purpose | Parameters |
|---|---|---|
| Run JavaScript | |
| Resize viewport | |
| Close page | None |
| Install browser | None |
| Run Playwright code | |
| 工具 | 用途 | 参数 |
|---|---|---|
| 运行JavaScript代码 | |
| 调整视口大小 | |
| 关闭页面 | 无 |
| 安装浏览器 | 无 |
| 运行Playwright代码 | |
Troubleshooting
故障排查
"No browser running" or Connection Errors
「无浏览器运行」或连接错误
The Playwright MCP server manages its own browser instance. If tools fail:
- Try to ensure the browser binary is available
browser_install - Try to a simple URL — this may initialize the browser
browser_navigate
Playwright MCP服务器管理自己的浏览器实例。如果工具调用失败:
- 尝试运行确保浏览器二进制文件可用
browser_install - 尝试用访问一个简单URL——这可能会初始化浏览器
browser_navigate
Elements Not Found After Navigation
导航后找不到元素
Pages with async content may not have rendered yet:
- Use before snapshot
browser_wait_for({ text: "expected content" }) - Use for time-based waiting
browser_wait_for({ timeout: 2000 })
含异步内容的页面可能还未渲染完成:
- 快照前使用
browser_wait_for({ text: "expected content" }) - 使用进行基于时间的等待
browser_wait_for({ timeout: 2000 })
Proxied or Internal Domains
代理或内部域名访问失败
The MCP browser cannot reach proxied or internal domains that require network tunnels. Use directly for local Supabase.
http://127.0.0.1:54321MCP浏览器无法访问需要网络隧道的代理或内部域名。本地Supabase请直接使用。
http://127.0.0.1:54321Snapshot Returns Very Large Output
快照返回输出过大
Complex pages produce large accessibility trees. If output is too large:
- Navigate to a specific sub-page rather than the full app
- Use to query specific elements instead
browser_evaluate
复杂页面会生成庞大的无障碍树。如果输出过大:
- 访问特定子页面而非整个应用
- 使用查询特定元素即可
browser_evaluate