-3

I am trying to launch Selenium nodes (Chrome browser) as Windows on the Amazon Windows AMI for running tests, but the browser size it not getting maximized.

I tried all possible solutions provided and it did not work.

How can I set the browser window size when using `google-chrome --headless`?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Smore
  • 1
  • 1
  • 2
  • Please post what you've actually tried so we don't have to guess. A lot of people post that they've tried all possible solutions but if that were really true, they wouldn't be here asking for a solution. Thanks. – JeffC Jan 25 '18 at 21:34

2 Answers2

3

Instead of aDriver.manage().window().setSize(new Dimension(width, height)); try it with aDriver.manage().window().setRect({ width: 400, height: 700, x: 0, y: 0 });.

This should work for you.

public static void setMaximize(boolean maximize) {
if (maximize) {
    if (getHeadless()) {
        aDriver.manage().window().setRect({ width: 1600, height: 900, x: 0, y: 0 });
    } else if (getDevice() == null) {
        try {
            aDriver.manage().window().maximize();
        } catch (WebDriverException e) {
            String name = getDriver();
            if (name == null) {
                System.out.println("Browser has not been set");
            } else {
                System.out.println(name + " does not support maximize");
            }
        }
    }
}
Jozott
  • 2,367
  • 2
  • 14
  • 28
0

Use the built-in setSize() in Selenium:

    aDriver.manage().window().setSize(new Dimension(width, height));

It works like a champ. I've used it for Firefox, Chrome (even headless), and Edge.

Adding full maximize code

public static void setMaximize(boolean maximize) {
    if (maximize) {
        if (getHeadless()) {
            aDriver.manage().window().setSize(new Dimension(1600, 900));
        } else if (getDevice() == null) {
            try {
                aDriver.manage().window().maximize();
            } catch (WebDriverException e) {
                String name = getDriver();
                if (name == null) {
                    System.out.println("Browser has not been set");
                } else {
                    System.out.println(name + " does not support maximize");
                }
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MivaScott
  • 1,763
  • 1
  • 12
  • 30
  • Hi Scott I did tried it but does not work.WebDriver not able to change state of window size after it launches. It just keep default size. – Smore Jan 25 '18 at 21:01
  • @Smore, I added the whole maximize method I use (condensed). I pass in either a true or false if I want the test to run small or full sized. If it's supposed to be maximize, but getHeadless is true, then it forces dimensions. – MivaScott Jan 25 '18 at 22:27
  • -1. It does not work on Chrome MacOs. ```TypeError: driver.manage(...).window(...).setSize is not a function at testInitialize (common/common.js:11:27) at at process._tickCallback (internal/process/next_tick.js:188:7)``` – Lewis Aug 01 '18 at 08:51
  • It works for me. The only thing is setSize takes width and height, not Dimension - `setSize(width, height)` – Ondra Aug 07 '19 at 09:56
  • 1
    @Lewis, per the API, setSize() should be there - https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Window.html#setSize-org.openqa.selenium.Dimension - Although looking at your error you should probably note the class description, _"...synonymous to window.resizeTo() in JS"_ – MivaScott Aug 07 '19 at 16:20
  • What programming language is used here? Java? C#? – Peter Mortensen Nov 22 '20 at 01:37
  • @PeterMortensen, Java – MivaScott Nov 22 '20 at 06:27