User Agents

Web browsers and everyday web tech, explained

Set Browser Window Size for Selenium Screenshots

· 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

Setting the browser window size in Selenium is essential for capturing consistent, correctly proportioned screenshots. By defining a fixed viewport, you ensure that your screenshots look the same across different machines and screen resolutions, which is critical for visual regression testing, documentation, or debugging. This guide covers the most reliable methods for controlling window size in Selenium WebDriver, including the newer CDP (Chrome DevTools Protocol) approach for headless browsers.

What You'll Need

  • A working Selenium WebDriver setup (Java, Python, JavaScript, C#, etc.) with the appropriate browser driver (e.g., ChromeDriver, GeckoDriver).
  • A basic understanding of your chosen programming language and Selenium's API.
  • Access to a browser that Selenium supports (Chrome, Firefox, Edge, Safari).

Step-by-Step

  1. Create a WebDriver instance. Start by launching the browser with your preferred driver. For example, in Python: driver = webdriver.Chrome(). In Java: WebDriver driver = new ChromeDriver();.
  2. Set the window size using the set_window_size() method (Python) or manage().window().setSize() (Java/C#). This method takes width and height in pixels. For example, to set a common 1920x1080 resolution: driver.set_window_size(1920, 1080) in Python, or driver.manage().window().setSize(new Dimension(1920, 1080)) in Java.
  3. Alternatively, use the window.maximize() method if you want the browser to fill the entire screen. This is useful for capturing full-page screenshots but may vary across systems. In Python: driver.maximize_window(). In Java: driver.manage().window().maximize().
  4. For headless browsers (Chrome/Firefox in headless mode), set the window size via browser options. In Chrome, use --window-size=1920,1080 as a command-line argument. Example in Python: options = webdriver.ChromeOptions(); options.add_argument("--window-size=1920,1080"); driver = webdriver.Chrome(options=options). In Java: options.addArguments("--window-size=1920,1080").
  5. Use the Chrome DevTools Protocol (CDP) for more precise control, especially for responsive testing. Selenium 4 supports CDP commands. For example, in Python: driver.execute_cdp_cmd("Emulation.setDeviceMetricsOverride", {"width": 375, "height": 812, "deviceScaleFactor": 0, "mobile": True}). This simulates a mobile viewport.
  6. Take the screenshot after setting the size. Use driver.save_screenshot("screenshot.png") in Python, or File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE) in Java, then copy the file to your desired location.

Common Mistakes

  • Setting size before navigating to a page: Some websites dynamically resize the viewport based on content. Set the size after loading the page to ensure the screenshot captures the intended layout.
  • Ignoring the browser's default zoom level: If the browser has a custom zoom, the screenshot may be cropped. Reset zoom to 100% before setting size.
  • Using maximize() when a specific size is required: Maximize only fills the physical screen; it doesn't guarantee a consistent pixel size across machines.
  • Forgetting to handle scrollbars: Some browsers show scrollbars that affect the viewport width. Consider setting the window size to account for scrollbar dimensions, or use CDP to hide them.

Tips

  • Use a standard resolution like 1366x768 or 1920x1080 to match common user screens and avoid surprises in visual regression.
  • For full-page screenshots, consider using a library like Selenium's full_page_screenshot (in Python) or the CDP's Page.captureScreenshot with captureBeyondViewport: true. This captures the entire scrollable page, not just the visible area.
  • When testing responsive design, use CDP to emulate different devices rather than physically resizing the window, which can be flaky.
  • Always close the driver after taking screenshots to free resources: driver.quit().
  • If you need consistent screenshots across CI/CD, set the window size in your test setup, not in each test. Use a base class or a fixture.

Troubleshooting

If the window size does not apply as expected, try the following:

  • Check the browser driver version: Outdated drivers may not support the set_window_size method properly. Update ChromeDriver, GeckoDriver, etc.
  • Verify the size after setting it: Use driver.get_window_size() (Python) or driver.manage().window().getSize() (Java) to confirm the actual dimensions. Some browsers ignore the request if the size exceeds the screen's maximum.
  • For headless mode, ensure the window-size argument is passed correctly: Sometimes the argument is overwritten by a default. Try setting both the option and calling set_window_size after navigating.
  • If screenshots are still cropped, check if the page has a fixed-width layout: Use CDP to set the device metrics override, which forces the viewport regardless of the browser chrome.
  • On macOS, Safari may have restrictions: Use the safaridriver and ensure you enable 'Allow Remote Automation' in Safari's Develop menu.

Frequently Asked Questions

Can I set the window size for a specific test only?

Yes, you can call set_window_size at the beginning of that test. It will persist until you change it or close the browser.

Does the window size affect the screenshot resolution?

Yes, the screenshot captures the viewport area. A larger window size gives a higher-resolution image, but the file size may increase.

How do I set the window size in headless mode?

Use the --window-size command-line argument when creating the browser options. For example, in Chrome: options.add_argument("--window-size=1920,1080").

Why does my screenshot have a different size than the window I set?

This can happen if the page has a minimum width or if the browser adds scrollbars. Also, check if you are using a retina display; the screenshot might be in device pixels, not CSS pixels.

Can I set the window size after navigating to a page?

Yes, you can set it at any time. However, some pages may reflow or reload, so it's best to set it before interacting with elements.

Is there a way to set the window size for all tests in a suite?

Yes, use a setup method (e.g., @Before in JUnit, setUp in unittest) to set the size once before each test starts.