Headless chrome not working as advertised

2019-07-14 06:27发布

问题:

Having used this page as a reference: http://executeautomation.com/blog/running-chrome-in-headless-mode-with-selenium-c/

I have tried to get Chrome working in headless mode (with a view to running under SpecFlow/xUnit). This has failed spectacularly and I would like to know if anyone has a solution/fix.

I'm running VS 2015 on Windows 7 with a .NET 4.7 console application and Google Chrome Version 65.0.3325.162 (Official Build) (64-bit)...

Here's my packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Selenium.Support" version="3.11.0" targetFramework="net461" />
  <package id="Selenium.WebDriver" version="3.11.0" targetFramework="net461" />
  <package id="Selenium.WebDriver.ChromeDriver" version="2.36.0" targetFramework="net461" />
  <package id="xunit" version="2.3.1" targetFramework="net461" />
  <package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
  <package id="xunit.analyzers" version="0.8.0" targetFramework="net461" />
  <package id="xunit.assert" version="2.3.1" targetFramework="net461" />
  <package id="xunit.core" version="2.3.1" targetFramework="net461" />
  <package id="xunit.extensibility.core" version="2.3.1" targetFramework="net461" />
  <package id="xunit.extensibility.execution" version="2.3.1" targetFramework="net461" />
  <package id="xunit.runner.visualstudio" version="2.3.1" targetFramework="net461" developmentDependency="true" />
</packages>

And here's my Class1.cs:

using OpenQA.Selenium.Chrome;
using Xunit;

namespace xUnitSpecFlowChrome
{
    public class Class1
    {
        [Fact]
        public void GoTest()
        {
            var options = new ChromeOptions();
            options.AddArgument("--headless");
            options.AddArgument("start-maximized");
            options.AddArgument("--disable-gpu");
            options.AddArgument("--disable-extensions");
            var driver = new ChromeDriver(options);

            driver.Navigate().GoToUrl("http://www.daringfireball.net");
            var title = driver.Title;
        }
    }
}

And here's what I see, when running that test, in the output:

------ Run test started ------
[xUnit.net 00:00:00.5727852]   Starting:    xUnitSpecFlowChrome
[xUnit.net 00:00:03.3212989]     xUnitSpecFlowChrome.Class1.GoTest [FAIL]
[xUnit.net 00:00:03.3239354]       System.InvalidOperationException : session not created exception
from tab crashed
  (Session info: headless chrome=65.0.3325.162)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64) (SessionNotCreated)
[xUnit.net 00:00:03.3264983]       Stack Trace:
[xUnit.net 00:00:03.3276332]            at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
[xUnit.net 00:00:03.3280606]            at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
[xUnit.net 00:00:03.3284914]            at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
[xUnit.net 00:00:03.3289079]            at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
[xUnit.net 00:00:03.3292786]            at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
[xUnit.net 00:00:03.3296566]            at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
[xUnit.net 00:00:03.3300055]         C:\git\xUnitSpecFlowChrome\xUnitSpecFlowChrome\Class1.cs(14,0): at xUnitSpecFlowChrome.Class1.GoTest()
[xUnit.net 00:00:03.3573096]   Finished:    xUnitSpecFlowChrome
========== Run test finished: 1 run (0:00:04.757) ==========

UPDATE

Having changed nothing of apparent consequence, I am now seeing this in the output pane:

 ------ Run test started ------
[xUnit.net 00:00:00.3376673]   Starting:    xUnitSpecFlowChrome
[xUnit.net 00:01:00.8905380]     xUnitSpecFlowChrome.Class1.GoTest [FAIL]
[xUnit.net 00:01:00.9024429]       OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:23698/session timed out after 60 seconds.
[xUnit.net 00:01:00.9038433]       ---- System.Net.WebException : The operation has timed out
[xUnit.net 00:01:00.9185440]       Stack Trace:
[xUnit.net 00:01:00.9199720]            at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
[xUnit.net 00:01:00.9212636]            at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
[xUnit.net 00:01:00.9224907]            at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
[xUnit.net 00:01:00.9237665]            at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
[xUnit.net 00:01:00.9250241]            at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
[xUnit.net 00:01:00.9262697]            at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
[xUnit.net 00:01:00.9275900]            at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
[xUnit.net 00:01:00.9289251]            at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
[xUnit.net 00:01:00.9302787]         C:\git\xUnitSpecFlowChrome\xUnitSpecFlowChrome\Class1.cs(16,0): at xUnitSpecFlowChrome.Class1.GoTest()
[xUnit.net 00:01:00.9315103]         ----- Inner Stack Trace -----
[xUnit.net 00:01:00.9327543]            at System.Net.HttpWebRequest.GetResponse()
[xUnit.net 00:01:00.9339867]            at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
[xUnit.net 00:01:00.9698773]   Finished:    xUnitSpecFlowChrome
========== Run test finished: 1 run (0:01:01.085) ==========

回答1:

The error System.InvalidOperationException : session not created exception indicates that a session could not be created.

To configure Headless Chrome you have to pass the following arguments through an instance of ChromeOptions :

var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("start-maximized");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-extensions");
var driver = new ChromeDriver(options);

Additional Considertations

Ensure the following :

  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
  • If your base Chrome version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Chrome.
  • Execute your @Test.

Update

As you are still seeing the error as from tab crashed, this issue relates to lack of memory within the system/docker_image. You can find a detailed discussion in UnknownError: session deleted because of page crash from tab crashed



回答2:

Genuinely can't believe this was it:

options.AddArgument("no-sandbox");

As accidentally found in: https://stackoverflow.com/a/39299877/71376

Don't know why this option isn't documented or flagged up more - but I hope this helps others.



回答3:

The posted answers did not work for me - the test works in Chrome, but not with the --headless option argument. However, it did not work as expected and I got a timeout error.

This code worked with the latest version... Window size is important for headless mode. Default sizes are 1280x800 for headed mode and 1366x768 in headless mode.

public void GoTest()
{
    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--window-size=1920,1080");
    options.AddArgument("--disable-gpu");
    options.AddArgument("--disable-extensions");
    options.AddArgument("--proxy-server='direct://'");
    options.AddArgument("--proxy-bypass-list=*");
    options.AddArgument("--start-maximized");
    options.AddArgument("--headless");
    options.AddArgument("no-sandbox");

    var _driver = new ChromeDriver(options);
    _driver.Navigate().GoToUrl("https://www.google.com");
    object html = _driver.ExecuteScript("return document.body.parentElement.outerHTML");

    _driver.Close();
}