Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I'm using Selenium WebDriver for automation and I'm using Chromedriver.

I have noticed that when my driver runs and opens the chrome browser, it opens the browser with a strange size. I tried to fix it but in vain.

Does anybody know how can I change it?

1 Answer

0 votes
by (62.9k points)

Resize Browser Window in WebDriver

Java using Dimension

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.Dimension;

 

public class BrowserOperations {

    WebDriver driver;

 

    //this will open browser with default size

    public void launchBrowser() {

        driver = new FirefoxDriver();

    }

 

    public void resizeBrowser() {

        Dimension d = new Dimension(800,480);

 

//Resize current window to the set dimension

        driver.manage().window().setSize(d);

    }

}

Java using Chrome options

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.remote.DesiredCapabilities;

 

public class BrowserOperations {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver";,

        "/path/to/chromedriver");

 

        ChromeOptions options = new ChromeOptions();

        options.addArguments("window-size=800,480");

 

        DesiredCapabilities cap = DesiredCapabilities.chrome();

        cap.setCapability(ChromeOptions.CAPABILITY, options);

 

        //this will open chrome with set size

        WebDriver driver = new ChromeDriver(capabilities);

 

        driver.get("https://www.intellipaat.com/");

    }

}

If you want to maximize the browser window to the maximum width and height of the screen, you can just call the maximize() method

Webdriver driver = new FirefoxDriver();

driver.manage().window().maximize()

Browse Categories

...