7# Browser Automation with playwright-cli
8
9## Quick start
10
11```bash
12# open new browser
13playwright-cli open
14# navigate to a page
15playwright-cli goto https://playwright.dev
16# interact with the page using refs from the snapshot
17playwright-cli click e15
18playwright-cli type "page.click"
19playwright-cli press Enter
20# take a screenshot (rarely used, as snapshot is more common)
21playwright-cli screenshot
22# close the browser
23playwright-cli close
24```
25
26## Commands
27
28### Core
29
30```bash
31playwright-cli open
32# open and navigate right away
33playwright-cli open https://example.com/
34playwright-cli goto https://playwright.dev
35playwright-cli type "search query"
36playwright-cli click e3
37playwright-cli dblclick e7
38# --submit presses Enter after filling the element
39playwright-cli fill e5 "user@example.com" --submit
40playwright-cli drag e2 e8
41# drop files or data onto an element (from outside the page)
42playwright-cli drop e4 --path=./image.png
43playwright-cli drop e4 --data="text/plain=hello world"
44playwright-cli hover e4
45playwright-cli select e9 "option-value"
46playwright-cli upload ./document.pdf
47playwright-cli check e12
48playwright-cli uncheck e12
49playwright-cli snapshot
50# search the snapshot for text or a regexp, returns matching nodes with surrounding context
51playwright-cli find "Sign in"
52playwright-cli find --regex "Sign (in|up)"
53# wrap the regexp in slashes to add flags, e.g. /i for case-insensitive
54playwright-cli find --regex "/sign (in|up)/i"
55playwright-cli eval "document.title"
56playwright-cli eval "el => el.textContent" e5
57# get element id, class, or any attribute not visible in the snapshot
58playwright-cli eval "el => el.id" e5
59playwright-cli eval "el => el.getAttribute('data-testid')" e5
60playwright-cli dialog-accept
61playwright-cli dialog-accept "confirmation text"
62playwright-cli dialog-dismiss
63playwright-cli resize 1920 1080
64playwright-cli close
65```
66
67### Navigation
68
69```bash
70playwright-cli go-back
71playwright-cli go-forward
72playwright-cli reload
73```
74
75### Keyboard
76
77```bash
78playwright-cli press Enter
79playwright-cli press ArrowDown
80playwright-cli keydown Shift
81playwright-cli keyup Shift
82```
83
84### Mouse
85
86```bash
87playwright-cli mousemove 150 300
88playwright-cli mousedown
89playwright-cli mousedown right
90playwright-cli mouseup
91playwright-cli mouseup right
92playwright-cli mousewheel 0 100
93```
94
95### Save as
96
97```bash
98playwright-cli screenshot
99playwright-cli screenshot e5
100playwright-cli screenshot --filename=page.png
101playwright-cli screenshot --hires
102playwright-cli pdf --filename=page.pdf
103```
104
105### Tabs
106
107```bash
108playwright-cli tab-list
109playwright-cli tab-new
110playwright-cli tab-new https://example.com/page
111playwright-cli tab-close
112playwright-cli tab-close 2
113playwright-cli tab-select 0
114```
115
116### Storage
117
118```bash
119playwright-cli state-save
120playwright-cli state-save auth.json
121playwright-cli state-load auth.json
122
123# Cookies
124playwright-cli cookie-list
125playwright-cli cookie-list --domain=example.com
126playwright-cli cookie-get session_id
127playwright-cli cookie-set session_id abc123
128playwright-cli cookie-set session_id abc123 --domain=example.com --httpOnly --secure
129playwright-cli cookie-delete session_id
130playwright-cli cookie-clear
131
132# LocalStorage
133playwright-cli localstorage-list
134playwright-cli localstorage-get theme
135playwright-cli localstorage-set theme dark
136playwright-cli localstorage-delete theme
137playwright-cli localstorage-clear
138
139# SessionStorage
140playwright-cli sessionstorage-list
141playwright-cli sessionstorage-get step
142playwright-cli sessionstorage-set step 3
143playwright-cli sessionstorage-delete step
144playwright-cli sessionstorage-clear
145```
146
147### Network
148
149```bash
150playwright-cli route "**/*.jpg" --status=404
151playwright-cli route "https://api.example.com/**" --body='{"mock": true}'
152playwright-cli route-list
153playwright-cli unroute "**/*.jpg"
154playwright-cli unroute
155```
156
157### DevTools
158
159```bash
160playwright-cli console
161playwright-cli console warning
162playwright-cli requests
163playwright-cli request 5
164playwright-cli run-code "async page => await page.context().grantPermissions(['geolocation'])"
165playwright-cli run-code --filename=script.js
166playwright-cli tracing-start
167playwright-cli tracing-stop
168playwright-cli video-start video.webm
169playwright-cli video-chapter "Chapter Title" --description="Details" --duration=2000
170playwright-cli video-stop
171
172# annotate each subsequent action (click, type, ...) with a callout naming the action and highlighting the target
173playwright-cli video-show-actions --duration=600 --position=top-right
174playwright-cli video-hide-actions
175
176# launch the dashboard for UI review / design feedback — user annotates the page, you receive the annotated screenshot, snapshot, and notes
177playwright-cli show --annotate
178
179# generate a Playwright locator for an element from its ref or selector
180playwright-cli generate-locator e5 --raw
181
182# show a persistent highlight overlay for an element, optionally with a custom style
183playwright-cli highlight e5
184playwright-cli highlight e5 --style="outline: 3px dashed red"
185# hide a single element highlight, or all page highlights when no target is given
186playwright-cli highlight e5 --hide
187playwright-cli highlight --hide
188```
189
190## Raw output
191
192The global --raw option strips page status, generated code, and snapshot sections from the output, returning only the result value. Use it to pipe command output into other tools. Commands that don't produce output return nothing.
193
194```bash
195playwright-cli --raw eval "JSON.stringify(performance.timing)" | jq '.loadEventEnd - .navigationStart'
196playwright-cli --raw eval "JSON.stringify([...document.querySelectorAll('a')].map(a => a.href))" > links.json
197playwright-cli --raw snapshot > before.yml
198playwright-cli click e5
199playwright-cli --raw snapshot > after.yml
200diff before.yml after.yml
201TOKEN=$(playwright-cli --raw cookie-get session_id)
202playwright-cli --raw localstorage-get theme
203```
204
205For structured output wrapping every reply as JSON, pass --json
206```bash
207playwright-cli list --json
208```
209
210## Open parameters
211```bash
212# Use specific browser when creating session
213playwright-cli open --browser=chrome
214playwright-cli open --browser=firefox
215playwright-cli open --browser=webkit
216playwright-cli open --browser=msedge
217
218# Emulate a generic mobile device (Pixel 10 for Chromium, iPhone 17 for WebKit).
219# Prefer this when a mobile layout is acceptable: mobile pages are usually
220# lighter, so snapshots are smaller and cheaper.
221playwright-cli open --mobile
222playwright-cli open --device="iPhone 15"
223
224# Use persistent profile (by default profile is in-memory)
225playwright-cli open --persistent
226# Use persistent profile with custom directory
227playwright-cli open --profile=/path/to/profile
228
229# Connect to browser via Playwright Extension
230playwright-cli attach --extension=chrome
231
232# Connect to a running Chrome or Edge by channel name
233playwright-cli attach --cdp=chrome
234playwright-cli attach --cdp=msedge
235
236# Connect to a running browser via CDP endpoint
237playwright-cli attach --cdp=http://localhost:9222
238
239# Start with config file
240playwright-cli open --config=my-config.json
241
242# Close the browser
243playwright-cli close
244# Detach from an attached browser (leaves the external browser running)
245playwright-cli -s=msedge detach
246# Delete user data for the default session
247playwright-cli delete-data
248```
249
250## URLs with & on Windows
251
252On Windows, cmd.exe and PowerShell treat & as a command separator, so URLs with multiple query parameters get truncated before playwright-cli runs. Escape & with ^& in cmd.exe, or use --% in PowerShell:
253
254```batch
255playwright-cli goto "https://example.com/?a=1^&b=2"
256```
257
258```powershell
259playwright-cli --% goto "https://example.com/?a=1&b=2"
260```
261
262## Snapshots
263
264After each command, playwright-cli provides a snapshot of the current browser state.
265
266```bash
267> playwright-cli goto https://example.com
268### Page
269- Page URL: https://example.com/
270- Page Title: Example Domain
271### Snapshot
272[Snapshot](.playwright-cli/page-2026-02-14T19-22-42-679Z.yml)
273```
274
275You can also take a snapshot on demand using playwright-cli snapshot command. All the options below can be combined as needed.
276
277```bash
278# default - save to a file with timestamp-based name
279playwright-cli snapshot
280
281# save to file, use when snapshot is a part of the workflow result
282playwright-cli snapshot --filename=after-click.yaml
283
284# snapshot an element instead of the whole page
285playwright-cli snapshot "#main"
286
287# limit snapshot depth for efficiency, take a partial snapshot afterwards
288playwright-cli snapshot --depth=4
289playwright-cli snapshot e34
290
291# include each element's bounding box as [box=x,y,width,height]
292playwright-cli snapshot --boxes
293
294# search a large snapshot instead of capturing it all — returns matching nodes
295# with 3 lines of context around each match (like grep -C)
296playwright-cli find "Add to cart"
297playwright-cli find --regex "\\$[0-9]+\\.[0-9]{2}"
298```
299
300## Targeting elements
301
302By default, use refs from the snapshot to interact with page elements.
303
304```bash
305# get snapshot with refs
306playwright-cli snapshot
307
308# interact using a ref
309playwright-cli click e15
310```
311
312You can also use css selectors or Playwright locators.
313
314```bash
315# css selector
316playwright-cli click "#main > button.submit"
317
318# role locator
319playwright-cli click "getByRole('button', { name: 'Submit' })"
320
321# test id
322playwright-cli click "getByTestId('submit-button')"
323```
324
325## Browser Sessions
326
327```bash
328# create new browser session named "mysession" with persistent profile
329playwright-cli -s=mysession open example.com --persistent
330# same with manually specified profile directory (use when requested explicitly)
331playwright-cli -s=mysession open example.com --profile=/path/to/profile
332playwright-cli -s=mysession click e6
333playwright-cli -s=mysession close # stop a named browser
334playwright-cli -s=mysession delete-data # delete user data for persistent session
335
336playwright-cli list
337# Close all browsers
338playwright-cli close-all
339# Forcefully kill all browser processes
340playwright-cli kill-all
341```
342
343## Installation
344
345If global playwright-cli command is not available, try a local version via npx playwright cli:
346
347```bash
348npx --no-install playwright --version
349```
350
351When local version is available, use npx playwright cli in all commands. Otherwise, install playwright-cli as a global command:
352
353```bash
354npm install -g @playwright/cli@latest
355```
356
357## Example: Form submission
358
359```bash
360playwright-cli open https://example.com/form
361playwright-cli snapshot
362
363playwright-cli fill e1 "user@example.com"
364playwright-cli fill e2 "password123"
365playwright-cli click e3
366playwright-cli snapshot
367playwright-cli close
368```
369
370## Example: Multi-tab workflow
371
372```bash
373playwright-cli open https://example.com
374playwright-cli tab-new https://example.com/other
375playwright-cli tab-list
376playwright-cli tab-select 0
377playwright-cli snapshot
378playwright-cli close
379```
380
381## Example: Debugging with DevTools
382
383```bash
384playwright-cli open https://example.com
385playwright-cli click e4
386playwright-cli fill e7 "test"
387playwright-cli console
388playwright-cli requests
389playwright-cli close
390```
391
392```bash
393playwright-cli open https://example.com
394playwright-cli tracing-start
395playwright-cli click e4
396playwright-cli fill e7 "test"
397playwright-cli tracing-stop
398playwright-cli close
399```
400
401## Example: Interactive session
402
403Ask the user for UI review or design feedback. The user draws boxes on the live page and types comments; you receive the annotated screenshot, the snapshot of the marked region, and the user's notes. Use this whenever the user asks for "UI review", "design feedback", or to "ask the user what they think / want / mean":
404
405```bash
406playwright-cli open https://example.com
407playwright-cli show --annotate
408```
409
410## Specific tasks
411
412* **Running and Debugging Playwright tests** [references/playwright-tests.md](references/playwright-tests.md)
413* **Request mocking** [references/request-mocking.md](references/request-mocking.md)
414* **Running Playwright code** [references/running-code.md](references/running-code.md)
415* **Browser session management** [references/session-management.md](references/session-management.md)
416* **Storage state (cookies, localStorage)** [references/storage-state.md](references/storage-state.md)
417* **Test generation (plan / generate / heal)** [references/test-generation.md](references/test-generation.md)
418* **Tracing** [references/tracing.md](references/tracing.md)
419* **Video recording** [references/video-recording.md](references/video-recording.md)
420* **Inspecting element attributes** [references/element-attributes.md](references/element-attributes.md)
421