User Agents

Web browsers and everyday web tech, explained

Selenium Window Size: Set and Check

· 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 and checking the browser window size in Selenium allows precise control over how web pages render during automated testing. This is essential for responsive design validation, screenshot consistency, and simulating real user devices. Selenium provides direct methods to configure the browser viewport dimensions programmatically, ensuring reliable and repeatable test environments across different screen sizes.

What You'll Need

  • Selenium WebDriver installed for your preferred language (Python, Java, JavaScript, etc.)
  • A compatible browser driver (ChromeDriver, GeckoDriver for Firefox, etc.)
  • Basic knowledge of your chosen programming language
  • A running development environment with access to your test scripts

Step-by-Step

  1. Initialize the WebDriver instance for your browser (e.g., Chrome or Firefox) using the appropriate driver executable. For example, in Python, import webdriver from selenium and create a Chrome() or Firefox() object.
  2. Use the set_window_size(width, height) method on the driver instance to define the desired viewport dimensions. Pass integer values for width and height in pixels, such as driver.set_window_size(1920, 1080) for a full HD display.
  3. Alternatively, use set_window_rect(rect) to set both size and position simultaneously. The rect parameter is a dictionary or object containing width, height, x, and y values. For size only, set x and y to 0.
  4. To maximize the window instead of setting specific dimensions, call driver.maximize_window(). This sets the browser to occupy the entire screen, useful for full-page testing.
  5. To check the current window size, call driver.get_window_size(). This returns a dictionary or map with 'width' and 'height' keys. Store the result in a variable and inspect the values.
  6. Verify the actual size matches expectations by comparing the returned values against the intended dimensions. For example, print or log the result to confirm accuracy.
  7. If running headless (without a visible browser), ensure the browser supports viewport sizing in headless mode. Some configurations require explicit arguments like --window-size=1920,1080 when launching the driver.
  8. For mobile emulation, use browser-specific options (e.g., ChromeOptions) to define device metrics. This approach simulates mobile viewports more accurately than simple resizing.
  9. Close the browser session using driver.quit() after completing tests to free system resources and avoid orphaned processes.
  10. Repeat these steps across multiple test cases with varying sizes to validate responsive layouts under different conditions.

Common Mistakes

  • Calling set_window_size() before initializing the driver, which causes a WebDriverException.
  • Assuming maximize_window() sets a fixed resolution—actual size depends on the host machine’s screen.
  • Not accounting for operating system UI elements (taskbars, menus) that reduce available viewport space.
  • Using incorrect data types (e.g., strings instead of integers) in size parameters, leading to type errors.
  • Overlooking headless mode limitations where window size may default to a small resolution unless explicitly set.
  • Setting window size after navigating to a page, which can cause inconsistent rendering due to initial layout calculations.

Tips

  • Set the window size immediately after driver initialization and before navigating to any URL for consistent results.
  • Use standardized viewport sizes (e.g., 375x667 for iPhone, 1920x1080 for desktop) to match common devices.
  • Combine window sizing with implicit or explicit waits to allow responsive layouts to adjust after resizing.
  • Log window dimensions at the start of each test to aid debugging when layout issues occur.
  • Use get_window_rect() instead of get_window_size() if you also need position data.
  • Test across multiple browsers, as rendering engines may interpret viewport sizes slightly differently.

Troubleshooting

If the window size does not apply correctly, first confirm the driver is properly initialized. Check that the browser window opens at all—failure here often indicates a missing or incompatible driver. If the window opens but size settings are ignored, ensure the command is called before page navigation. In headless mode, verify that the browser supports custom viewport sizes; Chrome requires the --window-size argument passed via browser options. If get_window_size() returns unexpected values, consider system scaling settings (e.g., 125% DPI scaling on Windows), which can affect reported dimensions. Adjust for scaling by dividing the returned size by the scale factor if necessary. If using remote WebDriver (e.g., Selenium Grid), ensure the remote node has sufficient screen resolution to accommodate the requested size. Restart the driver if persistent issues occur, as stale sessions can interfere with window commands.

Frequently Asked Questions

Can I set window size in headless mode?

Yes, but you must explicitly set it via set_window_size() or pass --window-size in browser arguments, as headless defaults vary.

Why does get_window_size() return different values than I set?

Operating system UI elements, scaling settings (DPI), or window borders may affect reported dimensions. The actual content area may differ.

Does maximize_window() give consistent results?

No—it depends on the machine’s screen resolution. Avoid it for cross-machine consistency; use set_window_size() instead.

How do I simulate mobile devices?

Use browser-specific options like Chrome’s deviceName or manually set viewport size and user agent to mimic mobile behavior.

Can I resize the window during a test?

Yes, call set_window_size() at any point, but be aware that layout changes may require rechecking element positions.

Is window size the same as viewport size?

Not exactly. Window size includes browser UI; viewport is the content area. For responsive testing, window size controls the effective viewport.