playwright-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Playwright CLI

Playwright CLI

Browser automation via Playwright. Token-efficient CLI for coding agents.
通过Playwright实现浏览器自动化。为编码Agent打造的高效Token CLI工具。

Installation

安装

bash
npm install -g @playwright/mcp@latest
playwright-cli --help
bash
npm install -g @playwright/mcp@latest
playwright-cli --help

Core Commands

核心命令

CommandDescription
playwright-cli open <url>
Open URL in browser
playwright-cli close
Close the page
playwright-cli type <text>
Type text into editable element
playwright-cli click <ref> [button]
Click on element
playwright-cli dblclick <ref> [button]
Double click
playwright-cli fill <ref> <text>
Fill text into field
playwright-cli drag <startRef> <endRef>
Drag and drop
playwright-cli hover <ref>
Hover over element
playwright-cli check <ref>
Check checkbox/radio
playwright-cli uncheck <ref>
Uncheck checkbox
playwright-cli select <ref> <val>
Select dropdown option
playwright-cli snapshot
Capture page snapshot for refs
命令描述
playwright-cli open <url>
在浏览器中打开URL
playwright-cli close
关闭页面
playwright-cli type <text>
在可编辑元素中输入文本
playwright-cli click <ref> [button]
点击元素
playwright-cli dblclick <ref> [button]
双击元素
playwright-cli fill <ref> <text>
为输入框填充文本
playwright-cli drag <startRef> <endRef>
拖拽操作
playwright-cli hover <ref>
悬停在元素上
playwright-cli check <ref>
勾选复选框/单选框
playwright-cli uncheck <ref>
取消勾选复选框
playwright-cli select <ref> <val>
选择下拉选项
playwright-cli snapshot
捕获页面快照以获取元素引用

Navigation

页面导航

bash
playwright-cli go-back           # Go back
playwright-cli go-forward        # Go forward
playwright-cli reload            # Reload page
bash
playwright-cli go-back           # 返回上一页
playwright-cli go-forward        # 前进到下一页
playwright-cli reload            # 重新加载页面

Keyboard & Mouse

键盘与鼠标操作

bash
playwright-cli press <key>       # Press key (a, arrowleft, enter...)
playwright-cli keydown <key>     # Key down
playwright-cli keyup <key>       # Key up
playwright-cli mousemove <x> <y> # Move mouse
playwright-cli mousedown [button] # Mouse down
playwright-cli mouseup [button]   # Mouse up
playwright-cli mousewheel <dx> <dy> # Scroll
bash
playwright-cli press <key>       # 按下按键(如a、arrowleft、enter等)
playwright-cli keydown <key>     # 按住按键
playwright-cli keyup <key>       # 松开按键
playwright-cli mousemove <x> <y> # 移动鼠标
playwright-cli mousedown [button] # 按住鼠标按键
playwright-cli mouseup [button]   # 松开鼠标按键
playwright-cli mousewheel <dx> <dy> # 滚动鼠标滚轮

Save & Export

保存与导出

bash
playwright-cli screenshot [ref]  # Screenshot page or element
playwright-cli pdf               # Save as PDF
bash
playwright-cli screenshot [ref]  # 截取页面或元素的屏幕截图
playwright-cli pdf               # 保存为PDF文件

Tabs

标签页管理

bash
playwright-cli tab-list          # List all tabs
playwright-cli tab-new [url]     # Open new tab
playwright-cli tab-close [index] # Close tab
playwright-cli tab-select <index> # Switch tab
bash
playwright-cli tab-list          # 列出所有标签页
playwright-cli tab-new [url]     # 打开新标签页
playwright-cli tab-close [index] # 关闭标签页
playwright-cli tab-select <index> # 切换标签页

DevTools

开发者工具

bash
playwright-cli console [min-level]  # View console messages
playwright-cli network              # View network requests
playwright-cli run-code <code>      # Run JS snippet
playwright-cli tracing-start        # Start trace
playwright-cli tracing-stop         # Stop trace
bash
playwright-cli console [min-level]  # 查看控制台消息
playwright-cli network              # 查看网络请求
playwright-cli run-code <code>      # 运行JS代码片段
playwright-cli tracing-start        # 开始追踪
playwright-cli tracing-stop         # 停止追踪

Sessions

会话管理

bash
playwright-cli session-list         # List sessions
playwright-cli session-stop [name]  # Stop session
playwright-cli session-stop-all     # Stop all
playwright-cli session-delete [name] # Delete session data
bash
playwright-cli session-list         # 列出所有会话
playwright-cli session-stop [name]  # 停止指定会话
playwright-cli session-stop-all     # 停止所有会话
playwright-cli session-delete [name] # 删除会话数据

Headed Mode

有头模式

bash
playwright-cli open https://example.com --headed
bash
playwright-cli open https://example.com --headed

Examples

使用示例

bash
undefined
bash
undefined

Open and interact

打开页面并进行交互

playwright-cli open https://example.com playwright-cli type "search query" playwright-cli press Enter playwright-cli screenshot
playwright-cli open https://example.com playwright-cli type "search query" playwright-cli press Enter playwright-cli screenshot

Use sessions

使用会话功能

playwright-cli open https://site1.com playwright-cli --session=project-a open https://site2.com
undefined
playwright-cli open https://site1.com playwright-cli --session=project-a open https://site2.com
undefined

Environment Variables

环境变量

VariableDescription
PLAYWRIGHT_MCP_BROWSER
Browser: chrome, firefox, webkit, msedge
PLAYWRIGHT_MCP_HEADLESS
Run headless (default: headed)
PLAYWRIGHT_MCP_ALLOWED_HOSTS
Comma-separated allowed hosts
PLAYWRIGHT_MCP_CONFIG
Path to config file
变量名描述
PLAYWRIGHT_MCP_BROWSER
指定浏览器:chrome、firefox、webkit、msedge
PLAYWRIGHT_MCP_HEADLESS
以无头模式运行(默认:有头模式)
PLAYWRIGHT_MCP_ALLOWED_HOSTS
允许访问的主机列表,用逗号分隔
PLAYWRIGHT_MCP_CONFIG
配置文件路径

Configuration

配置

Create
playwright-cli.json
for persistent settings:
json
{
  "browser": {
    "browserName": "chromium",
    "headless": false
  },
  "outputDir": "./playwright-output",
  "console": {
    "level": "info"
  }
}
创建
playwright-cli.json
文件以设置持久化配置:
json
{
  "browser": {
    "browserName": "chromium",
    "headless": false
  },
  "outputDir": "./playwright-output",
  "console": {
    "level": "info"
  }
}

Notes

注意事项

  • Cross-platform — requires Node.js 18+ (Linux, macOS, Windows)
  • Sessions persist cookies/storage by default
  • Use
    --session
    flag for isolated browser instances
  • Snapshots return element refs for subsequent commands
  • 跨平台支持 — 需要Node.js 18+(支持Linux、macOS、Windows)
  • 会话默认会保留Cookie和存储数据
  • 使用
    --session
    标志可创建独立的浏览器实例
  • 快照会返回元素引用,用于后续命令

Source

源码地址