Playwright 命令行工具
🌐 Playwright CLI
一个为编码代理设计的浏览器自动化命令行接口。高效的命令和可安装的技能让代理在有限的上下文窗口中平衡浏览器自动化、大型代码库和推断能力。
🌐 A command-line interface for browser automation designed for coding agents. Token-efficient commands and installable skills let agents balance browser automation with large codebases and reasoning within limited context windows.
示例
🌐 Example
$ playwright-cli open https://demo.playwright.dev/todomvc --headed
$ playwright-cli type "Buy groceries"
$ playwright-cli press Enter
$ playwright-cli type "Water flowers"
$ playwright-cli press Enter
$ playwright-cli check e21
$ playwright-cli screenshot
每个命令之后,CLI 会输出当前页面状态:
🌐 After each command, the CLI outputs the current page state:
### Page
- Page URL: https://demo.playwright.dev/todomvc/#/
- Page Title: React - TodoMVC
### Snapshot
[Snapshot](.playwright-cli/page-2026-02-14T19-22-42-679Z.yml)
快照文件包含带有元素引用的可访问性树,用于下一条命令。
🌐 The snapshot file contains the accessibility tree with element refs for the next command.
主要特点
🌐 Key Features
- 令牌高效 — 简明的命令行输出避免将大型工具模式加载到模型上下文中
- 基于技能 — 代理通过可安装的技能而不是冗长的帮助文本来发现功能
- 守护进程架构 — 持久化浏览器进程意味着每条命令无启动成本
- 基于引用 — 使用元素引用的可访问性快照以实现确定性交互
- 跨浏览器 — 支持 Chrome、Firefox、WebKit 和 Edge
- 会话 — 多个隔离的浏览器实例,各自有独立状态
Playwright 命令行接口 vs MCP
🌐 Playwright CLI vs MCP
| Playwright CLI | MCP | |
|---|---|---|
| 最适合 | 使用大型代码库的编码代理(Claude Code,Copilot) | 专用代理循环,探索性自动化 |
| 工作原理 | 代理运行 shell 命令 | LLM 使用结构化参数调用 MCP 工具 |
| Token 成本 | 较低 — 简明的 CLI 输出,按需加载技能 | 较高 — 工具模式 + 上下文快照 |
| 默认模式 | 无头模式 | 有头模式 |
| 设置 | npm install -g @playwright/cli | MCP 客户端中的 JSON 配置 |
所有命令
🌐 All Commands
核心
🌐 Core
open [url] goto <url> close
click <ref> dblclick <ref> fill <ref> <text>
type <text> select <ref> <val> check <ref>
uncheck <ref> hover <ref> drag <start> <end>
upload <file> snapshot screenshot [ref]
pdf eval <func> [ref] resize <w> <h>
dialog-accept [prompt] dialog-dismiss
导航
🌐 Navigation
go-back go-forward reload
键盘与鼠标
🌐 Keyboard & Mouse
press <key> keydown <key> keyup <key>
mousemove <x> <y> mousedown [btn] mouseup [btn]
mousewheel <dx> <dy>
标签
🌐 Tabs
tab-list tab-new [url] tab-select <idx>
tab-close [idx]
存储
🌐 Storage
state-save [file] state-load <file>
cookie-list [--domain] cookie-get <name> cookie-set <name> <val>
cookie-delete <name> cookie-clear
localstorage-list localstorage-get <key> localstorage-set <k> <v>
localstorage-delete <key> localstorage-clear
sessionstorage-list sessionstorage-get <key> sessionstorage-set <k> <v>
sessionstorage-delete <k> sessionstorage-clear
网络
🌐 Network
network route <pattern> [opts] route-list
unroute [pattern]
DevTools
console [min-level] run-code <code> tracing-start
tracing-stop video-start [file] video-chapter <title>
video-stop show
会话
🌐 Sessions
-s=<name> <cmd> list close-all
kill-all delete-data
配置
🌐 Config
open --headed open --browser=firefox open --persistent
open --profile=<path> open --config=file.json attach --extension
install --skills config-print