How do I set browser width and height in Selenium WebDriver?


We can set browser width and height in Selenium webdriver. There are multiple methods available to achieve this. Whenever an application is launched, it opens in its default browser size.

We can resize the browser with the help of the Dimension class in Java. We create an object of the Dimension class and pass the desired width and height of the browser as parameters to that class. Finally we pass the object of the Dimension class as an argument to the setSize method.

Syntax

Dimension dem = new Dimension(750,450);
driver.manage().window().setSize(dem);

We can also set the browser width and height with the help of Chrome options. We have to create an object of the ChromeOptions class and apply addArguments to it. The parameter window-size set with values of height and width of the browser are passed as arguments to the method. This knowledge is passed to the browser invocation method with the help of DesiredCapabilities class.

Syntax

ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=750,450");

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);

Example

Code Implementation with Dimension class.

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserResize{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = " https://www.tutorialspoint.com/tutor_connect/index.php";
      driver.get(url);
      // Dimension class with browser width and height value passed
      Dimension dem = new Dimension(750,450);
      // passing the Dimension object as an argument to setSize method
      driver.manage().window().setSize(dem);

Implementation with Chrome Options.

import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserResizeOptions{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      // ChromeOptions class with addArguments method
      ChromeOptions options = new ChromeOptions();
      options.addArguments("window-size=750,450");
      DesiredCapabilities capabilities = DesiredCapabilities.chrome();
      capabilities.setCapability(ChromeOptions.CAPABILITY, options);
      WebDriver driver = new ChromeDriver(capabilities);
      String url = "https://www.tutorialspoint.com/tutor_connect/index.php";
      driver.get(url);
   }
}

Updated on: 26-Oct-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements