How to Set Browser Window Size in Selenium WebDriver
In Selenium WebDriver, you can set the browser window size using the
driver.manage().window().setSize(new Dimension(width, height)) method. This lets you specify the exact width and height in pixels for the browser window.Syntax
The syntax to set the browser window size in Selenium WebDriver involves creating a Dimension object with the desired width and height, then passing it to the setSize() method of the browser window.
driver: Your WebDriver instance controlling the browser.manage(): Accesses browser management options.window(): Accesses the browser window controls.setSize(Dimension size): Sets the window size to the specified dimensions.Dimension(width, height): Represents the size in pixels.
java
driver.manage().window().setSize(new Dimension(1024, 768));
Example
This example shows how to open a Chrome browser using Selenium WebDriver, set the window size to 1024x768 pixels, and then close the browser.
java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.Dimension; public class SetWindowSizeExample { public static void main(String[] args) { // Set path to chromedriver executable if needed // System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // Set browser window size to 1024x768 driver.manage().window().setSize(new Dimension(1024, 768)); // Open a website driver.get("https://www.example.com"); // Pause to see the window size effect (optional) try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } // Close the browser driver.quit(); } }
Output
Browser window opens at 1024x768 size, loads https://www.example.com, then closes after 3 seconds.
Common Pitfalls
Common mistakes when setting browser window size include:
- Not importing the
Dimensionclass, causing compilation errors. - Setting the size before initializing the WebDriver instance.
- Confusing
setSize()withmaximize()which maximizes the window instead of setting a custom size. - Using incorrect units; Selenium expects pixels, not percentages or other units.
Always ensure the WebDriver is initialized before setting the window size.
java
/* Wrong way: setting size before driver initialization */ // driver.manage().window().setSize(new Dimension(800, 600)); // WebDriver driver = new ChromeDriver(); /* Right way: initialize driver first */ WebDriver driver = new ChromeDriver(); driver.manage().window().setSize(new Dimension(800, 600));
Quick Reference
| Method | Description |
|---|---|
| driver.manage().window().setSize(new Dimension(width, height)) | Sets the browser window size to specified width and height in pixels. |
| driver.manage().window().maximize() | Maximizes the browser window to full screen. |
| driver.manage().window().fullscreen() | Sets the browser window to full screen mode (like pressing F11). |
Key Takeaways
Always initialize the WebDriver before setting the window size.
Use the Dimension class to specify width and height in pixels.
setSize() sets exact window size; maximize() fills the screen.
Import org.openqa.selenium.Dimension to avoid errors.
Setting window size helps test responsive layouts reliably.