How to Handle Browser Windows and Cookies In Selenium 4?

Reading Time: 3 minutes

Hello People, this article explains How to Handle and resize browser windows and cookies using the Selenium Webdriver. Resizing the browser window and handling cookies is important as it makes testing much easier. We can resize the browser window in Selenium Webdriver which actually helps in searching for the elements easier. In addition to this, this article also explains how to handle cookies like creating, adding cookies, or deleting cookies. Moreover, once you visit any website, cookies are sent to your machine by the website. Since these cookies contain information that helps the website keep track of your visits or activity. 

Use of Selenium other than Software Testing | Netwoven
  • Whenever WebDriver starts the browser, it creates it with default settings. For instance, resizing the browser window, mainly when we are testing for responsive websites because we need to check to see how the different elements on the page render when we resize the browser window.
  • Maximizing the browser is crucial in the selenium framework while automating any web application. Meanwhile executing the selenium framework or any script, the browser may not be in the full-screen state. Thus we need to maximize the browser so that all the elements of the web application are visible.
  • There are several ways to maximize or minimize the browser. Selenium Webdriver does not provide any direct method for minimizing the browser. For minimizing the browser in Selenium Webdriver, we need to use the resize method.

Methods to Resize Browser Window

Void setSize() – This will set the size of the current browser.

Void maximize() – This will maximize the current browser.

Dimension getSize() – This method returns the dimension of the browser in height and width.

Point setPosition() – This will set the position of the current browser.

How to Resize a Browser Window in Selenium Webdriver:

Below, the Selenium script depicts how to resize a browser. Therefore, the script will follow the following steps:

  • Opens the Google Chrome browser.
  • Navigate to the given site URL.
  • Resize the browser window.
  • Close the browser window.

Code Snippet:

public class ResizeBrowser {

public static void main(String args[]) 



WebDriver driver = new ChromeDriver();
      Dimension d = new Dimension(200,1070);

      //Resize current window to set dimension

       //Close the browser




How to handle Cookies in Selenium Webdriver

Working with Cookies in Selenium Automated Tests - TestProject

A cookie is a small piece of data sent from a website and gets stored on the user’s computer. Certainly, Cookies are also used to recognize the user if they return to a website, and load the previously held information. Mainly, cookies store the user’s identity and track the user’s journey through the pages of the website.

Moreover, some of the commands used in Selenium for deleting, adding, and creating the list of cookies are listed below:


// Returns the List of all Cookies


//Returns the specific cookie according 

to name


//Creates and adds the cookie


// Deletes the specific cookie


// Deletes the specific cookie 

according to the Name


// Deletes all the cookies

How To Pass Cookies From Selenium WebDriver To Rest-Assured

Code Snippet:

import org.openqa.selenium.*;


import java.util.Set;

public class getAllCookies {
public static void main(String[] args) {
 WebDriver driver = new ChromeDriver();
try {
// Add few cookies
driver.manage().addCookie(new Cookie("test1", 

driver.manage().addCookie(new Cookie("test2", 


// Get All available cookies
 Set<Cookie> cookies = 


// delete a cookie with name 'test1'

// deletes all cookies

        } finally {


To sum up, the article, resizing the browser window helps in the proper execution of test cases. Most importantly it provides better visibility to the QA. Secondly, Resizing ensures that no element is left unidentified. In addition to this, it also explains different syntaxes to handle cookies.

For more Reference Visit: