3

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) ==========
Matt W
  • 11,753
  • 25
  • 118
  • 215
  • Referring to the following post, I am also running VS under local admin privs: https://stackoverflow.com/questions/35985964/cant-launch-chrome-driver-in-selenium – Matt W Mar 15 '18 at 18:59
  • According to this file my browser version is supported by my webdriver version: https://chromedriver.storage.googleapis.com/2.36/notes.txt – Matt W Mar 15 '18 at 19:10
  • Can you debug the error `NUnit couldn't find any tests in C:\git\xUnitSpecFlowChrome\xUnitSpecFlowChrome\bin\Debug\xUnitSpecFlowChrome.dll`? – undetected Selenium Mar 16 '18 at 08:53
  • My apologies - I may have made a typo. NUnit is not involved in this solution. Running the project now does not produce that error. – Matt W Mar 16 '18 at 12:23
  • So is your question solved now or you are still looking out for an Answer? – undetected Selenium Mar 16 '18 at 12:28
  • Still looking for an answer. The issue being concerned with the failure reported by chromedriver: session not created exception from tab crashed – Matt W Mar 16 '18 at 12:51
  • Try adding a `--verbose` to the options, it might help you diagnose the problem. Have you set a binary location to chromedriver.exe, if needed? – Erik A. Brandstadmoen Mar 18 '18 at 20:21
  • I will try adding verbose. I did try specifying the binary location, but as I'm using a NuGet package the path it appeared to be using was no different than the one I provided. – Matt W Mar 18 '18 at 20:23
  • I too have the same issue, if I remove options.AddArgument("--headless"); then it's working fine and fast if I keep headless option then getting the timeout issue – Velmurugan Feb 25 '19 at 19:21
  • @velmurugan See the chosen answer. – Matt W Feb 26 '19 at 07:59
  • I too have that option – Velmurugan Feb 26 '19 at 08:04

3 Answers3

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.

Matt W
  • 11,753
  • 25
  • 118
  • 215
0

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Cleaned solution and OS, as advised. Base Chrome is version as described in my post. The output persists as described above. – Matt W Mar 16 '18 at 14:03
  • Ahhhh, can you check your system memory usage, `tab crashed` generally rises out of lack of memory space. – undetected Selenium Mar 16 '18 at 14:06
  • Retried after closing everything except VS. Max usage of memory was 41%. This is a 16GB machine. – Matt W Mar 16 '18 at 15:43
  • I'm not using Docker, however, the message has now changed. I do not know why but I have posted it below the original post. – Matt W Mar 16 '18 at 16:34
  • To be sure that I had not introduced an anomaly, I rebooted and started VS in admin mode immediately. Cleaned, rebuilt and ran the single test in the project and I got the original error output. – Matt W Mar 16 '18 at 16:49
  • Should I be using Chromium or regular Chrome? I have the regularly installed vanilla Chrome. – Matt W Mar 16 '18 at 17:11
  • @MattW Better to stick with regular Chrome. I was wondering in your code you mentioned about `using OpenQA.Selenium.Chrome;` but why not `using OpenQA.Selenium.WebDriver;` – undetected Selenium Mar 16 '18 at 17:15
  • OpenQA.Selenium.WebDriver is not a namespace available to me - the code simply won't build. Should I modify the package list? – Matt W Mar 16 '18 at 17:30
  • Can't find the OpenQA.Selenium.WebDriver class in the documentation: http://seleniumhq.github.io/selenium/docs/api/dotnet/ – Matt W Mar 16 '18 at 18:13
  • Using CCleaner sounds like a joke, how should this ever run on a pipeline? Seriously, this is not how it should be. – Hannes Schneidermayer Aug 26 '21 at 15:25
0

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();
}
Matt W
  • 11,753
  • 25
  • 118
  • 215
Velmurugan
  • 406
  • 8
  • 17