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
运行 playwright-cli --help 查看内置列表,或运行 playwright-cli --help <command> 查看单个命令的参数和选项。
🌐 Run playwright-cli --help for the built-in listing, or playwright-cli --help <command> for a
single command's arguments and options.
核心
🌐 Core
open [url] attach [name] close
detach goto <url> type <text>
click <target> [button] dblclick <target> [button] fill <target> <text>
drag <start> <end> drop <target> hover <target>
select <target> <val> upload <files...> check <target>
uncheck <target> snapshot [target] find [text]
eval <func> [target] dialog-accept [prompt] dialog-dismiss
resize <w> <h> delete-data
导航
🌐 Navigation
go-back go-forward reload
Keyboard
press <key> keydown <key> keyup <key>
Mouse
mousemove <x> <y> mousedown [button] mouseup [button]
mousewheel <dx> <dy>
另存为
🌐 Save as
screenshot [target] pdf
标签
🌐 Tabs
tab-list tab-new [url] tab-close [index]
tab-select <index>
存储
🌐 Storage
state-load <file> state-save [file]
cookie-list 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
requests request <index> request-headers <index>
request-body <index> response-headers <index> response-body <index>
route <pattern> route-list unroute [pattern]
network-state-set <state>
DevTools
console [min-level] run-code [code] recording-start
recording-stop tracing-start tracing-stop
video-start [file] video-stop video-chapter <title>
video-show-actions video-hide-actions show
pause-at <location> resume step-over
generate-locator <target> highlight [target]
安装
🌐 Install
install install-browser [browser]
浏览器会话
🌐 Browser sessions
-s=<name> <cmd> list close-all
kill-all
全局选项
🌐 Global options
--help [command] --json --raw
--version