patchright-cli 0.4.1__py3-none-any.whl → 0.6.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.
- patchright_cli/__init__.py +1 -1
- patchright_cli/_skills/patchright-cli/SKILL.md +350 -0
- patchright_cli/_skills/patchright-cli/references/request-mocking.md +103 -0
- patchright_cli/_skills/patchright-cli/references/running-code.md +129 -0
- patchright_cli/_skills/patchright-cli/references/session-management.md +106 -0
- patchright_cli/_skills/patchright-cli/references/snapshot-refs.md +86 -0
- patchright_cli/_skills/patchright-cli/references/storage-state.md +113 -0
- patchright_cli/_skills/patchright-cli/references/tracing.md +53 -0
- patchright_cli/_skills/patchright-cli/references/video-recording.md +85 -0
- patchright_cli/cli.py +131 -5
- patchright_cli/daemon.py +311 -18
- patchright_cli/ref_registry.py +27 -1
- patchright_cli/snapshot.py +4 -2
- {patchright_cli-0.4.1.dist-info → patchright_cli-0.6.0.dist-info}/METADATA +54 -55
- patchright_cli-0.6.0.dist-info/RECORD +20 -0
- patchright_cli-0.4.1.dist-info/RECORD +0 -12
- {patchright_cli-0.4.1.dist-info → patchright_cli-0.6.0.dist-info}/WHEEL +0 -0
- {patchright_cli-0.4.1.dist-info → patchright_cli-0.6.0.dist-info}/entry_points.txt +0 -0
- {patchright_cli-0.4.1.dist-info → patchright_cli-0.6.0.dist-info}/licenses/LICENSE +0 -0
patchright_cli/__init__.py
CHANGED
|
@@ -0,0 +1,350 @@
|
|
|
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
|
+
--raw # Strip page/snapshot decorations from output
|
|
62
|
+
--json # Wrap full response as JSON ({success, output, ...})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Core commands
|
|
68
|
+
|
|
69
|
+
### Browser lifecycle
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
open [url] # Launch browser (optionally navigate)
|
|
73
|
+
open --persistent # Keep cookies/storage between runs
|
|
74
|
+
open --headless # Headless mode
|
|
75
|
+
attach --cdp=<url> # Attach to existing Chrome via CDP
|
|
76
|
+
close # Close session (closes the browser)
|
|
77
|
+
detach # Detach an attached session (keeps external browser running)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Navigation
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
goto <url> # Navigate to URL
|
|
84
|
+
go-back / go-forward # History navigation
|
|
85
|
+
reload # Reload page
|
|
86
|
+
url # Print current URL
|
|
87
|
+
title # Print page title
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Snapshots
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
snapshot # Full page snapshot
|
|
94
|
+
snapshot <ref> # Subtree of a specific element
|
|
95
|
+
snapshot --depth=N # Limit depth
|
|
96
|
+
snapshot -i # Interactive elements only
|
|
97
|
+
snapshot --boxes # Append [box=x,y,w,h] to each [ref=eN] line
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Interaction
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
click <ref> # Left-click (smart: navigates links directly)
|
|
104
|
+
click <ref> right # Right-click
|
|
105
|
+
click <ref> --modifiers=Alt # Click with modifier keys
|
|
106
|
+
dblclick <ref> # Double-click
|
|
107
|
+
hover <ref> # Hover
|
|
108
|
+
drag <ref> <target-ref> # Drag element to target
|
|
109
|
+
drop <ref> --path=./file.png # Drop file onto element (HTML5 drop from outside)
|
|
110
|
+
drop <ref> --data="text/plain=hello" # Drop arbitrary MIME data
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Form input
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
fill <ref> "value" # Clear field and type value
|
|
117
|
+
fill <ref> "value" --submit # Fill and press Enter
|
|
118
|
+
type "text" # Type via keyboard (no target)
|
|
119
|
+
type "text" --submit # Type and press Enter
|
|
120
|
+
select <ref> "option" # Select dropdown option
|
|
121
|
+
check <ref> / uncheck <ref> # Checkbox/radio
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Text and screenshots
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
text <ref> # Get text content by ref
|
|
128
|
+
text "css-selector" # Get text by CSS selector
|
|
129
|
+
screenshot # Page screenshot
|
|
130
|
+
screenshot <ref> # Element screenshot
|
|
131
|
+
screenshot --full-page # Full scrollable page
|
|
132
|
+
screenshot --filename=F # Custom filename
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Scroll, wait, and keyboard
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
scroll <dx> <dy> # Scroll by pixels
|
|
139
|
+
scroll-to <ref> # Scroll element into view
|
|
140
|
+
wait <ms> # Wait N milliseconds
|
|
141
|
+
wait-for <ref> # Wait until element visible
|
|
142
|
+
wait-for <ref> --state=hidden # Wait until hidden
|
|
143
|
+
press <key> # Keypress (Enter, ArrowDown, etc.)
|
|
144
|
+
keydown <key> / keyup <key> # Hold/release key
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Mouse
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
mousemove <x> <y> # Move to coordinates
|
|
151
|
+
mousedown / mouseup # Button down/up
|
|
152
|
+
mousewheel <dx> <dy> # Scroll wheel
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Tabs
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
tab-list # List open tabs
|
|
159
|
+
tab-new <url> # Open new tab
|
|
160
|
+
tab-select <index> # Switch to tab
|
|
161
|
+
tab-close [index] # Close tab
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Dialogs
|
|
165
|
+
|
|
166
|
+
Dialogs must be pre-armed *before* the action that triggers them:
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
dialog-accept # Accept next dialog
|
|
170
|
+
dialog-accept "OK" # Accept with text input
|
|
171
|
+
dialog-dismiss # Dismiss next dialog
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### DevTools
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
console # Show console messages
|
|
178
|
+
console warning # Filter by level
|
|
179
|
+
network # Show network requests (each line is `#<id> METHOD STATUS URL`)
|
|
180
|
+
requests # Alias for `network`
|
|
181
|
+
network --static # Include static assets
|
|
182
|
+
request <id> # Full detail for one request (headers + body)
|
|
183
|
+
request <id> --body # Also fetch response body
|
|
184
|
+
generate-locator <ref> # Emit Playwright locator string for a ref
|
|
185
|
+
highlight <ref> # Persistent overlay on element [--style=CSS]
|
|
186
|
+
highlight <ref> --hide # Remove overlay; `highlight --hide` clears all
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Recording and capture
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
screenshot / pdf # Static capture
|
|
193
|
+
video-start / video-stop # Video recording
|
|
194
|
+
tracing-start / tracing-stop # Playwright trace
|
|
195
|
+
codegen / codegen-stop # Record interactions as script
|
|
196
|
+
resize <w> <h> # Viewport resize
|
|
197
|
+
upload <file> [ref] # File upload
|
|
198
|
+
show # Live dashboard
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Storage
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
cookie-list / cookie-get / cookie-set / cookie-delete / cookie-clear
|
|
205
|
+
cookie-export / cookie-import
|
|
206
|
+
localstorage-list / localstorage-get / localstorage-set / localstorage-delete / localstorage-clear
|
|
207
|
+
sessionstorage-list / sessionstorage-get / sessionstorage-set / sessionstorage-delete / sessionstorage-clear
|
|
208
|
+
state-save <file> / state-load <file>
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Network
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
route "<pattern>" [options] # Mock requests (--status, --body, --content-type, --header)
|
|
215
|
+
route-list / unroute # Manage routes
|
|
216
|
+
network-state-set offline # Simulate offline
|
|
217
|
+
network-state-set online # Restore connectivity
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Sessions
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
list # List active sessions
|
|
224
|
+
close-all # Close all sessions
|
|
225
|
+
kill-all # Force-kill all + stop daemon
|
|
226
|
+
delete-data # Delete session profile data
|
|
227
|
+
grant-permissions <perms> # Grant browser permissions
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Common patterns
|
|
233
|
+
|
|
234
|
+
### Login flow
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
patchright-cli open https://example.com/login --persistent
|
|
238
|
+
patchright-cli snapshot
|
|
239
|
+
patchright-cli fill e3 "user@example.com"
|
|
240
|
+
patchright-cli fill e5 "password123"
|
|
241
|
+
patchright-cli click e8
|
|
242
|
+
patchright-cli snapshot
|
|
243
|
+
# With --persistent, cookies survive across sessions
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Extracting data
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
patchright-cli open https://example.com/data
|
|
250
|
+
patchright-cli snapshot
|
|
251
|
+
cat > /tmp/extract.js << 'JSEOF'
|
|
252
|
+
JSON.stringify(
|
|
253
|
+
[...document.querySelectorAll('table tr')].map(row =>
|
|
254
|
+
[...row.cells].map(cell => cell.textContent.trim())
|
|
255
|
+
)
|
|
256
|
+
)
|
|
257
|
+
JSEOF
|
|
258
|
+
patchright-cli eval --file=/tmp/extract.js
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Using a proxy
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
patchright-cli --proxy=http://host:port open https://example.com
|
|
265
|
+
patchright-cli --proxy=socks5://host:port open https://example.com
|
|
266
|
+
patchright-cli --proxy=http://user:pass@host:port open https://example.com
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Handling dialogs
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
patchright-cli dialog-accept # Arm before triggering action
|
|
273
|
+
patchright-cli click e5 # This click triggers the dialog
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Waiting for dynamic content
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
patchright-cli wait-for e12 # Wait for element
|
|
280
|
+
patchright-cli wait 1000 # Fixed wait
|
|
281
|
+
# For custom conditions, use run-code (see references/running-code.md)
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
---
|
|
285
|
+
|
|
286
|
+
## Anti-detect notes
|
|
287
|
+
|
|
288
|
+
- Uses real Chrome (not Chromium) -- this is what makes it undetectable
|
|
289
|
+
- Patchright patches `navigator.webdriver` and other detection vectors automatically
|
|
290
|
+
- Headed by default -- headless mode is more detectable, use only when necessary
|
|
291
|
+
- No custom user-agent or headers by default -- preserves Chrome's natural fingerprint
|
|
292
|
+
- Persistent profiles maintain realistic browser history and cookies
|
|
293
|
+
- The daemon architecture means Chrome stays running between commands, behaving like a real user's browser
|
|
294
|
+
- `attach --cdp` lets you connect to an existing Chrome instance
|
|
295
|
+
|
|
296
|
+
## Config files
|
|
297
|
+
|
|
298
|
+
Create `.patchright-cli/config.json` in your project directory (auto-discovered) or pass `--config=<path>`:
|
|
299
|
+
|
|
300
|
+
```json
|
|
301
|
+
{
|
|
302
|
+
"headless": false,
|
|
303
|
+
"persistent": true,
|
|
304
|
+
"proxy": "http://proxy:8080",
|
|
305
|
+
"device": "iPhone 15",
|
|
306
|
+
"locale": "en-US",
|
|
307
|
+
"timezone": "America/New_York"
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
CLI flags always override config file values.
|
|
312
|
+
|
|
313
|
+
## Installation
|
|
314
|
+
|
|
315
|
+
### CLI tool
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
# Recommended -- always runs latest version
|
|
319
|
+
uvx patchright-cli <command>
|
|
320
|
+
|
|
321
|
+
# Or install globally
|
|
322
|
+
pip install patchright-cli
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Skill (for AI coding agents)
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
# Install for all detected agents (Claude Code, Cursor, Gemini, Codex, 40+ more)
|
|
329
|
+
npx skills add AhaiMk01/patchright-cli
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Other methods:
|
|
333
|
+
|
|
334
|
+
```bash
|
|
335
|
+
# Via patchright-cli itself
|
|
336
|
+
pip install patchright-cli && patchright-cli install --skills
|
|
337
|
+
|
|
338
|
+
# Or just tell your agent:
|
|
339
|
+
# "Install patchright-cli skill from https://raw.githubusercontent.com/AhaiMk01/patchright-cli/main/skills/patchright-cli/SKILL.md"
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## Specific tasks
|
|
343
|
+
|
|
344
|
+
* **Snapshots and element refs** [references/snapshot-refs.md](references/snapshot-refs.md)
|
|
345
|
+
* **Browser session management** [references/session-management.md](references/session-management.md)
|
|
346
|
+
* **Request mocking** [references/request-mocking.md](references/request-mocking.md)
|
|
347
|
+
* **Storage state (cookies, localStorage)** [references/storage-state.md](references/storage-state.md)
|
|
348
|
+
* **Running JavaScript** [references/running-code.md](references/running-code.md)
|
|
349
|
+
* **Tracing** [references/tracing.md](references/tracing.md)
|
|
350
|
+
* **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
|