366 lines
10 KiB
Markdown
Raw Normal View History

2026-05-20 21:39:12 +08:00
---
name: patchwright-cli
description: Anti-detection browser automation via Patchright. Use instead of playwright-cli when the target site has bot detection (Cloudflare, DataDome, etc.).
allowed-tools: Bash(patchwright-cli:*) Bash(npx:*) Bash(npm:*)
---
# Browser Automation with patchwright-cli
## When to use patchwright-cli vs playwright-cli
Both tools share the same CLI interface and commands. The difference is the underlying browser engine:
- **playwright-cli** uses stock Playwright/Chromium — suitable for most automation and testing tasks.
- **patchwright-cli** uses [Patchright](https://github.com/nickstuaw/patchright-core), a patched version of Playwright that bypasses common bot detection systems (Cloudflare Turnstile, DataDome, PerimeterX, etc.).
**Use patchwright-cli when:**
- The target site blocks automated browsers or shows CAPTCHAs
- You encounter "Access Denied", "Please verify you are human", or similar bot-detection pages
- You need to interact with sites protected by WAF/anti-bot services
**Use playwright-cli when:**
- The target site has no bot detection
- You are running Playwright tests or test generation
- You want the standard, unpatched browser behavior
If both skills are installed, prefer playwright-cli by default and switch to patchwright-cli only when bot detection is encountered.
## Anti-detection best practices
When targeting sites with bot detection, **always use `--headed` mode** (and optionally `--browser=chrome`). The default headless mode exposes `HeadlessChrome` in the User-Agent and lacks real screen/window metrics, which most anti-bot systems trivially detect.
```bash
# Recommended for bot-protected sites
patchwright-cli open --headed https://protected-site.com
# Best stealth: headed + system Chrome (real UA, real screen metrics)
patchwright-cli open --headed --browser=chrome https://protected-site.com
```
Headless mode is fine for sites without bot detection or for quick scripting tasks.
## Quick start
```bash
# open new browser (use --headed for bot-protected sites)
patchwright-cli open --headed
# navigate to a page
patchwright-cli goto https://playwright.dev
# interact with the page using refs from the snapshot
patchwright-cli click e15
patchwright-cli type "page.click"
patchwright-cli press Enter
# take a screenshot (rarely used, as snapshot is more common)
patchwright-cli screenshot
# close the browser
patchwright-cli close
```
## Commands
### Core
```bash
patchwright-cli open
# open and navigate right away
patchwright-cli open https://example.com/
patchwright-cli goto https://playwright.dev
patchwright-cli type "search query"
patchwright-cli click e3
patchwright-cli dblclick e7
# --submit presses Enter after filling the element
patchwright-cli fill e5 "user@example.com" --submit
patchwright-cli drag e2 e8
patchwright-cli hover e4
patchwright-cli select e9 "option-value"
patchwright-cli upload ./document.pdf
patchwright-cli check e12
patchwright-cli uncheck e12
patchwright-cli snapshot
patchwright-cli eval "document.title"
patchwright-cli eval "el => el.textContent" e5
# get element id, class, or any attribute not visible in the snapshot
patchwright-cli eval "el => el.id" e5
patchwright-cli eval "el => el.getAttribute('data-testid')" e5
patchwright-cli dialog-accept
patchwright-cli dialog-accept "confirmation text"
patchwright-cli dialog-dismiss
patchwright-cli resize 1920 1080
patchwright-cli close
```
### Navigation
```bash
patchwright-cli go-back
patchwright-cli go-forward
patchwright-cli reload
```
### Keyboard
```bash
patchwright-cli press Enter
patchwright-cli press ArrowDown
patchwright-cli keydown Shift
patchwright-cli keyup Shift
```
### Mouse
```bash
patchwright-cli mousemove 150 300
patchwright-cli mousedown
patchwright-cli mousedown right
patchwright-cli mouseup
patchwright-cli mouseup right
patchwright-cli mousewheel 0 100
```
### Save as
```bash
patchwright-cli screenshot
patchwright-cli screenshot e5
patchwright-cli screenshot --filename=page.png
patchwright-cli pdf --filename=page.pdf
```
### Tabs
```bash
patchwright-cli tab-list
patchwright-cli tab-new
patchwright-cli tab-new https://example.com/page
patchwright-cli tab-close
patchwright-cli tab-close 2
patchwright-cli tab-select 0
```
### Storage
```bash
patchwright-cli state-save
patchwright-cli state-save auth.json
patchwright-cli state-load auth.json
# Cookies
patchwright-cli cookie-list
patchwright-cli cookie-list --domain=example.com
patchwright-cli cookie-get session_id
patchwright-cli cookie-set session_id abc123
patchwright-cli cookie-set session_id abc123 --domain=example.com --httpOnly --secure
patchwright-cli cookie-delete session_id
patchwright-cli cookie-clear
# LocalStorage
patchwright-cli localstorage-list
patchwright-cli localstorage-get theme
patchwright-cli localstorage-set theme dark
patchwright-cli localstorage-delete theme
patchwright-cli localstorage-clear
# SessionStorage
patchwright-cli sessionstorage-list
patchwright-cli sessionstorage-get step
patchwright-cli sessionstorage-set step 3
patchwright-cli sessionstorage-delete step
patchwright-cli sessionstorage-clear
```
### Network
```bash
patchwright-cli route "**/*.jpg" --status=404
patchwright-cli route "https://api.example.com/**" --body='{"mock": true}'
patchwright-cli route-list
patchwright-cli unroute "**/*.jpg"
patchwright-cli unroute
```
### DevTools
```bash
patchwright-cli console
patchwright-cli console warning
patchwright-cli network
patchwright-cli run-code "async page => await page.context().grantPermissions(['geolocation'])"
patchwright-cli run-code --filename=script.js
patchwright-cli tracing-start
patchwright-cli tracing-stop
patchwright-cli video-start video.webm
patchwright-cli video-chapter "Chapter Title" --description="Details" --duration=2000
patchwright-cli video-stop
```
## Open parameters
```bash
# Use headed mode (recommended for bot-protected sites)
patchwright-cli open --headed
patchwright-cli open --headed --browser=chrome
# Use specific browser when creating session
patchwright-cli open --browser=chrome
patchwright-cli open --browser=firefox
patchwright-cli open --browser=webkit
patchwright-cli open --browser=msedge
# Connect to browser via extension
patchwright-cli open --extension
# Use persistent profile (by default profile is in-memory)
patchwright-cli open --persistent
# Use persistent profile with custom directory
patchwright-cli open --profile=/path/to/profile
# Start with config file
patchwright-cli open --config=my-config.json
# Close the browser
patchwright-cli close
# Delete user data for the default session
patchwright-cli delete-data
```
## Snapshots
After each command, patchwright-cli provides a snapshot of the current browser state.
```bash
> patchwright-cli goto https://example.com
### Page
- Page URL: https://example.com/
- Page Title: Example Domain
### Snapshot
[Snapshot](.patchwright-cli/page-2026-02-14T19-22-42-679Z.yml)
```
You can also take a snapshot on demand using `patchwright-cli snapshot` command. All the options below can be combined as needed.
```bash
# default - save to a file with timestamp-based name
patchwright-cli snapshot
# save to file, use when snapshot is a part of the workflow result
patchwright-cli snapshot --filename=after-click.yaml
# snapshot an element instead of the whole page
patchwright-cli snapshot "#main"
# limit snapshot depth for efficiency, take a partial snapshot afterwards
patchwright-cli snapshot --depth=4
patchwright-cli snapshot e34
```
## Targeting elements
By default, use refs from the snapshot to interact with page elements.
```bash
# get snapshot with refs
patchwright-cli snapshot
# interact using a ref
patchwright-cli click e15
```
You can also use css selectors or Playwright locators.
```bash
# css selector
patchwright-cli click "#main > button.submit"
# role locator
patchwright-cli click "getByRole('button', { name: 'Submit' })"
# test id
patchwright-cli click "getByTestId('submit-button')"
```
## Browser Sessions
```bash
# create new browser session named "mysession" with persistent profile
patchwright-cli -s=mysession open example.com --persistent
# same with manually specified profile directory (use when requested explicitly)
patchwright-cli -s=mysession open example.com --profile=/path/to/profile
patchwright-cli -s=mysession click e6
patchwright-cli -s=mysession close # stop a named browser
patchwright-cli -s=mysession delete-data # delete user data for persistent session
patchwright-cli list
# Close all browsers
patchwright-cli close-all
# Forcefully kill all browser processes
patchwright-cli kill-all
```
## Installation
If global `patchwright-cli` command is not available, try a local version via `npx patchwright-cli`:
```bash
npx --no-install patchwright-cli --version
```
When local version is available, use `npx patchwright-cli` in all commands. Otherwise, install `patchwright-cli` as a global command:
```bash
npm install -g patchwright-cli@latest
```
## Example: Form submission
```bash
patchwright-cli open https://example.com/form
patchwright-cli snapshot
patchwright-cli fill e1 "user@example.com"
patchwright-cli fill e2 "password123"
patchwright-cli click e3
patchwright-cli snapshot
patchwright-cli close
```
## Example: Multi-tab workflow
```bash
patchwright-cli open https://example.com
patchwright-cli tab-new https://example.com/other
patchwright-cli tab-list
patchwright-cli tab-select 0
patchwright-cli snapshot
patchwright-cli close
```
## Example: Debugging with DevTools
```bash
patchwright-cli open https://example.com
patchwright-cli click e4
patchwright-cli fill e7 "test"
patchwright-cli console
patchwright-cli network
patchwright-cli close
```
```bash
patchwright-cli open https://example.com
patchwright-cli tracing-start
patchwright-cli click e4
patchwright-cli fill e7 "test"
patchwright-cli tracing-stop
patchwright-cli close
```
## Specific tasks
* **Running and Debugging Playwright tests** [references/playwright-tests.md](references/playwright-tests.md)
* **Request mocking** [references/request-mocking.md](references/request-mocking.md)
* **Running Playwright code** [references/running-code.md](references/running-code.md)
* **Browser session management** [references/session-management.md](references/session-management.md)
* **Storage state (cookies, localStorage)** [references/storage-state.md](references/storage-state.md)
* **Test generation** [references/test-generation.md](references/test-generation.md)
* **Tracing** [references/tracing.md](references/tracing.md)
* **Video recording** [references/video-recording.md](references/video-recording.md)
* **Inspecting element attributes** [references/element-attributes.md](references/element-attributes.md)