Not able to maximize Chrome Window in headless mode
Asked Answered
M

7

22

I recently upgraded my chrome version to 60 and chromedriver to version 2.31. Post that I have started getting the following exception when I try to do a maximize of the browser window.

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

org.openqa.selenium.WebDriverException: unknown error: failed to change window state to maximized, current state is normal (Session info: chrome=60.0.3112.78) (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.2.0-27-generic x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 108 milliseconds Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09' System info: host: 'bb-blr-prod-stage-stg1-01', ip: '10.3.211.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.2.0-27-generic', java.version: '1.7.0_80' Session ID: c7de7149dd490cc7760d2f4fc49f0325 Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=/tmp/.org.chromium.Chromium.WABPhO, chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8)}, networkConnectionEnabled=false, unexpectedAlertBehaviour=, rotatable=false, setWindowRect=true, locationContextEnabled=true, mobileEmulationEnabled=false, pageLoadStrategy=normal, version=60.0.3112.78, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, hasTouchScreen=false, applicationCacheEnabled=false, takesScreenshot=true}]

I run my tests in headless mode using ChromeDriver on Geb.

  • Chrome version - 60.0.3112.78
  • chromedriver version - 2.31.488763
  • OS - Ubuntu 14.04.4 LTS
  • Selenium version - 2.53.1
  • WebDriver Language Bindings
  • Geb - 0.13.1
Mauser answered 28/7, 2017 at 13:7 Comment(0)
V
19

Since you're running tests in a headless mode, there is no active browser window available. As such your

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

would always fail in such situations because the driver doesn't know which window to maximize since there aren't any available.

You can either follow what @DebanjanB has mentioned or you can start the headless browser with a specific screen size like 1440x900 etc, doing something like this

 driver.manage().window().setSize(new Dimension(1440, 900));

[Edit] In most cases now that I've seen that the maximize() method works in headless too - however I've not tested this on a CI system.

Vinegary answered 28/7, 2017 at 13:28 Comment(0)
I
9

Add below ChromeOption in your code :

options.addArguments("--window-size=1325x744");

Also refer this blog for more

Indulgent answered 29/12, 2017 at 7:9 Comment(1)
Confirmed working in Selenium 3.8.1 and ChromeDriver 2.34 in headless mode. As a workaround, I put in a config for the local machine's native resolution so as not to hardcode a specific value.Simonides
P
4

There seems to be a minor discrepancy in the line of code:

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

You need to replace this line of code with:

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

In case this solution doesn't address your issue, to use Google Chrome in headless you can use either of the following solutions:


Using start-maximized

It is recommended to maximize the Google Chrome browser through ChromeOptions class as follows:

  • Code Block:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("--headless");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.google.com/");
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File(".\\Screenshots\\using-start-maximized.png"));
    driver.quit();
    
  • Browser Snapshot:

using-start-maximized.png


Using --window-size=1400,600

As an alternative you can also add the argument for the expected window size as follows:

  • Code Block:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--window-size=1400,600");
    options.addArguments("--headless");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.google.com/");
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File(".\\Screenshots\\using-window-size.png"));
    driver.quit();
    
  • Browser Snapshot:

using-window-size.png


Using setSize(new Dimension(1440, 900))

  • Code Block:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.google.com/");
    driver.manage().window().setSize(new Dimension(1440, 900));
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File(".\\Screenshots\\using-setSize.png"));
    driver.quit();
    
  • Browser Snapshot:

using-setSize.png


tl; dr

You can find Selenium client based discussion on maximizing window in Selenium Firefox headless returns different results

Pasadena answered 28/7, 2017 at 13:22 Comment(0)
U
4

This is an open bug (follow here) : https://bugs.chromium.org/p/chromedriver/issues/detail?id=1901

Unpretentious answered 8/8, 2017 at 9:58 Comment(0)
B
2

I'm using chromedriver 2.30 & chrome browser v60 through protractor. I run the tests headless too albeit I don't do it via chromeoptions. Rather I run tests headless using xvfb-run on a unix distribution. I'm encountering this issue also albeit it fails randomly for me. See stack below

[chrome #11]       [31mWebDriverError: unknown error: failed to change window state to maximized, current state is normal
[chrome #11]         (Session info: chrome=60.0.3112.78)
[chrome #11]         (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 3.10.0-514.26.2.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
[chrome #11]       Command duration or timeout: 122 milliseconds
[chrome #11]       Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
[chrome #11]       System info: host: 's1wfadvcilvm08', ip: '172.16.184.183', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-514.26.2.el7.x86_64', java.version: '1.8.0_141'
[chrome #11]       Driver info: org.openqa.selenium.chrome.ChromeDriver
[chrome #11]       Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57), userDataDir=/tmp/.org.chromium.Chromium.BNsN1w}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.78, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]

My code at the beginning of each test does the following

browser.manage().window().maximize();

changing to

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

doesn't work for me either unfortunately. Shouldn't browser.manage().window().maximize() be still working as I'm running headless using xvfb-run rather than doing headless via chrome options?

Brigandine answered 28/7, 2017 at 18:7 Comment(0)
B
0

This bug was initially fixed in ChromeDriver 2.42, and was actual for macOS until 2.44 (check changelogs: http://chromedriver.chromium.org/downloads).
So there is a solution for everyone who faces this issue: update your driver version

Barden answered 21/11, 2019 at 13:43 Comment(0)
L
0

This is known issue on Ubuntu 20.04 LTS for headless Google Chrome. I just used Java's awt pakage to get maximum resolution of whatevevr screen i.e. xvbf for Ubuntu LTS and current screen for Windows 10 and set it in arggument of ChromeOption to configure Webdriver

java.awt.Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();

chromeOptions.addArguments("--window-size=" + (int) screenDimension.getWidth() + "," + (int) screenDimension.getHeight());

Liguria answered 20/5, 2022 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.