Playwright CLI 会话管理:多浏览器会话、隔离、持久化与清理

基于官方 session-management 参考文档,整理 Playwright CLI 中命名浏览器会话、会话隔离、持久化 profile、并发使用和清理命令的常用方法。

如果你在用 Playwright CLI 做自动化,很快就会碰到一个实际问题:同一时间里,能不能开多个互不干扰的浏览器会话?答案是可以,而且 Playwright CLI 已经把这套机制做得很直接。

这篇就按照官方 session-management 参考文档,整理它最实用的几部分:命名会话、会话隔离、持久化 profile、并发模式,以及常见清理命令。文中的命令行与命令块说明均按参考内容保留。

01 命名浏览器会话

官方建议用 -s 参数隔离不同浏览器上下文:

1
2
3
4
5
6
7
8
9
# Browser 1: Authentication flow
playwright-cli -s=auth open https://app.example.com/login

# Browser 2: Public browsing (separate cookies, storage)
playwright-cli -s=public open https://example.com

# Commands are isolated by browser session
playwright-cli -s=auth fill e1 "user@example.com"
playwright-cli -s=public snapshot

这里的重点是:不同 session 名称对应不同浏览器上下文。你可以把 auth 用在登录流程,把 public 用在匿名访问,它们之间不会共用 cookies 或本地状态。

02 浏览器会话隔离了什么

每个浏览器会话都会独立维护下面这些内容:

  • Cookies
  • LocalStorage / SessionStorage
  • IndexedDB
  • Cache
  • Browsing history
  • Open tabs

这意味着如果你在 auth 会话里登录了某个网站,并不会自动影响 public 会话。做多账号测试、登录态验证、匿名对比时,这一点尤其重要。

03 浏览器会话相关命令

官方文档把会话管理里最常用的一组命令放在了一起:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# List all browser sessions
playwright-cli list

# Stop a browser session (close the browser)
playwright-cli close                 # stop the default browser
playwright-cli -s=mysession close   # stop a named browser

# Stop all browser sessions
playwright-cli close-all

# Forcefully kill all daemon processes (for stale/zombie processes)
playwright-cli kill-all

# Delete browser session user data (profile directory)
playwright-cli delete-data              # delete default browser data
playwright-cli -s=mysession delete-data # delete named browser data

可以把它们理解成三类操作:

  • list:看当前有哪些会话
  • close / close-all / kill-all:结束会话或清理卡住的浏览器进程
  • delete-data:删除某个会话对应的用户数据目录

如果你只是结束浏览器,通常先用 close;如果已经出现残留进程或僵尸进程,再用 kill-all 会更合适。

04 用环境变量设置默认会话

如果你不想每条命令都重复写 -s=mysession,官方还提供了环境变量方式:

1
2
export PLAYWRIGHT_CLI_SESSION="mysession"
playwright-cli open example.com  # Uses "mysession" automatically

这样之后不显式指定 -s 时,命令就会默认使用 mysession 这个浏览器会话。

05 常见模式:并发抓取

参考文档给了一个很典型的并发抓取例子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/bash
# Scrape multiple sites concurrently

# Start all browsers
playwright-cli -s=site1 open https://site1.com &
playwright-cli -s=site2 open https://site2.com &
playwright-cli -s=site3 open https://site3.com &
wait

# Take snapshots from each
playwright-cli -s=site1 snapshot
playwright-cli -s=site2 snapshot
playwright-cli -s=site3 snapshot

# Cleanup
playwright-cli close-all

这个模式适合同时打开多个站点,各自抓取页面状态,再统一清理。因为每个站点都跑在独立会话里,所以不会互相污染本地状态。

06 常见模式:A/B 测试会话

另一个常见场景,是同时对比不同实验版本:

1
2
3
4
5
6
7
# Test different user experiences
playwright-cli -s=variant-a open "https://app.com?variant=a"
playwright-cli -s=variant-b open "https://app.com?variant=b"

# Compare
playwright-cli -s=variant-a screenshot
playwright-cli -s=variant-b screenshot

这种写法很适合做 A/B 页面差异对比,因为两个版本在独立会话里运行,截图和状态检查也更容易分开管理。

07 持久化浏览器 profile

官方文档特别说明:默认情况下,浏览器 profile 只保存在内存里。

如果你希望把浏览器 profile 持久化到磁盘,需要在 open 时加上 --persistent

1
2
3
4
5
# Use persistent profile (auto-generated location)
playwright-cli open https://example.com --persistent

# Use persistent profile with custom directory
playwright-cli open https://example.com --profile=/path/to/profile

这个能力适合需要长期复用登录态、本地缓存或扩展调试环境的场景。尤其在反复调试同一站点时,持久化 profile 往往会比每次从零开始更高效。

08 默认浏览器会话

如果没有显式传入 -s,那么命令会使用默认浏览器会话:

1
2
3
4
# These use the same default browser session
playwright-cli open https://example.com
playwright-cli snapshot
playwright-cli close  # Stops default browser

也就是说,不带 -s 的几条命令默认是在同一个默认会话里连续执行的。

09 打开时附带会话配置

除了会话名,官方也给出了几种常见的启动配置方式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Open with config file
playwright-cli open https://example.com --config=.playwright/my-cli.json

# Open with specific browser
playwright-cli open https://example.com --browser=firefox

# Open in headed mode
playwright-cli open https://example.com --headed

# Open with persistent profile
playwright-cli open https://example.com --persistent

这些参数可以和会话管理一起配合使用。比如你可以让某个命名会话固定跑在 firefox,或者让某个会话始终以 headed 模式启动,方便人工观察。

10 官方给出的最佳实践

参考文档列了三条很实用的最佳实践。

1. 用有语义的会话名

1
2
3
4
5
6
# GOOD: Clear purpose
playwright-cli -s=github-auth open https://github.com
playwright-cli -s=docs-scrape open https://docs.example.com

# AVOID: Generic names
playwright-cli -s=s1 open https://github.com

会话名最好能直接表达用途。像 github-authdocs-scrape 这种名字,后面维护脚本时会清楚很多。

2. 用完及时清理

1
2
3
4
5
6
7
8
9
# Stop browsers when done
playwright-cli -s=auth close
playwright-cli -s=scrape close

# Or stop all at once
playwright-cli close-all

# If browsers become unresponsive or zombie processes remain
playwright-cli kill-all

如果任务结束后不关浏览器,会话和后台进程会一直留着。短期看问题不大,但任务一多,很容易把环境弄乱。

3. 删除陈旧浏览器数据

1
2
# Remove old browser data to free disk space
playwright-cli -s=oldsession delete-data

当某些旧会话已经不用时,删除对应数据目录可以节省磁盘空间,也能避免后面误用过期状态。

11 快速总结

如果只抓重点,可以记住下面这几件事:

  • -s=<name> 用来创建并使用独立浏览器会话
  • 不同会话之间会隔离 cookies、各种存储、缓存、历史记录和标签页
  • close-all 适合统一关闭,kill-all 适合处理异常残留进程
  • --persistent 用来把 profile 落盘,适合长期复用状态
  • 会话名尽量语义化,旧数据定期清理

如果你的工作流里已经有登录态复用、多账号并行、A/B 对比或批量抓取需求,那么 session management 基本就是 Playwright CLI 里最值得先掌握的一块能力。

参考链接

  • Playwright CLI session-management 参考文档:https://github.com/microsoft/playwright-cli/blob/main/skills/playwright-cli/references/session-management.md
  • Playwright CLI 项目主页:https://github.com/microsoft/playwright-cli
记录并分享
使用 Hugo 构建
主题 StackJimmy 设计