This guide shows you how to capture high-resolution (Retina) screenshots using Selenium WebDriver with Chrome. By default, Chrome screenshots match the browser’s viewport size, which is often lower than the device’s physical pixel density. This technique forces a higher device scale factor so the screenshot contains more pixels, ideal for crisp images on modern displays.
What You’ll Need
- Python 3.7+ (or Java, but examples are in Python)
- Selenium WebDriver installed (
pip install selenium) - Chrome browser and matching ChromeDriver
- Basic knowledge of Selenium commands
Step-by-Step
- Set up the Chrome options. Create a
ChromeOptionsinstance and add the argument--force-device-scale-factor=2. This tells Chrome to render the page at 200% zoom, effectively doubling the pixel density. You can adjust the value (e.g., 1.5, 3) to get different scale factors. - Launch Chrome with those options. Pass the options to
webdriver.Chrome(). Make sure ChromeDriver is in your PATH or provide the executable path. - Navigate to the target page. Use
driver.get("https://example.com")and wait for the page to load completely. You may need explicit waits for dynamic content. - Set the window size. To capture the full page, you might need to set the window size to the page’s scroll dimensions. For a viewport screenshot, just set a standard size like 1920×1080.
- Take the screenshot. Use
driver.save_screenshot("screenshot.png")ordriver.get_screenshot_as_file(). The saved image will have dimensions equal to the viewport size multiplied by the scale factor (e.g., 1920×1080 becomes 3840×2160). - Close the driver. Always call
driver.quit()to free resources.
Common Mistakes
- Forgetting the scale factor argument – without it, you get standard resolution screenshots.
- Using headless mode incorrectly – headless Chrome sometimes ignores the scale factor unless you also set
--window-sizeto the desired output size. - Not waiting for elements – screenshots taken before the page finishes loading may be blank or incomplete.
- Assuming the screenshot is full-page – by default, Selenium captures only the visible viewport. For full-page, you need additional steps (see Tips).
Tips
- For full-page screenshots, use the
--headlessmode with a large window size set to the page’s full scroll height. You can get the height via JavaScript:driver.execute_script("return document.body.scrollHeight"). - Adjust the scale factor dynamically – you can set it to 2 for Retina, but for very large pages, a factor of 1.5 may be a good compromise between quality and file size.
- Use a consistent viewport size – set the window size before taking the screenshot to ensure predictable output dimensions.
- For critical screenshots, consider using
driver.get_screenshot_as_png()to get binary data that you can process or upload directly.
Troubleshooting
Screenshot is still low resolution: Verify that the --force-device-scale-factor argument is correctly spelled and passed to ChromeOptions. Also, check that ChromeDriver is up to date with your Chrome version.
Screenshot is blank or black: This often happens in headless mode. Try adding --headless=new (for Chrome 112+) or set --window-size to a large value. Also ensure the page has fully loaded.
Scale factor not working in headless: In older headless modes, you might need to use --force-device-scale-factor=2 together with --window-size=1920,1080. In newer Chrome, use --headless=new.
ChromeDriver not found: Download the correct ChromeDriver from the official site and add it to your PATH, or specify the path in the webdriver.Chrome() call.
Frequently Asked Questions
Do I need a Retina display to get Retina screenshots?
No. The scale factor forces Chrome to render at a higher pixel density regardless of your physical screen. The screenshot will have more pixels, but it may look smaller when viewed at 100% zoom on a non-Retina monitor.
Can I use this with Firefox or Edge?
Yes, but the argument differs. For Firefox, use layout.css.devPixelsPerPx preference. For Edge, it’s Chromium-based, so the same Chrome argument works.
How do I take a full-page screenshot in Retina?
Set the window size to the full page dimensions (using JavaScript) and then take the screenshot. In headless mode, you can set the window size to the scroll height directly.
Will the screenshot file be very large?
Yes, a 2x scale factor quadruples the pixel count, so file size increases significantly. Use PNG format, which compresses well for screenshots, or consider JPEG if size is a concern.
Can I set the scale factor per screenshot?
No, the scale factor is set at browser launch. If you need different scales, you must restart the browser with different options.
Does this work with Selenium Grid?
Yes, as long as the node runs Chrome and the options are passed in the desired capabilities. The scale factor is a browser-level setting.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Hogwarts Legacy Microsoft Visual C Runtime Error Fix
- Set Up UPI Payments Through WhatsApp
- Pair Oppo Enco Earbuds with Android, iPhone & Windows
- Fix Administrator Account Disabled Error in Windows 11
- How to Play Kodi on Smart TV from PC
- Factory Reset on Samsung Galaxy: What It Erases