Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Set The Size Of Browser Window In Selenium?

Posted on 12/09/2024 By admin

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.

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.

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);
Dimension newDimension = new Dimension(800, 600);
driver.manage().window().setSize(newDimension);
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);
        }
}
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

Uncategorized

Post navigation

Previous Post: oauth2.o flow
Next Post: API Testing – Page 7

Related Posts

Launch of Edge and Internet Explorer browser in Selenium 3 Uncategorized
Interview Experience at Capgemini Bangalore for Selenium Java Profile ( Aug – 2019) Uncategorized
api tutorials Uncategorized
GroupsTest – Make Selenium Easy Uncategorized
December 30, 2018 – Make Selenium Easy Uncategorized
Java Program 13: Java Program To Find If Given Numbers Are Coprime Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark