Skip to content

High resolution screenshots #376

Closed
microsoft/playwright
#41465

Description

I'm trying to automate creating screenshots for documentation, but I have an issue with getting the right resolution. Below are two screenshots of the same playwright-cli open --headed https://en.wikipedia.org/wiki/Main_Page session:

Created with macOS screenshot shortcut:

Image

Created with playwright-cli screenshot e362:

Image

As you can see the macOS screenshot is higher quality, in fact it is double the resolution.

I find no way of controlling the resolution of screenshots with playwright-cli, is it possible?

Activity

  1. markusjohnsson commented on Apr 30, 2026

    @markusjohnsson
    Author

    I've tried using a config file with deviceScaleFactor:

    {
        "browser": {
            "contextOptions": {
                "viewport": {
                    "width": 1280,
                    "height": 720
                },
                "deviceScaleFactor": 2
            }
        }
    }

    But I still get low res screenshots.

    I'd like to make the equivalent call of screenshot({ "scale": "device" }). Can I write my own script that connects the the playwright-cli browser?

  2. markusjohnsson commented on Apr 30, 2026

    @markusjohnsson
    Author

    OK, so after my last comment I found the run-code command and I now have this, that actually produces a high res screenhot:

    playwright-cli run-code 'page => { 
        const fileName = `screenshot-${Date.now()}.png`;
        page.screenshot({ scale: "device", path: fileName, type: "png" });
        return fileName;
    }'

    BUT, I'd like to be able to use a <ref> to run for just one element, not the whole page, is that possible?

    EDIT:

    I have a working solution for now:

    #!/bin/sh
    
    playwright-cli run-code 'page => { 
        try {
            const fileName = `screenshot-${Date.now()}.png`;
            const e = page.locator(`aria-ref='$1'`);
            e.screenshot({ scale: "device", path: fileName, type: "png" });
            return fileName;
        } catch (error) {
            console.error("Error taking screenshot:", error);
            return null;
        }
    }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions