Set Window Size for Browser

To set specific window size (width x height) to the browser window using Selenium for Java, call driver.manage().window().setSize() method and pass the Dimension object as argument. The Dimension object can be initialized with required width and height.

The following is a simple code snippet to set the window size to a width of 1024 and a height of 768.

driver.manage().window().setSize(new Dimension(1024, 768));

setSize() method returns void.

Example

In the following program, we write Selenium Java code to set the window size to a width of 1024 and a height of 600.

Java Program

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyAppTest {
	public static void main(String[] args) {
		//initialize web driver
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();
		
		try {
			//get the URL
			driver.get("https://www.tutorialkart.com/");
			//set new dimension to browser window
			int width = 1024;
			int height = 600;
			driver.manage().window().setSize(new Dimension(width, height));
		} finally {
			driver.quit();
		}
	}
}

Screenshots

1. Screenshot before resize.

Set Window Size for Browser

2. Screenshot after resize.

Set Window Size for Browser