Implementation:Microsoft Playwright CLI Automation Commands
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, CLI |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete tool for executing one-off browser automation tasks (open, screenshot, PDF) from the command line, provided by the Playwright library.
Description
The CLI Automation Commands implementation provides three primary commands in the Playwright CLI, plus browser-alias shortcuts:
open [url] -- Launches a browser in headful mode, navigates to the given URL, and exposes the Playwright console API via context._exposeConsoleApi(). This allows the user to interact with the page and call Playwright methods directly from the browser's developer console. The browser remains open until the user closes the last page or presses Ctrl+C.
screenshot <url> <filename> -- Launches a headless browser, navigates to the URL, optionally waits for a CSS selector or a timeout, and captures the page as an image file. The --full-page flag captures the entire scrollable area rather than just the visible viewport.
pdf <url> <filename> -- Launches a headless Chromium browser (PDF generation is Chromium-only), navigates to the URL, optionally waits, and renders the page to a PDF file. The --paper-format option accepts standard paper sizes (Letter, Legal, Tabloid, Ledger, A0-A6).
Browser shortcuts (cr, ff, wk) -- Convenience commands that call open with the browser pre-selected. For example, npx playwright cr https://example.com is equivalent to npx playwright open -b chromium https://example.com.
All four command types are registered via commandWithOpenOptions(), inheriting the full set of context-configuration flags (device, viewport, geolocation, proxy, etc.).
The screenshot functionality is backed by the Screenshotter class in the server layer, which provides screenshotPage() and screenshotElement() with fine-grained control over image type, quality, background omission, animation handling, clipping, scaling, caret visibility, and CSS mask overlays.
Usage
Use this implementation when:
- Taking a quick screenshot of a deployed web page from CI.
- Generating PDF reports from web-based dashboards.
- Interactively exploring a page with specific emulation settings.
- Scripting visual regression captures in shell scripts.
Code Reference
Source Location
- Repository: playwright
- open command:
packages/playwright-core/src/cli/program.ts(lines 58-66) - open function:
packages/playwright-core/src/cli/program.ts(lines 553-557) - screenshot command:
packages/playwright-core/src/cli/program.ts(lines 262-272) - screenshot function:
packages/playwright-core/src/cli/program.ts(lines 638-647) - pdf command:
packages/playwright-core/src/cli/program.ts(lines 274-284) - pdf function:
packages/playwright-core/src/cli/program.ts(lines 649-659) - browser shortcuts:
packages/playwright-core/src/cli/program.ts(lines 246-260) - Screenshotter class:
packages/playwright-core/src/server/screenshotter.ts(lines 37-49 for ScreenshotOptions type)
Signature
// Open an interactive browser session
async function open(
options: Options,
url: string | undefined
): Promise<void>;
// Capture a screenshot
async function screenshot(
options: Options,
captureOptions: CaptureOptions,
url: string,
path: string
): Promise<void>;
// Generate a PDF (Chromium only)
async function pdf(
options: Options,
captureOptions: CaptureOptions,
url: string,
path: string
): Promise<void>;
// Capture options shared by screenshot and pdf
type CaptureOptions = {
waitForSelector?: string;
waitForTimeout?: string;
fullPage: boolean;
paperFormat?: string;
};
// Screenshotter options (server layer)
type ScreenshotOptions = {
type?: 'png' | 'jpeg';
quality?: number;
omitBackground?: boolean;
animations?: 'disabled' | 'allow';
mask?: { frame: Frame; selector: string }[];
maskColor?: string;
fullPage?: boolean;
clip?: Rect;
scale?: 'css' | 'device';
caret?: 'hide' | 'initial';
style?: string;
};
Import
# CLI usage -- all commands are subcommands of the playwright CLI
npx playwright open [url]
npx playwright screenshot <url> <filename>
npx playwright pdf <url> <filename>
# Browser shortcuts
npx playwright cr [url]
npx playwright ff [url]
npx playwright wk [url]
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | string | Varies | URL to navigate to. Required for screenshot and pdf. Optional for open (opens a blank page if omitted).
|
| filename | string | Yes (screenshot/pdf) | Output file path for the captured screenshot or PDF. |
| --wait-for-selector | string | No | CSS selector to wait for before capturing. The command waits until an element matching the selector is visible. |
| --wait-for-timeout | string | No | Time in milliseconds to wait before capturing. Applied after --wait-for-selector if both are specified.
|
| --full-page | boolean | No (screenshot only) | Capture the entire scrollable page instead of just the visible viewport. |
| --paper-format | string | No (pdf only) | Paper size for PDF output: Letter, Legal, Tabloid, Ledger, A0, A1, A2, A3, A4, A5, A6. |
All commandWithOpenOptions flags |
various | No | All browser selection and context configuration options (--browser, --device, --viewport-size, --geolocation, etc.) are available. |
Outputs
| Name | Type | Description |
|---|---|---|
| Interactive browser window | GUI window | (open only) A headful browser window with the Playwright console API exposed.
|
| Screenshot image file | File (PNG/JPEG) | (screenshot only) The captured page screenshot written to the specified path.
|
| PDF file | File (PDF) | (pdf only) The rendered PDF written to the specified path.
|
| Console output | stdout | Progress messages: "Navigating to ...", "Capturing screenshot into ...", "Saving as pdf into ...". |
Usage Examples
Open Interactive Browser
# Open a page in the default browser (Chromium) for interactive exploration
npx playwright open https://example.com
# Open in WebKit using the shortcut
npx playwright wk https://example.com
Capture Screenshot
# Take a viewport screenshot with WebKit
npx playwright screenshot -b webkit https://example.com example.png
# Take a full-page screenshot after waiting for a specific element
npx playwright screenshot --full-page --wait-for-selector ".main-content" https://example.com full.png
Generate PDF
# Generate an A4 PDF (Chromium only)
npx playwright pdf --paper-format A4 https://example.com output.pdf
# Wait for dynamic content before generating PDF
npx playwright pdf --wait-for-timeout 3000 https://example.com report.pdf
Combined with Context Options
# Screenshot with device emulation and dark mode
npx playwright screenshot --device "iPhone 11" --color-scheme dark https://example.com mobile-dark.png
# PDF with custom viewport
npx playwright pdf --viewport-size="1440,900" https://example.com wide.pdf
Related Pages
Implements Principle
Requires Environment
- Environment:Microsoft_Playwright_Browser_Binaries_Environment
- Environment:Microsoft_Playwright_Platform_Support_Environment