Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Automate browser interactions for web testing, form filling, screenshots, and data extraction using the playwright-cli tool.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/session-management.md
1# Browser Session Management23Run multiple isolated browser sessions concurrently with state persistence.45## Named Browser Sessions67Use `-s` flag to isolate browser contexts:89```bash10# Browser 1: Authentication flow11playwright-cli -s=auth open https://app.example.com/login1213# Browser 2: Public browsing (separate cookies, storage)14playwright-cli -s=public open https://example.com1516# Commands are isolated by browser session17playwright-cli -s=auth fill e1 "[email protected]"18playwright-cli -s=public snapshot19```2021## Browser Session Isolation Properties2223Each browser session has independent:24- Cookies25- LocalStorage / SessionStorage26- IndexedDB27- Cache28- Browsing history29- Open tabs3031## Browser Session Commands3233```bash34# List all browser sessions35playwright-cli list3637# Stop a browser session (close the browser)38playwright-cli close # stop the default browser39playwright-cli -s=mysession close # stop a named browser4041# Stop all browser sessions42playwright-cli close-all4344# Forcefully kill all daemon processes (for stale/zombie processes)45playwright-cli kill-all4647# Delete browser session user data (profile directory)48playwright-cli delete-data # delete default browser data49playwright-cli -s=mysession delete-data # delete named browser data50```5152## Environment Variable5354Set a default browser session name via environment variable:5556```bash57export PLAYWRIGHT_CLI_SESSION="mysession"58playwright-cli open example.com # Uses "mysession" automatically59```6061## Common Patterns6263### Concurrent Scraping6465```bash66#!/bin/bash67# Scrape multiple sites concurrently6869# Start all browsers70playwright-cli -s=site1 open https://site1.com &71playwright-cli -s=site2 open https://site2.com &72playwright-cli -s=site3 open https://site3.com &73wait7475# Take snapshots from each76playwright-cli -s=site1 snapshot77playwright-cli -s=site2 snapshot78playwright-cli -s=site3 snapshot7980# Cleanup81playwright-cli close-all82```8384### A/B Testing Sessions8586```bash87# Test different user experiences88playwright-cli -s=variant-a open "https://app.com?variant=a"89playwright-cli -s=variant-b open "https://app.com?variant=b"9091# Compare92playwright-cli -s=variant-a screenshot93playwright-cli -s=variant-b screenshot94```9596### Persistent Profile9798By default, browser profile is kept in memory only. Use `--persistent` flag on `open` to persist the browser profile to disk:99100```bash101# Use persistent profile (auto-generated location)102playwright-cli open https://example.com --persistent103104# Use persistent profile with custom directory105playwright-cli open https://example.com --profile=/path/to/profile106```107108## Attaching to a Running Browser109110Use `attach` to connect to a browser that is already running, instead of launching a new one.111112### Attach by channel name113114Connect to a running Chrome or Edge instance by its channel name. The browser must have remote debugging enabled — navigate to `chrome://inspect/#remote-debugging` in the target browser and check "Allow remote debugging for this browser instance".115116```bash117# Attach to Chrome118playwright-cli attach --cdp=chrome119120# Attach to Chrome Canary121playwright-cli attach --cdp=chrome-canary122123# Attach to Microsoft Edge124playwright-cli attach --cdp=msedge125126# Attach to Edge Dev127playwright-cli attach --cdp=msedge-dev128```129130Supported channels: `chrome`, `chrome-beta`, `chrome-dev`, `chrome-canary`, `msedge`, `msedge-beta`, `msedge-dev`, `msedge-canary`.131132When `--session` is not provided, the session is named after the channel (e.g. `--cdp=msedge` creates a session called `msedge`), so parallel attaches to Chrome and Edge don't collide on `default`. Pass `--session=<name>` to override.133134### Attach via CDP endpoint135136Connect to a browser that exposes a Chrome DevTools Protocol endpoint:137138```bash139playwright-cli attach --cdp=http://localhost:9222140```141142### Attach via browser extension143144Connect to a browser with the Playwright extension installed:145146```bash147playwright-cli attach --extension148```149150### Detach151152Tear down an attached session without affecting the external browser:153154```bash155# Detach the default attached session156playwright-cli detach157158# Detach a specific attached session159playwright-cli -s=msedge detach160```161162`detach` only works on sessions created via `attach`. For sessions created via `open`, use `close`.163164## Default Browser Session165166When `-s` is omitted, commands use the default browser session:167168```bash169# These use the same default browser session170playwright-cli open https://example.com171playwright-cli snapshot172playwright-cli close # Stops default browser173```174175## Browser Session Configuration176177Configure a browser session with specific settings when opening:178179```bash180# Open with config file181playwright-cli open https://example.com --config=.playwright/my-cli.json182183# Open with specific browser184playwright-cli open https://example.com --browser=firefox185186# Open in headed mode187playwright-cli open https://example.com --headed188189# Open with persistent profile190playwright-cli open https://example.com --persistent191```192193## Best Practices194195### 1. Name Browser Sessions Semantically196197```bash198# GOOD: Clear purpose199playwright-cli -s=github-auth open https://github.com200playwright-cli -s=docs-scrape open https://docs.example.com201202# AVOID: Generic names203playwright-cli -s=s1 open https://github.com204```205206### 2. Always Clean Up207208```bash209# Stop browsers when done210playwright-cli -s=auth close211playwright-cli -s=scrape close212213# Or stop all at once214playwright-cli close-all215216# If browsers become unresponsive or zombie processes remain217playwright-cli kill-all218```219220### 3. Delete Stale Browser Data221222```bash223# Remove old browser data to free disk space224playwright-cli -s=oldsession delete-data225```226