Not able to maximize Chrome Window in headless mode

Pinaki picture Pinaki · Jul 28, 2017 · Viewed 18.4k times · Source

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

Answer

demouser123 picture demouser123 · Jul 28, 2017

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));