How can I control Chromedriver open window size?


We can control the chromedriver to open with a window size in Selenium. This is done with the help of ChromeOptions class. We have to create an object of that and apply addArguments method on it.

Then pass window-size=x, y as a parameter to the method. The x and y are the dimensions of the window. Next, we have to apply this option to the Chrome browser with the DesiredCapabilities class. Finally, this information is sent to the driver object.

Syntax

ChromeOptions op = new ChromeOptions();
op.addArguments("window-size=500,250");
DesiredCapabilities c = DesiredCapabilities.chrome();
c.setCapability(ChromeOptions.CAPABILITY, op);
WebDriver d = new ChromeDriver(op);

Example

Code Implementation.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
public class ChromeWindowSize{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      //ChromeOptions object
      ChromeOptions op = new ChromeOptions();
      //window size set
      op.addArguments("window-size=500,250");
      //setting capability to browser
      DesiredCapabilities c = DesiredCapabilities.chrome();
      c.setCapability(ChromeOptions.CAPABILITY, op);
      WebDriver driver = new ChromeDriver(op);
      driver.get("https://www.tutorialspoint.com/index.htm");
      // get window size
      System.out.println
      ("Chrome browser size: " + driver.manage().window().getSize());
   }
}

Output

Browser window −

Updated on: 28-Dec-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements