How To Set The Size Of Browser Window In Selenium?

Introduction

When we launch the browser in Selenium generally maximize the browser. But sometimes we may need to explicitly set the size of the browser window as well.

If you run your scripts in headless mode or on the cloud then maximizing the window browser does not work sometimes and you may get NoSuchElementException. In this case, setting the browser window size explicitly will work.

To test the responsiveness of your application with different sizes of browser windows also requires setting the size of the browser window explicitly.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Dimension Class

Selenium provides a class named Dimension which can be used to get the current size of the browser window and set the new size of the browser window. Size means the height and width of the browser.

To get the current size of the browser

Dimension currentDimension = driver.manage().window().getSize();
int height = currentDimension.getHeight();
int width = currentDimension.getWidth();
System.out.println("Current height: "+ height);
System.out.println("Current width: "+width);

To set the new size of the browser

Dimension newDimension = new Dimension(800, 600);
driver.manage().window().setSize(newDimension);

Complete Code

package BrowserRelatedActions;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class SetSizeOfBrowser {

	public static void main(String[] args) {
		
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com/");
		
		// Default size
		Dimension currentDimension = driver.manage().window().getSize();
		int height = currentDimension.getHeight();
		int width = currentDimension.getWidth();
		System.out.println("Current height: "+ height);
		System.out.println("Current width: "+width);
		
		// Set new size
		Dimension newDimension = new Dimension(800, 600);
		driver.manage().window().setSize(newDimension);
		
		// Getting 
		Dimension newSetDimension = driver.manage().window().getSize();
		int newHeight = newSetDimension.getHeight();
		int newWidth = newSetDimension.getWidth();
		System.out.println("Current height: "+ newHeight);
		System.out.println("Current width: "+newWidth);
	}
}

Output

Current height: 708
Current width: 1050
Current height: 600
Current width: 800

Please note default size depends on your screen size.

Please subscribe to my YouTube channel Retarget Common to learn from my video tutorials.

Below are important end to end tutorials for Testers:-

Selenium Tutorials

Rest Assured Tutorials

Postman Tutorials

TestNG Tutorials

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

1 thought on “How To Set The Size Of Browser Window In Selenium?”

Leave a Reply

Your email address will not be published. Required fields are marked *