User Agents

Web browsers and everyday web tech, explained

Fix Selenium Chrome Screenshots at Double Resolution on Retina

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

If you've ever taken a screenshot with Selenium WebDriver on a Mac with a Retina display, you may have noticed the image comes out at twice the expected size—e.g., a 1280x800 viewport becomes 2560x1600. This happens because Chrome runs with device scale factor 2 on Retina displays, and Selenium's screenshot method captures the full backing store. This guide shows you how to force Chrome to use a scale factor of 1, so your screenshots match the viewport size exactly.

What You'll Need

  • Selenium WebDriver (Python, Java, or any language binding) already installed.
  • ChromeDriver matching your Chrome version.
  • Basic familiarity with your Selenium setup.

Step-by-Step

  1. Locate your ChromeDriver setup code. Find where you instantiate the ChromeDriver in your test script. This is usually something like WebDriver driver = new ChromeDriver(); (Java) or driver = webdriver.Chrome() (Python).
  2. Add the force-device-scale-factor argument. This Chrome command-line switch forces the device scale factor to 1, overriding the Retina default of 2. In Python, modify your Chrome options like this:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    opts = Options()
    opts.add_argument('force-device-scale-factor=1')
    driver = webdriver.Chrome(options=opts)
    In Java:
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--force-device-scale-factor=1");
    WebDriver driver = new ChromeDriver(options);
  3. Optionally, also set the window size explicitly. To ensure your viewport is exactly what you want, set the window size after creating the driver:
    driver.set_window_size(1280, 800)
    (Python) or driver.manage().window().setSize(new Dimension(1280, 800)); (Java).
  4. Take your screenshot as usual. Use driver.get_screenshot_as_file('screenshot.png') (Python) or File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); (Java). The resulting image should now be 1280x800, not 2560x1600.
  5. Verify the image dimensions. Open the screenshot and check the pixel dimensions. If it's still double, confirm the argument was applied by checking Chrome's chrome://version page—look for 'Command Line' and ensure 'force-device-scale-factor=1' appears.

Common Mistakes

  • Using the wrong argument syntax. Some examples use --force-device-scale-factor=1 with two dashes; both work, but ensure the equals sign is present and the value is 1 (not 2).
  • Setting the argument after the driver is created. The argument must be passed at driver creation time; you can't change it later.
  • Forgetting to update all test scripts. If you have multiple test files, each must include the argument. Consider creating a shared helper function.
  • Confusing viewport size with window size. The window size includes browser chrome (toolbar, tabs). Use set_window_size and then adjust for the viewport if needed, or use the --window-size argument.

Tips

  • Use a headless browser for consistent screenshots. When running headless, Chrome may still use a scale factor of 1 by default, but adding the argument ensures consistency across environments.
  • Combine with --high-dpi-support=1 if you need high-DPI support for other purposes. But for normal screenshots, leave it out.
  • If you need Retina-quality screenshots for design reviews, you can set force-device-scale-factor=2 deliberately to get double resolution.
  • Test your screenshots on a non-Retina monitor to confirm the fix doesn't break other environments.

Troubleshooting

Still getting double resolution? First, verify that the argument is actually being passed. In Python, you can print driver.capabilities to see the 'goog:chromeOptions' and confirm the args. If not, ensure you're using the correct variable name (e.g., opts vs options).

ChromeDriver not accepting the argument? Update ChromeDriver to the latest version. Older versions may ignore unknown switches.

Screenshots are blurry or scaled incorrectly after the fix? This could be a CSS zoom issue. Some websites use CSS media queries that change layout based on device pixel ratio. The screenshot will reflect the layout for scale factor 1, which may look different from what you see on screen. That's expected.

If you're using Selenium Grid or remote WebDriver, the argument must be set on the node, not the client. Pass it in the desired capabilities when creating the session.

Frequently Asked Questions

Why do my screenshots come out at double resolution only on Mac Retina displays?

Because Retina displays have a device pixel ratio of 2, Chrome renders at that scale. Selenium captures the full backing store, which is the physical pixel size.

Will this affect the layout of the page in screenshots?

Yes, because the device scale factor affects how CSS media queries like min-resolution are evaluated. The page may render as it would on a standard 1x display, which is usually the intended behavior for consistent screenshots.

Can I set this argument in a cross-platform way?

Yes, the force-device-scale-factor argument works on Windows, Linux, and macOS. It's not Mac-specific, but it's most needed on Retina displays.

Does this work with headless Chrome?

Yes, it works with headless Chrome. In fact, headless Chrome often defaults to scale factor 1, but adding the argument ensures consistency.

What if I want to keep the double resolution for some screenshots?

You can create two driver instances with different options, or you can change the argument value dynamically by restarting the driver. There's no way to change it on the fly.

Is there a way to fix this without changing code?

No, you must add the command-line argument to your WebDriver setup. There's no browser setting that overrides it.