Not able to maximize Chrome Window in headless mode

前端 未结 6 782
谎友^
谎友^ 2020-12-03 14:26

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 bro

相关标签:
6条回答
  • 2020-12-03 14:31

    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));
    
    0 讨论(0)
  • 2020-12-03 14:38

    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

    0 讨论(0)
  • 2020-12-03 14:41

    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?

    0 讨论(0)
  • 2020-12-03 14:44

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

    0 讨论(0)
  • 2020-12-03 14:53

    Add below ChromeOption in your code :

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

    Also refer this blog for more

    0 讨论(0)
  • 2020-12-03 14:54

    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 --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 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:


    tl; dr

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

    0 讨论(0)
提交回复
热议问题