Skip to content

Playwright CLI

Introduction

Playwright CLI is a robust command-line interface designed to automate browser interactions, streamline web testing, and facilitate complex data extraction. Built on top of the industry-standard Playwright framework, this tool allows developers to navigate websites, interact with UI elements, and inspect web pages without leaving the terminal.

Whether you are testing a web application, filling out repetitive forms, or generating visual assets like screenshots and PDFs, Playwright CLI provides a comprehensive suite of commands to handle modern web workflows.

Key Features:

  • Full Browser Control: Navigate, reload, and manage multiple tabs or browser sessions seamlessly.

  • Rich Interaction: Perform clicks, double-clicks, drag-and-drop actions, and complex keyboard inputs.

  • State Inspection: Take snapshots of the current page state to identify element references for further interaction.

  • Developer Tools Integration: Access console logs, monitor network activity, and start/stop performance tracing.

  • Media Generation: Capture high-quality screenshots of specific elements or entire pages, and export web content to PDF.

Installation

To get started with Playwright CLI, ensure you have Node.js installed, then install the package globally or use it via npx:

Terminal window
# Install globally
npm install -g @microsoft/playwright-cli
# Or run directly via npx
npx playwright-cli open https://example.com

Usage

The core workflow involves opening a URL, taking a snapshot to identify elements (refs), and then interacting with those elements.

Basic Interaction

Terminal window
# Open a website
playwright-cli open https://playwright.dev
# Interact with elements using references from the snapshot
playwright-cli click e15
playwright-cli type "page.click"
playwright-cli press Enter

Form Submission Example

Automate form filling by selecting elements and inputting data:

Terminal window
playwright-cli open https://example.com/form
playwright-cli snapshot
# Fill fields using element IDs (e.g., e1, e2)
playwright-cli fill e1 "user@example.com"
playwright-cli fill e2 "password123"
playwright-cli click e3
playwright-cli snapshot

Multi-Tab Management

Manage complex workflows across multiple pages:

Terminal window
playwright-cli open https://example.com
playwright-cli tab-new https://example.com/other
playwright-cli tab-list
playwright-cli tab-select 0
playwright-cli snapshot

Advanced Debugging

Utilize built-in DevTools features to debug your automation scripts:

Terminal window
playwright-cli open https://example.com
playwright-cli tracing-start
playwright-cli click e4
playwright-cli console
playwright-cli network
playwright-cli tracing-stop

Tip: Use the snapshot command frequently after page transitions or dynamic content loads to ensure you are targeting the correct element references.