Skip to main content

会话与仪表板

🌐 Sessions & Dashboard

CLI 默认会将浏览器配置文件保存在内存中——会话期间 CLI 调用之间会保留 cookie 和存储状态,但浏览器关闭时会丢失。

🌐 The CLI keeps the browser profile in memory by default — cookies and storage state are preserved between CLI calls within a session but lost when the browser closes.

命名会话

🌐 Named sessions

运行多个独立的浏览器实例:

🌐 Run multiple isolated browser instances:

playwright-cli open https://playwright.nodejs.cn
playwright-cli -s=example open https://example.com --persistent
playwright-cli list

### Browsers

- default:
- status: open
- browser-type: chrome
- user-data-dir: <in-memory>
- headed: false
- example:
- status: open
- browser-type: chrome
- user-data-dir: /Users/me/Library/Caches/ms-playwright/daemon/8f2c.../ud-example-chrome
- headed: false

每个会话都有自己的浏览器实例、cookies、本地存储、IndexedDB、缓存、导航历史、打开的标签页和控制台日志。

🌐 Each session has its own browser instance, cookies, localStorage, IndexedDB, cache, navigation history, open tabs, and console log.

playwright-cli list --all 将列表扩展到每个工作区,并添加了 Playwright 浏览器服务器以及可用于 附加 的本地 Chrome/Edge 配置文件。

环境变量

🌐 Environment variable

PLAYWRIGHT_CLI_SESSION=todo-app claude .

该代理会话中的所有 playwright-cli 命令都使用 todo-app 浏览器实例。

🌐 All playwright-cli commands in that agent session use the todo-app browser instance.

配置文件持久性

🌐 Profile persistence

内存中(默认)

🌐 In-memory (default)

Cookie 和存储在会话中的命令之间会持续存在,但在浏览器关闭时会丢失:

🌐 Cookies and storage persist between commands within a session but are lost when the browser closes:

playwright-cli open https://example.com

持久化到磁盘

🌐 Persistent to disk

配置文件已保存到磁盘,并且在浏览器重启后仍然存在。这相当于 Playwright MCP 中的默认持久模式。

🌐 Profile is saved to disk and survives browser restarts. This is equivalent to the default persistent mode in Playwright MCP.

playwright-cli open https://example.com --persistent

配置文件目录是由工作区和会话名称 — ud-<session>-<browser> 推导出来的,位于每个工作区的目录下:

平台默认位置
macOS~/Library/Caches/ms-playwright/daemon/<workspace-hash>/
Linux$XDG_CACHE_HOME/ms-playwright/daemon/<workspace-hash>/(默认是 ~/.cache
Windows%LOCALAPPDATA%\ms-playwright\daemon\<workspace-hash>\

自定义目录

🌐 Custom directory

playwright-cli open https://example.com --profile=./my-profile

会话管理

🌐 Session management

playwright-cli list # list sessions in this workspace
playwright-cli list --all # list sessions across all workspaces
playwright-cli -s=name close # close a specific session
playwright-cli -s=name detach # detach without closing an attached browser
playwright-cli close-all # close all browsers
playwright-cli kill-all # force kill (for unresponsive browsers)
playwright-cli -s=name delete-data # delete stored profile data

close 会停止 CLI 启动的浏览器。对于使用 attach 创建的会话,请使用 detach —— 它会终止会话但保持外部浏览器运行。详情见 Attach

仪表板

🌐 Dashboard

打开可视化仪表板以观察和控制所有正在运行的会话:

🌐 Open a visual dashboard to observe and control all running sessions:

playwright-cli show

仪表板提供:

🌐 The dashboard provides:

查看描述
会话网格所有活动会话,带实时屏幕投射、名称、网址和标题。点击以放大。
会话详情带有标签栏、导航控件和完整远程鼠标/键盘输入的实时视口。按 Esc 键释放。

使用场景:

🌐 Use cases:

  • 观看编码代理在后台自动操作浏览器
  • 当代理被验证码或双重身份验证卡住时接管
  • 关闭过期会话或从界面删除数据

选项

🌐 Options

playwright-cli show # launch the dashboard in the background
playwright-cli -s=name show # launch it and reveal a specific session
playwright-cli show --port=0 # run as a blocking HTTP server on a random port
playwright-cli show --port=8080 --host=0.0.0.0
playwright-cli show --kill # stop the dashboard daemon

注释模式

🌐 Annotation mode

show --annotate 将仪表盘变成一个反馈工具:用户可以在实时页面上画框并输入评论,命令会返回带注释的截图、被标记区域的快照和注意。它会阻塞直到用户完成,这使得它成为在自动化过程中请求 UI 审查或设计反馈的方式。

playwright-cli open https://example.com
playwright-cli show --annotate

保存和恢复状态

🌐 Saving and restoring state

保存身份验证状态以便在会话之间重用:

🌐 Save authenticated state for reuse across sessions:

# Login, then save state
playwright-cli state-save auth-state.json

# Later: restore state in a new session
playwright-cli state-load auth-state.json

工作流程:隔离测试

🌐 Workflow: isolated testing

# Admin session
playwright-cli -s=admin open https://app.example.com --persistent
playwright-cli -s=admin state-load admin-auth.json
playwright-cli -s=admin goto /admin/settings

# User session
playwright-cli -s=user open https://app.example.com --persistent
playwright-cli -s=user state-load user-auth.json
playwright-cli -s=user goto /dashboard

# Monitor both
playwright-cli show