Make Selenium Easy

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

To set the new size of the browser

Complete Code

Output

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

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

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

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

How To Set The Size Of Browser Window In Selenium?

by Amod Mahajan time to read: 2 min
1
%d bloggers like this: