Create a session
Sessions provide isolated state for your automation tasks. Each session has its own This outputs a session ID (e.g.,
state object that persists between commands.1). You’ll use this ID in all subsequent commands.Navigate to a page
Use the
execute command to run Playwright code. Navigate to a website:Always wrap
-e code in single quotes ('...') to prevent bash from interpreting $, backticks, and other special characters. Use double quotes for strings inside the JavaScript code.Take a snapshot
Get an accessibility snapshot of the page. This shows all interactive elements with their locators:Example output:The snapshot shows interactive elements and their Playwright locators (in square brackets).
Interact with elements
Use locators from the snapshot to click elements. The snapshot provides the exact selector:Or use the locator directly:
Create your own page
The defaultpage variable is shared across all sessions. To avoid interference from other agents, create your own page:
state.page for all subsequent operations:
Multiline code
For complex logic, use a heredoc to avoid bash quoting issues:'EOF' delimiter disables all bash expansion, so $, backticks, and single quotes work inside the JavaScript.
Session management
List all active sessions:Taking screenshots
For visual layout issues, take a screenshot with visual labels:Persist data in state
Thestate object persists between execute calls within your session:
state. Sessions don’t interfere with each other.
What’s available in scope?
When you runplaywriter -s <id> -e '<code>', the following variables are in scope:
page- a default page (may be shared with other agents)context- browser context, access all pages viacontext.pages()state- object persisted between calls within your sessionrequire- load Node.js modules (e.g.,const fs = require('node:fs'))- Node.js globals:
setTimeout,fetch,URL,Buffer,crypto, etc.
Common patterns
Get page title
Click a button
Fill an input
Wait for selector
Extract data
Next steps
CLI usage
Learn advanced CLI features and best practices
API reference
Explore all available functions and utilities
Examples
See real-world automation examples
Troubleshooting
Fix common issues and view logs