无法在无头模式下最大化Chrome窗口

Pin*_*aki 15 selenium google-chrome geb selenium-chromedriver

我最近将我的chrome版本升级到60并将chromedriver升级到版本2.31.发布当我尝试最大化浏览器窗口时,我已经开始获得以下异常.

    driver.driver.manage().window().maximize()
Run Code Online (Sandbox Code Playgroud)

org.openqa.selenium.WebDriverException:未知错误:无法将窗口状态更改为最大化,当前状态正常(会话信息:chrome = 60.0.3112.78)(驱动程序信息:chromedriver = 2.31.488763(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform = Linux 4.2 .0-27-generic x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:108毫秒构建信息:版本:'2.53.1',修订版:'a36b8b1cd5757287168e54b817830adce9b0158d',时间:'2016-06 -30 19:26:09'系统信息:主机:'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'会话ID:c7de7149dd490cc7760d2f4fc49f0325驱动信息: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)},networkCo nnectionEnabled = false,unexpectedAlertBehaviour =,rotate = 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}]

我在Geb上使用ChromeDriver在无头模式下运行我的测试.

  • Chrome版本 - 60.0.3112.78
  • chromedriver版本 - 2.31.488763
  • 操作系统 - Ubuntu 14.04.4 LTS
  • Selenium版本 - 2.53.1
  • WebDriver语言绑定
  • Geb - 0.13.1

dem*_*123 14

由于您在无头模式下运行测试,因此没有active可用的浏览器窗口.就这样你

   driver.driver.manage().window().maximize()
Run Code Online (Sandbox Code Playgroud)

在这种情况下总会失败,因为驱动程序不知道哪个窗口要最大化,因为没有任何可用的窗口.

您可以按照@DebanjanB提到的内容进行操作,也可以使用特定屏幕大小(如1440x900等)启动无头浏览器,执行此类操作

 driver.manage().window().setSize(new Dimension(1440, 900));
Run Code Online (Sandbox Code Playgroud)


Nar*_*raR 7

ChromeOption在代码中添加以下内容:

options.addArguments("--window-size=1325x744");
Run Code Online (Sandbox Code Playgroud)

另请参阅此博客以获取更多信息


Deb*_*anB 4

代码行似乎存在细微差异:

driver.driver.manage().window().maximize()
Run Code Online (Sandbox Code Playgroud)

您需要将这行代码替换为:

driver.manage().window().maximize()
Run Code Online (Sandbox Code Playgroud)

如果此解决方案无法解决您的问题,要使用Google Chromeheadless您可以使用以下任一解决方案:


使用start-maximized

建议通过类来最大化Google ChromeChromeOptions浏览器,如下:

  • 代码块:

    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();
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

使用-start-maximized.png


使用--window-size=1400,600

作为替代方案,您还可以添加预期的参数window size如下所示:

  • 代码块:

    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();
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

使用窗口大小.png


使用setSize(new Dimension(1440, 900))

  • 代码块:

    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();
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

使用-setSize.png


TL; 博士

您可以找到基于 Selenium 客户端的有关Selenium Firefox headless最大化窗口的讨论返回不同的结果