patchright-cli 0.4.1__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  """patchright-cli — Undetected browser automation CLI using Patchright."""
2
2
 
3
- __version__ = "0.4.1"
3
+ __version__ = "0.5.0"
@@ -0,0 +1,348 @@
1
+ ---
2
+ name: patchright-cli
3
+ description: Anti-detect browser automation using Patchright (undetected Playwright fork). Use when you need to interact with websites that block regular Playwright/Chrome DevTools, such as Akamai/Cloudflare-protected sites. Provides the same command interface as playwright-cli but bypasses bot detection. Use this skill whenever the user asks to automate a browser, scrape a website, fill forms, log into sites, take screenshots, or do anything involving Chrome automation — especially if the target site has bot protection like Cloudflare or Akamai.
4
+ ---
5
+
6
+ # Anti-Detect Browser Automation with patchright-cli
7
+
8
+ patchright-cli drives a real Chrome browser that passes bot detection (Cloudflare, Akamai, etc.). It works as a CLI: you issue commands, get back page state. This is the tool to reach for whenever regular Playwright or Chrome DevTools gets blocked.
9
+
10
+ ## Quick start
11
+
12
+ ```bash
13
+ patchright-cli open https://example.com # launch browser + navigate
14
+ patchright-cli snapshot # get element refs
15
+ # read the snapshot YAML to find the right ref, then:
16
+ patchright-cli click e5 # interact by ref
17
+ patchright-cli fill e3 "search query" # type into an input
18
+ patchright-cli press Enter # press a key
19
+ patchright-cli screenshot # capture the page
20
+ patchright-cli close # done
21
+ ```
22
+
23
+ Every interaction follows: **open -> snapshot -> read refs -> interact -> snapshot again -> repeat**. Refs are ephemeral -- they change on every page update. If a ref fails, re-snapshot.
24
+
25
+ ## Command chaining
26
+
27
+ Commands can be chained with `&&`. The browser persists via a background daemon, so chaining is safe.
28
+
29
+ ```bash
30
+ # Chain when you don't need intermediate output
31
+ patchright-cli open https://example.com && patchright-cli screenshot
32
+
33
+ # Run separately when you need to read refs first
34
+ patchright-cli snapshot # read refs from output
35
+ patchright-cli click e5 # use discovered ref
36
+ ```
37
+
38
+ ## Global options
39
+
40
+ These go before the command:
41
+
42
+ ```bash
43
+ --headless # Run headless (default: headed -- headed is less detectable)
44
+ --persistent # Use persistent profile (keeps cookies/storage across sessions)
45
+ --profile=/path # Custom profile directory
46
+ --proxy=<url> # Proxy server (http, https, socks5) -- supports user:pass@host auth
47
+ -s=mysession # Named session (default: "default", or PATCHRIGHT_CLI_SESSION env var)
48
+ --port=9322 # Custom daemon port (default: 9321)
49
+ --config=<path> # Load options from JSON config file
50
+ --cdp=<url> # Attach to Chrome via CDP endpoint (use with `attach` command)
51
+ --device="iPhone 15" # Emulate a device
52
+ --viewport-size=1280x720 # Set viewport size
53
+ --locale=en-US # Browser locale
54
+ --timezone=America/New_York # Timezone ID
55
+ --geolocation=40.7,-74.0 # Geolocation override (lat,lon)
56
+ --user-agent=<ua> # Custom user agent string
57
+ --grant-permissions=geolocation,camera # Grant permissions at launch
58
+ --timeout-action=10000 # Default action timeout (ms)
59
+ --timeout-navigation=30000 # Default navigation timeout (ms)
60
+ --show-port=9322 # Dashboard port (default: 9322)
61
+ ```
62
+
63
+ ---
64
+
65
+ ## Core commands
66
+
67
+ ### Browser lifecycle
68
+
69
+ ```bash
70
+ open [url] # Launch browser (optionally navigate)
71
+ open --persistent # Keep cookies/storage between runs
72
+ open --headless # Headless mode
73
+ attach --cdp=<url> # Attach to existing Chrome via CDP
74
+ close # Close session
75
+ ```
76
+
77
+ ### Navigation
78
+
79
+ ```bash
80
+ goto <url> # Navigate to URL
81
+ go-back / go-forward # History navigation
82
+ reload # Reload page
83
+ url # Print current URL
84
+ title # Print page title
85
+ ```
86
+
87
+ ### Snapshots
88
+
89
+ ```bash
90
+ snapshot # Full page snapshot
91
+ snapshot <ref> # Subtree of a specific element
92
+ snapshot --depth=N # Limit depth
93
+ snapshot -i # Interactive elements only
94
+ ```
95
+
96
+ ### Interaction
97
+
98
+ ```bash
99
+ click <ref> # Left-click (smart: navigates links directly)
100
+ click <ref> right # Right-click
101
+ click <ref> --modifiers=Alt # Click with modifier keys
102
+ dblclick <ref> # Double-click
103
+ hover <ref> # Hover
104
+ drag <ref> <target-ref> # Drag element to target
105
+ ```
106
+
107
+ ### Form input
108
+
109
+ ```bash
110
+ fill <ref> "value" # Clear field and type value
111
+ fill <ref> "value" --submit # Fill and press Enter
112
+ type "text" # Type via keyboard (no target)
113
+ type "text" --submit # Type and press Enter
114
+ select <ref> "option" # Select dropdown option
115
+ check <ref> / uncheck <ref> # Checkbox/radio
116
+ ```
117
+
118
+ ### Text and screenshots
119
+
120
+ ```bash
121
+ text <ref> # Get text content by ref
122
+ text "css-selector" # Get text by CSS selector
123
+ screenshot # Page screenshot
124
+ screenshot <ref> # Element screenshot
125
+ screenshot --full-page # Full scrollable page
126
+ screenshot --filename=F # Custom filename
127
+ ```
128
+
129
+ ### Scroll, wait, and keyboard
130
+
131
+ ```bash
132
+ scroll <dx> <dy> # Scroll by pixels
133
+ scroll-to <ref> # Scroll element into view
134
+ wait <ms> # Wait N milliseconds
135
+ wait-for <ref> # Wait until element visible
136
+ wait-for <ref> --state=hidden # Wait until hidden
137
+ press <key> # Keypress (Enter, ArrowDown, etc.)
138
+ keydown <key> / keyup <key> # Hold/release key
139
+ ```
140
+
141
+ ### Mouse
142
+
143
+ ```bash
144
+ mousemove <x> <y> # Move to coordinates
145
+ mousedown / mouseup # Button down/up
146
+ mousewheel <dx> <dy> # Scroll wheel
147
+ ```
148
+
149
+ ### Tabs
150
+
151
+ ```bash
152
+ tab-list # List open tabs
153
+ tab-new <url> # Open new tab
154
+ tab-select <index> # Switch to tab
155
+ tab-close [index] # Close tab
156
+ ```
157
+
158
+ ### Dialogs
159
+
160
+ Dialogs must be pre-armed *before* the action that triggers them:
161
+
162
+ ```bash
163
+ dialog-accept # Accept next dialog
164
+ dialog-accept "OK" # Accept with text input
165
+ dialog-dismiss # Dismiss next dialog
166
+ ```
167
+
168
+ ### DevTools
169
+
170
+ ```bash
171
+ console # Show console messages
172
+ console warning # Filter by level
173
+ network # Show network requests
174
+ network --static # Include static assets
175
+ ```
176
+
177
+ ### Recording and capture
178
+
179
+ ```bash
180
+ screenshot / pdf # Static capture
181
+ video-start / video-stop # Video recording
182
+ tracing-start / tracing-stop # Playwright trace
183
+ codegen / codegen-stop # Record interactions as script
184
+ resize <w> <h> # Viewport resize
185
+ upload <file> [ref] # File upload
186
+ show # Live dashboard
187
+ ```
188
+
189
+ ### Storage
190
+
191
+ ```bash
192
+ cookie-list / cookie-get / cookie-set / cookie-delete / cookie-clear
193
+ cookie-export / cookie-import
194
+ localstorage-list / localstorage-get / localstorage-set / localstorage-delete / localstorage-clear
195
+ sessionstorage-list / sessionstorage-get / sessionstorage-set / sessionstorage-delete / sessionstorage-clear
196
+ state-save <file> / state-load <file>
197
+ ```
198
+
199
+ ### Network
200
+
201
+ ```bash
202
+ route "<pattern>" [options] # Mock requests (--status, --body, --content-type, --header)
203
+ route-list / unroute # Manage routes
204
+ network-state-set offline # Simulate offline
205
+ network-state-set online # Restore connectivity
206
+ ```
207
+
208
+ ### Sessions
209
+
210
+ ```bash
211
+ list # List active sessions
212
+ close-all # Close all sessions
213
+ kill-all # Force-kill all + stop daemon
214
+ delete-data # Delete session profile data
215
+ grant-permissions <perms> # Grant browser permissions
216
+ ```
217
+
218
+ ---
219
+
220
+ ## Common patterns
221
+
222
+ ### Login flow
223
+
224
+ ```bash
225
+ patchright-cli open https://example.com/login --persistent
226
+ patchright-cli snapshot
227
+ patchright-cli fill e3 "user@example.com"
228
+ patchright-cli fill e5 "password123"
229
+ patchright-cli click e8
230
+ patchright-cli snapshot
231
+ # With --persistent, cookies survive across sessions
232
+ ```
233
+
234
+ ### Extracting data
235
+
236
+ ```bash
237
+ patchright-cli open https://example.com/data
238
+ patchright-cli snapshot
239
+ cat > /tmp/extract.js << 'JSEOF'
240
+ JSON.stringify(
241
+ [...document.querySelectorAll('table tr')].map(row =>
242
+ [...row.cells].map(cell => cell.textContent.trim())
243
+ )
244
+ )
245
+ JSEOF
246
+ patchright-cli eval --file=/tmp/extract.js
247
+ ```
248
+
249
+ ### Using a proxy
250
+
251
+ ```bash
252
+ patchright-cli --proxy=http://host:port open https://example.com
253
+ patchright-cli --proxy=socks5://host:port open https://example.com
254
+ patchright-cli --proxy=http://user:pass@host:port open https://example.com
255
+ ```
256
+
257
+ ### Handling dialogs
258
+
259
+ ```bash
260
+ patchright-cli dialog-accept # Arm before triggering action
261
+ patchright-cli click e5 # This click triggers the dialog
262
+ ```
263
+
264
+ ### Waiting for dynamic content
265
+
266
+ ```bash
267
+ patchright-cli wait-for e12 # Wait for element
268
+ patchright-cli wait 1000 # Fixed wait
269
+ # For custom conditions, use run-code (see references/running-code.md)
270
+ ```
271
+
272
+ ---
273
+
274
+ ## Anti-detect notes
275
+
276
+ - Uses real Chrome (not Chromium) -- this is what makes it undetectable
277
+ - Patchright patches `navigator.webdriver` and other detection vectors automatically
278
+ - Headed by default -- headless mode is more detectable, use only when necessary
279
+ - No custom user-agent or headers by default -- preserves Chrome's natural fingerprint
280
+ - Persistent profiles maintain realistic browser history and cookies
281
+ - The daemon architecture means Chrome stays running between commands, behaving like a real user's browser
282
+ - `attach --cdp` lets you connect to an existing Chrome instance
283
+
284
+ ## Config files
285
+
286
+ Create `.patchright-cli/config.json` in your project directory (auto-discovered) or pass `--config=<path>`:
287
+
288
+ ```json
289
+ {
290
+ "headless": false,
291
+ "persistent": true,
292
+ "proxy": "http://proxy:8080",
293
+ "device": "iPhone 15",
294
+ "locale": "en-US",
295
+ "timezone": "America/New_York"
296
+ }
297
+ ```
298
+
299
+ CLI flags always override config file values.
300
+
301
+ ## Installation
302
+
303
+ ### CLI tool
304
+
305
+ ```bash
306
+ # Recommended -- always runs latest version
307
+ uvx patchright-cli <command>
308
+
309
+ # Or install globally
310
+ pip install patchright-cli
311
+ ```
312
+
313
+ ### Skill (for AI coding agents)
314
+
315
+ Install the skill so your agent knows how to use patchright-cli. The same command also works to update to the latest version.
316
+
317
+ **macOS / Linux (bash/zsh):**
318
+
319
+ | Agent | Install / update command |
320
+ |-------|--------------------------|
321
+ | **Claude Code** | `mkdir -p ~/.claude/skills/patchright-cli && curl -sL https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md -o ~/.claude/skills/patchright-cli/SKILL.md` |
322
+ | **Gemini CLI** | `mkdir -p ~/.gemini/skills/patchright-cli && curl -sL https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md -o ~/.gemini/skills/patchright-cli/SKILL.md` |
323
+ | **Codex CLI** | `mkdir -p ~/.codex/skills/patchright-cli && curl -sL https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md -o ~/.codex/skills/patchright-cli/SKILL.md` |
324
+ | **OpenCode** | `mkdir -p ~/.opencode/skills/patchright-cli && curl -sL https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md -o ~/.opencode/skills/patchright-cli/SKILL.md` |
325
+
326
+ **Windows (PowerShell):**
327
+
328
+ | Agent | Install / update command |
329
+ |-------|--------------------------|
330
+ | **Claude Code** | `New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.claude\skills\patchright-cli" | Out-Null; Invoke-WebRequest -Uri "https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md" -OutFile "$env:USERPROFILE\.claude\skills\patchright-cli\SKILL.md"` |
331
+ | **Gemini CLI** | `New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.gemini\skills\patchright-cli" | Out-Null; Invoke-WebRequest -Uri "https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md" -OutFile "$env:USERPROFILE\.gemini\skills\patchright-cli\SKILL.md"` |
332
+ | **Codex CLI** | `New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.codex\skills\patchright-cli" | Out-Null; Invoke-WebRequest -Uri "https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md" -OutFile "$env:USERPROFILE\.codex\skills\patchright-cli\SKILL.md"` |
333
+ | **OpenCode** | `New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.opencode\skills\patchright-cli" | Out-Null; Invoke-WebRequest -Uri "https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md" -OutFile "$env:USERPROFILE\.opencode\skills\patchright-cli\SKILL.md"` |
334
+
335
+ **IDE-based agents (Cursor / Windsurf / Aider):** Copy `SKILL.md` to `.<tool>/skills/patchright-cli/` in your project.
336
+
337
+ **Or just tell your agent:**
338
+ > Install patchright-cli skill from https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md
339
+
340
+ ## Specific tasks
341
+
342
+ * **Snapshots and element refs** [references/snapshot-refs.md](references/snapshot-refs.md)
343
+ * **Browser session management** [references/session-management.md](references/session-management.md)
344
+ * **Request mocking** [references/request-mocking.md](references/request-mocking.md)
345
+ * **Storage state (cookies, localStorage)** [references/storage-state.md](references/storage-state.md)
346
+ * **Running JavaScript** [references/running-code.md](references/running-code.md)
347
+ * **Tracing** [references/tracing.md](references/tracing.md)
348
+ * **Video recording & capture** [references/video-recording.md](references/video-recording.md)
@@ -0,0 +1,103 @@
1
+ # Request Mocking
2
+
3
+ ## Route command
4
+
5
+ Intercept and modify network requests matching a URL pattern:
6
+
7
+ ```bash
8
+ patchright-cli route "<pattern>" [options]
9
+ ```
10
+
11
+ The pattern uses glob syntax (e.g., `**/*.jpg`, `https://api.example.com/**`).
12
+
13
+ ### Options
14
+
15
+ | Flag | Effect |
16
+ |------|--------|
17
+ | `--status=N` | Respond with HTTP status code N |
18
+ | `--body=<string>` | Respond with this body content |
19
+ | `--content-type=<mime>` | Set the Content-Type header |
20
+ | `--header=Name:Value` | Add or override a response header |
21
+ | `--remove-header=Name` | Remove a response header |
22
+
23
+ ### Examples
24
+
25
+ ```bash
26
+ # Block all images (respond with 404)
27
+ patchright-cli route "**/*.jpg" --status=404
28
+
29
+ # Mock an API endpoint
30
+ patchright-cli route "https://api.example.com/**" --body='{"mock": true}' --content-type=application/json
31
+
32
+ # Add a custom header to all responses
33
+ patchright-cli route "**/*" --header=X-Custom:value
34
+
35
+ # Remove a header from responses
36
+ patchright-cli route "**/*" --remove-header=Content-Type
37
+
38
+ # Block all CSS (speeds up testing)
39
+ patchright-cli route "**/*.css" --status=404
40
+
41
+ # Mock a specific API response
42
+ patchright-cli route "https://api.example.com/users" --body='[{"id":1,"name":"Test"}]' --content-type=application/json --status=200
43
+ ```
44
+
45
+ ## Managing routes
46
+
47
+ ```bash
48
+ patchright-cli route-list # List all active routes
49
+ patchright-cli unroute "**/*.jpg" # Remove a specific route
50
+ patchright-cli unroute # Remove all routes
51
+ ```
52
+
53
+ ## Network state
54
+
55
+ Simulate offline/online conditions:
56
+
57
+ ```bash
58
+ patchright-cli network-state-set offline # Simulate offline
59
+ patchright-cli network-state-set online # Restore connectivity
60
+ ```
61
+
62
+ ## Common patterns
63
+
64
+ ### Block tracking and analytics
65
+
66
+ ```bash
67
+ patchright-cli route "**/analytics/**" --status=204
68
+ patchright-cli route "**/tracking/**" --status=204
69
+ patchright-cli route "**/*.gif?*" --status=204 # Tracking pixels
70
+ ```
71
+
72
+ ### Mock authentication API
73
+
74
+ ```bash
75
+ patchright-cli route "https://api.example.com/auth/login" \
76
+ --body='{"token":"fake-jwt","user":{"id":1}}' \
77
+ --content-type=application/json \
78
+ --status=200
79
+ ```
80
+
81
+ ### Test error handling
82
+
83
+ ```bash
84
+ # Simulate server error on API calls
85
+ patchright-cli route "https://api.example.com/**" --status=500 --body='{"error":"Internal Server Error"}'
86
+
87
+ # Simulate network timeout (offline mode)
88
+ patchright-cli network-state-set offline
89
+ ```
90
+
91
+ ## DevTools inspection
92
+
93
+ Monitor network activity and console output:
94
+
95
+ ```bash
96
+ patchright-cli console # Show console messages (last 50)
97
+ patchright-cli console warning # Filter by level
98
+ patchright-cli console --clear # Clear buffer after printing
99
+
100
+ patchright-cli network # Show requests (excludes static assets)
101
+ patchright-cli network --static # Include images, fonts, scripts, etc.
102
+ patchright-cli network --clear # Clear log after printing
103
+ ```
@@ -0,0 +1,129 @@
1
+ # Running JavaScript
2
+
3
+ ## eval
4
+
5
+ Evaluate a JavaScript expression and return the result.
6
+
7
+ ```bash
8
+ patchright-cli eval --file=/tmp/expr.js
9
+ ```
10
+
11
+ ### Why use --file (not inline JS)
12
+
13
+ **Always use `--file` or stdin for `eval` and `run-code`.** Inline JS breaks because quotes get mangled through shell layers (bash -> uvx -> python).
14
+
15
+ ```bash
16
+ # WRONG -- nested quotes break
17
+ patchright-cli eval "document.querySelector('a').href"
18
+
19
+ # RIGHT -- write to a temp file
20
+ cat > /tmp/check.js << 'JSEOF'
21
+ JSON.stringify({title: document.title, links: document.querySelectorAll('a').length})
22
+ JSEOF
23
+ patchright-cli eval --file=/tmp/check.js
24
+
25
+ # RIGHT -- pipe via stdin (simple expressions only)
26
+ echo 'document.title' | patchright-cli eval
27
+ ```
28
+
29
+ ### eval with element ref
30
+
31
+ Target a specific element by passing a ref as the second argument. The element is received as the first parameter of the expression:
32
+
33
+ ```bash
34
+ echo 'el => el.textContent' | patchright-cli eval - e5
35
+ echo 'el => el.href' | patchright-cli eval - e3
36
+ ```
37
+
38
+ ### eval options
39
+
40
+ | Usage | Effect |
41
+ |-------|--------|
42
+ | `eval --file=<path>` | Read expression from file |
43
+ | `eval` (no args) | Read expression from stdin |
44
+ | `eval <expr> <ref>` | Evaluate on a specific element (element passed as first arg) |
45
+
46
+ ## run-code
47
+
48
+ Run a JavaScript code block wrapped in `async () => { ... }`, so you can use `return` and `await`:
49
+
50
+ ```bash
51
+ cat > /tmp/scroll.js << 'JSEOF'
52
+ window.scrollTo(0, document.body.scrollHeight);
53
+ return document.body.scrollHeight;
54
+ JSEOF
55
+ patchright-cli run-code --file=/tmp/scroll.js
56
+ ```
57
+
58
+ ### run-code options
59
+
60
+ | Usage | Effect |
61
+ |-------|--------|
62
+ | `run-code --file=<path>` | Read code from file |
63
+ | `run-code` (no args) | Read code from stdin |
64
+
65
+ ## Temp file heredoc pattern
66
+
67
+ The recommended pattern for running JS from a shell:
68
+
69
+ ```bash
70
+ cat > /tmp/myscript.js << 'JSEOF'
71
+ // Your JavaScript here
72
+ const data = document.querySelectorAll('.item');
73
+ return JSON.stringify([...data].map(el => el.textContent));
74
+ JSEOF
75
+ patchright-cli run-code --file=/tmp/myscript.js
76
+ ```
77
+
78
+ Use `'JSEOF'` (quoted) to prevent shell variable expansion inside the heredoc.
79
+
80
+ ## Common patterns
81
+
82
+ ### Extract structured data
83
+
84
+ ```bash
85
+ cat > /tmp/extract.js << 'JSEOF'
86
+ JSON.stringify(
87
+ [...document.querySelectorAll('table tr')].map(row =>
88
+ [...row.cells].map(cell => cell.textContent.trim())
89
+ )
90
+ )
91
+ JSEOF
92
+ patchright-cli eval --file=/tmp/extract.js
93
+ ```
94
+
95
+ ### Wait for a custom condition
96
+
97
+ ```bash
98
+ cat > /tmp/wait.js << 'JSEOF'
99
+ for (let i = 0; i < 30; i++) {
100
+ if (document.querySelector('.results-loaded')) return true;
101
+ await new Promise(r => setTimeout(r, 500));
102
+ }
103
+ return false;
104
+ JSEOF
105
+ patchright-cli run-code --file=/tmp/wait.js
106
+ ```
107
+
108
+ ### Scroll to bottom of infinite scroll
109
+
110
+ ```bash
111
+ cat > /tmp/infinite.js << 'JSEOF'
112
+ let prev = 0;
113
+ for (let i = 0; i < 20; i++) {
114
+ window.scrollTo(0, document.body.scrollHeight);
115
+ await new Promise(r => setTimeout(r, 1000));
116
+ if (document.body.scrollHeight === prev) break;
117
+ prev = document.body.scrollHeight;
118
+ }
119
+ return document.body.scrollHeight;
120
+ JSEOF
121
+ patchright-cli run-code --file=/tmp/infinite.js
122
+ ```
123
+
124
+ ### Get element attribute or style
125
+
126
+ ```bash
127
+ echo 'el => el.getAttribute("href")' | patchright-cli eval - e5
128
+ echo 'el => getComputedStyle(el).color' | patchright-cli eval - e3
129
+ ```
@@ -0,0 +1,106 @@
1
+ # Browser Session Management
2
+
3
+ ## Named sessions
4
+
5
+ By default, patchright-cli uses a session named `"default"`. Use `-s=name` to create or switch to a named session:
6
+
7
+ ```bash
8
+ patchright-cli -s=session1 open https://site-a.com
9
+ patchright-cli -s=session2 open https://site-b.com
10
+ ```
11
+
12
+ Each named session has its own independent browser context -- separate tabs, cookies, storage, and history.
13
+
14
+ ### Default session via environment variable
15
+
16
+ Set `PATCHRIGHT_CLI_SESSION` to avoid passing `-s` on every command:
17
+
18
+ ```bash
19
+ export PATCHRIGHT_CLI_SESSION=myproject
20
+ patchright-cli open https://example.com # Uses "myproject" session
21
+ patchright-cli snapshot # Same session
22
+ ```
23
+
24
+ ## Persistent profiles
25
+
26
+ Use `--persistent` to keep cookies, localStorage, and browser data across sessions:
27
+
28
+ ```bash
29
+ patchright-cli open https://example.com --persistent
30
+ # ... log in, do work ...
31
+ patchright-cli close
32
+ # Later:
33
+ patchright-cli open https://example.com --persistent
34
+ # Cookies and storage are restored
35
+ ```
36
+
37
+ Profiles are stored at `~/.patchright-cli/profiles/<session-name>`. Use `--profile=/path` to specify a custom profile directory:
38
+
39
+ ```bash
40
+ patchright-cli open --persistent --profile=/path/to/my/profile https://example.com
41
+ ```
42
+
43
+ ## CDP attach
44
+
45
+ Connect to an already-running Chrome instance via Chrome DevTools Protocol instead of launching a new one:
46
+
47
+ ```bash
48
+ patchright-cli attach --cdp=http://localhost:9222
49
+ ```
50
+
51
+ This is useful for debugging or controlling a browser you launched manually with `--remote-debugging-port`.
52
+
53
+ ## Session commands
54
+
55
+ ```bash
56
+ patchright-cli list # List all active sessions
57
+ patchright-cli close # Close current session gracefully
58
+ patchright-cli close-all # Close all sessions gracefully
59
+ patchright-cli kill-all # Force-kill all sessions + stop daemon
60
+ patchright-cli delete-data # Delete default session's profile data
61
+ patchright-cli -s=mysession delete-data # Delete named session's profile data
62
+ ```
63
+
64
+ ## Concurrent sessions
65
+
66
+ You can run multiple sessions simultaneously for parallel workflows:
67
+
68
+ ```bash
69
+ patchright-cli -s=scrape1 open https://site1.com --persistent
70
+ patchright-cli -s=scrape2 open https://site2.com --persistent
71
+ # Work with both independently
72
+ patchright-cli -s=scrape1 snapshot
73
+ patchright-cli -s=scrape2 snapshot
74
+ ```
75
+
76
+ ## Dashboard
77
+
78
+ The `show` command starts a local web dashboard that streams live screenshots of all active sessions:
79
+
80
+ ```bash
81
+ patchright-cli show # Open at http://127.0.0.1:9322
82
+ patchright-cli show --show-port=9400 # Custom port
83
+ ```
84
+
85
+ ## Granting permissions
86
+
87
+ Grant browser permissions (geolocation, camera, etc.) to a running session:
88
+
89
+ ```bash
90
+ patchright-cli grant-permissions geolocation,camera
91
+ patchright-cli grant-permissions notifications --origin=https://example.com
92
+ ```
93
+
94
+ You can also grant permissions at launch with `--grant-permissions=geolocation,camera`.
95
+
96
+ ## Daemon behavior
97
+
98
+ The daemon auto-starts on the first command and auto-shuts down after 5 minutes of inactivity. If it ever crashes, the next command will respawn it automatically.
99
+
100
+ ## Cleanup best practices
101
+
102
+ - Use `close` when done with a session to free resources
103
+ - Use `close-all` at the end of a multi-session workflow
104
+ - Use `delete-data` to remove persistent profile data you no longer need
105
+ - Use `kill-all` as a last resort if sessions are stuck
106
+ - Persistent profiles accumulate data over time -- periodically clean up unused ones