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 設計