Maximizing Chrome Window in Selenium Webdriver using Java

Jash Unadkat
2 min readSep 3, 2020

--

When browser windows are maximized, it reduces the chances of Selenium scripts missing out on web elements they must interact with during automated tests. It is possible that certain elements may not be visible to or recognized by Selenium if the browser window is not in a maximized state.

Maximizing a browser window at first also provides better visibility to the QAs for the test cases being executed. Thus QAs must consider maximizing the browser window as a best practice.

As Chrome is the most widely used browser, this article will explain how to maximize the chrome window in selenium webdriver using java.

Use the maximize() method from WebDriver.Window Interface

The code snippet below implements four basic scenarios:

  1. Launching the Chrome browser
  2. Navigating to the desired URL
  3. Maximizing the Chrome Window
  4. Terminating the browser

Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Max {public static void main(String args[]) throws InterruptedException
{
System.setProperty("<Path of the ChromeDriver>");
WebDriver driver = new ChromeDriver();
// Navigate to a website
driver.get("https://www.google.com/");
//Mazimize current window
driver.manage().window().maximize();
//Delay execution for 5 seconds to view the maximize operation
Thread.sleep(5000);
//Close the browser
driver.quit();
}
}

Successful execution of the selenium script above will do the following: launch the Chrome browser, navigate to Google website, maximize the Chrome Window, and wait for five seconds.

An alternate method that can be used for maximizing the Chrome window is to use the ChromeOptions class. This method informs the Chrome browser explicitly to launch in maximized mode.

ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");

WebDriver driver = new ChromeDriver(options);

In this second method, the browser is, by default launched in maximized mode.

--

--

Jash Unadkat
Jash Unadkat

Written by Jash Unadkat

As a tech geek, I love writing articles about everything related to web development or software testing space.

No responses yet