Puppeteer Actions Reference
Puppeteer Actions Reference
Section titled “Puppeteer Actions Reference”Overview
Section titled “Overview”The Puppeteer Actions system provides a comprehensive set of browser automation capabilities through a unified, type-safe interface. All actions are executed within authenticated browser contexts with enterprise-focused security controls.
Architecture
Section titled “Architecture”interface BrowserAction { action: ActionType; params: ActionParams; timeout?: number;}
Actions are processed through:
- Security Validation: Input sanitization and XSS prevention
- Authorization: Session-based access control
- Execution: Action-specific handlers with error recovery
- Monitoring: Performance timing and audit logging
Action Types
Section titled “Action Types”Navigation Actions
Section titled “Navigation Actions”navigate
Section titled “navigate”Navigate to a specified URL with optional wait conditions.
{ action: "navigate", params: { url: "https://example.com", waitUntil?: "load" | "domcontentloaded" | "networkidle0" | "networkidle2", timeout?: 30000 }}
goBack / goForward
Section titled “goBack / goForward”Navigate browser history.
{ action: "goBack" | "goForward", params: { waitUntil?: "load" | "domcontentloaded" | "networkidle0" | "networkidle2", timeout?: 30000 }}
reload
Section titled “reload”Reload the current page.
{ action: "reload", params: { waitUntil?: "load" | "domcontentloaded" | "networkidle0" | "networkidle2", timeout?: 30000 }}
Interaction Actions
Section titled “Interaction Actions”Click an element using CSS selector or coordinates.
{ action: "click", params: { selector?: string, x?: number, y?: number, button?: "left" | "right" | "middle", clickCount?: number, delay?: number }}
Type text into an input field.
{ action: "type", params: { selector: string, text: string, delay?: number, // Delay between keystrokes clear?: boolean // Clear field before typing }}
select
Section titled “select”Select option(s) from a dropdown.
{ action: "select", params: { selector: string, values: string[] // Option values to select }}
upload
Section titled “upload”Upload files to a file input.
{ action: "upload", params: { selector: string, files: string[] // File paths to upload }}
hover / focus / blur
Section titled “hover / focus / blur”Mouse and focus interactions.
{ action: "hover" | "focus" | "blur", params: { selector: string }}
Content Actions
Section titled “Content Actions”evaluate
Section titled “evaluate”Execute JavaScript in the page context.
{ action: "evaluate", params: { script: string, args?: any[] // Arguments to pass to script }}
Security: Scripts are validated for dangerous keywords (eval, Function constructor, etc.) before execution.
screenshot
Section titled “screenshot”Capture page screenshot.
{ action: "screenshot", params: { fullPage?: boolean, clip?: { x: number, y: number, width: number, height: number }, quality?: number, // JPEG quality (0-100) type?: "jpeg" | "png", encoding?: "base64" | "binary" }}
Generate PDF from page.
{ action: "pdf", params: { format?: "Letter" | "Legal" | "A4" | "A3", landscape?: boolean, scale?: number, displayHeaderFooter?: boolean, margin?: { top: string, bottom: string, left: string, right: string }, printBackground?: boolean }}
content
Section titled “content”Get page HTML content.
{ action: "content", params: {} // No parameters required}
Utility Actions
Section titled “Utility Actions”Wait for various conditions.
{ action: "wait", params: { selector?: string, // Wait for selector timeout?: number, // Wait for timeout visible?: boolean, // Wait for visibility hidden?: boolean, // Wait for hidden function?: string // Wait for JS function to return true }}
scroll
Section titled “scroll”Scroll page or element.
{ action: "scroll", params: { x?: number, y?: number, selector?: string, // Element to scroll behavior?: "auto" | "smooth" }}
keyboard
Section titled “keyboard”Send keyboard events.
{ action: "keyboard", params: { type: "press" | "down" | "up", key: string, // Key name (e.g., "Enter", "Escape") text?: string // For typing text }}
Send mouse events.
{ action: "mouse", params: { type: "move" | "down" | "up" | "click", x: number, y: number, button?: "left" | "right" | "middle" }}
cookies
Section titled “cookies”Manage browser cookies.
{ action: "cookies", params: { operation: "get" | "set" | "delete", cookies?: Array<{ name: string, value?: string, domain?: string, path?: string, expires?: number, httpOnly?: boolean, secure?: boolean, sameSite?: "Strict" | "Lax" | "None" }> }}
Common Patterns
Section titled “Common Patterns”Form Submission
Section titled “Form Submission”// Fill and submit a login formconst actions = [ { action: 'navigate', params: { url: 'https://example.com/login' } }, { action: 'type', params: { selector: '#username', text: 'user@example.com' } }, { action: 'type', params: { selector: '#password', text: 'password' } }, { action: 'click', params: { selector: '#submit-button' } }, { action: 'wait', params: { selector: '.dashboard', timeout: 5000 } },];
Data Extraction
Section titled “Data Extraction”// Extract text from multiple elementsconst extractAction = { action: 'evaluate', params: { script: ` Array.from(document.querySelectorAll('.item')) .map(el => ({ title: el.querySelector('.title')?.textContent, price: el.querySelector('.price')?.textContent })) `, },};
Visual Testing
Section titled “Visual Testing”// Capture screenshots at different viewport sizesconst viewports = [ { width: 1920, height: 1080 }, // Desktop { width: 768, height: 1024 }, // Tablet { width: 375, height: 667 }, // Mobile];
for (const viewport of viewports) { // Set viewport (via context configuration) await executeAction({ action: 'screenshot', params: { fullPage: true, type: 'png', }, });}
Error Handling
Section titled “Error Handling”Action Validation Errors
Section titled “Action Validation Errors”- Invalid selector: Element not found within timeout
- Invalid URL: Malformed or disallowed protocol
- Script validation failure: Dangerous JavaScript detected
- File not found: Upload file path doesn’t exist
Timeout Handling
Section titled “Timeout Handling”All actions support configurable timeouts:
- Default: 30 seconds for navigation, 5 seconds for interactions
- Maximum: 120 seconds
- Timeout errors include action context for debugging
Recovery Strategies
Section titled “Recovery Strategies”// Retry pattern with exponential backoffconst retryAction = async (action: BrowserAction, maxRetries = 3) => { for (let i = 0; i < maxRetries; i++) { try { return await executeAction(action); } catch (error) { if (i === maxRetries - 1) throw error; await new Promise((resolve) => setTimeout(resolve, Math.pow(2, i) * 1000)); } }};
Performance Considerations
Section titled “Performance Considerations”Action Batching
Section titled “Action Batching”Execute multiple actions in sequence to minimize round-trips:
// Batch related actionsconst batchedActions = [ { action: 'navigate', params: { url: 'https://example.com' } }, { action: 'wait', params: { selector: '.content' } }, { action: 'screenshot', params: { fullPage: true } },];
Resource Usage
Section titled “Resource Usage”- Screenshots: Large full-page screenshots can consume significant memory
- PDFs: Complex pages may take longer to render
- Evaluate: Minimize script complexity for better performance
- Navigation: Use appropriate
waitUntil
conditions
Best Practices
Section titled “Best Practices”- Use specific selectors: ID selectors are fastest, avoid complex CSS
- Set appropriate timeouts: Don’t use excessive timeouts
- Validate inputs client-side: Reduce server round-trips
- Monitor action duration: Track performance metrics
- Clean up resources: Close contexts when done
Performance Metrics
Section titled “Performance Metrics”Actions automatically collect:
- Execution duration
- Success/failure rates
- Timeout occurrences
- Resource consumption
Security Considerations
Section titled “Security Considerations”Input Validation
Section titled “Input Validation”All actions undergo strict validation:
- URL allowlist for navigation
- Script sanitization for evaluate
- Path validation for uploads
- Selector injection prevention
Access Control
Section titled “Access Control”- Session-based authorization required
- Action-level permissions
- Audit logging for compliance
- Rate limiting per session
NIST Compliance
Section titled “NIST Compliance”/** * @nist ac-3 "Access enforcement" * @nist si-10 "Information input validation" * @nist sc-18 "Mobile code" */
API Integration
Section titled “API Integration”REST API
Section titled “REST API”POST /api/v1/contexts/{contextId}/executeContent-Type: application/jsonAuthorization: Bearer {token}
{ "action": "navigate", "params": { "url": "https://example.com" }}
MCP Tool
Section titled “MCP Tool”{ "tool": "execute-in-context", "arguments": { "contextId": "ctx-123", "command": "screenshot", "parameters": { "fullPage": true } }}
WebSocket
Section titled “WebSocket”{ "type": "context.execute", "contextId": "ctx-123", "action": { "action": "click", "params": { "selector": "#button" } }}
For more information, see: