browsing-with-playwright
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBrowser Automation
浏览器自动化
Automate browser interactions via Playwright MCP server.
通过Playwright MCP服务器实现浏览器交互自动化。
Server Lifecycle
服务器生命周期
Start Server
启动服务器
bash
undefinedbash
undefinedUsing helper script (recommended)
使用辅助脚本(推荐)
bash scripts/start-server.sh
bash scripts/start-server.sh
Or manually
或手动启动
npx @playwright/mcp@latest --port 8808 --shared-browser-context &
undefinednpx @playwright/mcp@latest --port 8808 --shared-browser-context &
undefinedStop Server
停止服务器
bash
undefinedbash
undefinedUsing helper script (closes browser first)
使用辅助脚本(先关闭浏览器)
bash scripts/stop-server.sh
bash scripts/stop-server.sh
Or manually
或手动停止
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_close -p '{}'
pkill -f "@playwright/mcp"
undefinedpython3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_close -p '{}'
pkill -f "@playwright/mcp"
undefinedWhen to Stop
何时停止服务器
- End of task: Stop when browser work is complete
- Long sessions: Keep running if doing multiple browser tasks
- Errors: Stop and restart if browser becomes unresponsive
Important: The flag is required to maintain browser state across multiple mcp-client.py calls. Without it, each call gets a fresh browser context.
--shared-browser-context- 任务结束时:浏览器操作完成后停止
- 长会话场景:若需执行多个浏览器任务,保持服务器运行
- 出现错误时:若浏览器无响应,停止并重启服务器
重要提示:需要参数来在多次mcp-client.py调用之间保持浏览器状态。如果不使用该参数,每次调用都会创建一个全新的浏览器上下文。
--shared-browser-contextQuick Reference
快速参考
Navigation
导航操作
bash
undefinedbash
undefinedGo to URL
访问指定URL
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_navigate
-p '{"url": "https://example.com"}'
-p '{"url": "https://example.com"}'
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_navigate
-p '{"url": "https://example.com"}'
-p '{"url": "https://example.com"}'
Go back
返回上一页
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_navigate_back -p '{}'
undefinedpython3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_navigate_back -p '{}'
undefinedGet Page State
获取页面状态
bash
undefinedbash
undefinedAccessibility snapshot (returns element refs for clicking/typing)
可访问性快照(返回用于点击/输入的元素引用)
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_snapshot -p '{}'
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_snapshot -p '{}'
Screenshot
截图
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_take_screenshot
-p '{"type": "png", "fullPage": true}'
-p '{"type": "png", "fullPage": true}'
undefinedpython3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_take_screenshot
-p '{"type": "png", "fullPage": true}'
-p '{"type": "png", "fullPage": true}'
undefinedInteract with Elements
元素交互
Use from snapshot output to target elements:
refbash
undefined使用快照输出中的来定位元素:
refbash
undefinedClick element
点击元素
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_click
-p '{"element": "Submit button", "ref": "e42"}'
-p '{"element": "Submit button", "ref": "e42"}'
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_click
-p '{"element": "Submit button", "ref": "e42"}'
-p '{"element": "Submit button", "ref": "e42"}'
Type text
输入文本
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_type
-p '{"element": "Search input", "ref": "e15", "text": "hello world", "submit": true}'
-p '{"element": "Search input", "ref": "e15", "text": "hello world", "submit": true}'
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_type
-p '{"element": "Search input", "ref": "e15", "text": "hello world", "submit": true}'
-p '{"element": "Search input", "ref": "e15", "text": "hello world", "submit": true}'
Fill form (multiple fields)
填写表单(多字段)
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_fill_form
-p '{"fields": [{"ref": "e10", "value": "john@example.com"}, {"ref": "e12", "value": "password123"}]}'
-p '{"fields": [{"ref": "e10", "value": "john@example.com"}, {"ref": "e12", "value": "password123"}]}'
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_fill_form
-p '{"fields": [{"ref": "e10", "value": "john@example.com"}, {"ref": "e12", "value": "password123"}]}'
-p '{"fields": [{"ref": "e10", "value": "john@example.com"}, {"ref": "e12", "value": "password123"}]}'
Select dropdown
选择下拉选项
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_select_option
-p '{"element": "Country dropdown", "ref": "e20", "values": ["US"]}'
-p '{"element": "Country dropdown", "ref": "e20", "values": ["US"]}'
undefinedpython3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_select_option
-p '{"element": "Country dropdown", "ref": "e20", "values": ["US"]}'
-p '{"element": "Country dropdown", "ref": "e20", "values": ["US"]}'
undefinedWait for Conditions
等待条件触发
bash
undefinedbash
undefinedWait for text to appear
等待文本出现
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_wait_for
-p '{"text": "Success"}'
-p '{"text": "Success"}'
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_wait_for
-p '{"text": "Success"}'
-p '{"text": "Success"}'
Wait for time (ms)
等待指定时长(毫秒)
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_wait_for
-p '{"time": 2000}'
-p '{"time": 2000}'
undefinedpython3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_wait_for
-p '{"time": 2000}'
-p '{"time": 2000}'
undefinedExecute JavaScript
执行JavaScript
bash
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_evaluate \
-p '{"function": "return document.title"}'bash
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_evaluate \
-p '{"function": "return document.title"}'Multi-Step Playwright Code
多步骤Playwright代码
For complex workflows, use to run multiple actions in one call:
browser_run_codebash
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_run_code \
-p '{"code": "async (page) => { await page.goto(\"https://example.com\"); await page.click(\"text=Learn more\"); return await page.title(); }"}'Tip: Use for complex multi-step operations that should be atomic (all-or-nothing).
browser_run_code对于复杂工作流,使用在单次调用中执行多个操作:
browser_run_codebash
python3 scripts/mcp-client.py call -u http://localhost:8808 -t browser_run_code \
-p '{"code": "async (page) => { await page.goto(\"https://example.com\"); await page.click(\"text=Learn more\"); return await page.title(); }"}'提示:对于需要原子性(要么全部完成要么全部失败)的复杂多步骤操作,请使用。
browser_run_codeWorkflow: Form Submission
工作流:表单提交
- Navigate to page
- Get snapshot to find element refs
- Fill form fields using refs
- Click submit
- Wait for confirmation
- Screenshot result
- 导航至目标页面
- 获取快照以找到元素引用
- 使用元素引用填写表单字段
- 点击提交按钮
- 等待确认信息出现
- 对结果进行截图
Workflow: Data Extraction
工作流:数据提取
- Navigate to page
- Get snapshot (contains text content)
- Use browser_evaluate for complex extraction
- Process results
- 导航至目标页面
- 获取快照(包含文本内容)
- 使用browser_evaluate进行复杂数据提取
- 处理提取结果
Verification
验证步骤
Run:
python3 scripts/verify.pyExpected:
✓ Playwright MCP server running运行:
python3 scripts/verify.py预期结果:
✓ Playwright MCP server runningIf Verification Fails
如果验证失败
- Run diagnostic:
pgrep -f "@playwright/mcp" - Check: Server process running on port 8808
- Try:
bash scripts/start-server.sh - Stop and report if still failing - do not proceed with downstream steps
- 运行诊断命令:
pgrep -f "@playwright/mcp" - 检查:服务器进程是否在8808端口运行
- 尝试重新启动:
bash scripts/start-server.sh - 如果仍失败,请停止操作并上报 - 不要继续后续步骤
Tool Reference
工具参考
See references/playwright-tools.md for complete tool documentation.
完整工具文档请查看references/playwright-tools.md。
Troubleshooting
故障排除
| Issue | Solution |
|---|---|
| Element not found | Run browser_snapshot first to get current refs |
| Click fails | Try browser_hover first, then click |
| Form not submitting | Use |
| Page not loading | Increase wait time or use browser_wait_for |
| Server not responding | Stop and restart: |
| 问题 | 解决方案 |
|---|---|
| 未找到元素 | 先运行browser_snapshot获取当前元素引用 |
| 点击操作失败 | 先尝试browser_hover,再执行点击 |
| 表单未提交 | 在browser_type中使用 |
| 页面加载失败 | 增加等待时长或使用browser_wait_for |
| 服务器无响应 | 停止并重启: |