53

I want to try out headless chrome, but I am running into this issue, that I can't start the driver in headless mode. I was following google documentation. am I missing something ? The code execution gets stuck in var browser = new ChromeDriver(); line

Here is my code:

var chromeOptions = new ChromeOptions
{
    BinaryLocation = @"C:\Users\2-as Aukstas\Documents\Visual Studio 2017\Projects\ChromeTest\ChromeTest\bin\Debug\chromedriver.exe",
    DebuggerAddress = "localhost:9222"
};

chromeOptions.AddArguments(new List<string>() {"headless", "disable-gpu" });

var browser = new ChromeDriver(chromeOptions);


browser.Navigate().GoToUrl("https://stackoverflow.com/");
Console.WriteLine(browser.FindElement(By.CssSelector("#h-top-questions")).Text);
Evaldas B
  • 2,464
  • 3
  • 17
  • 25

5 Answers5

88

UPDATE
Chrome version 60 is out so all you need to do is to download ChromeDriver and Selenium via NuGet and use this simple code and everything works like a charm. Amazing.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

...



var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");

using (var browser = new ChromeDriver(chromeOptions))
{
  // add your code here
}

DATED

There is a solution until the official release of Chrome 60. You can download Chrome Canary and use headless with it. After installation set BinaryLocation to point to Chrome Canary. Also comment out the DebuggerAddress line (it forces Chrome to time out):

var chromeOptions = new ChromeOptions
{
    BinaryLocation = @"C:\Users\2-as Aukstas\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
    //DebuggerAddress = "127.0.0.1:9222"
};

chromeOptions.AddArguments(new List<string>() { "no-sandbox", "headless", "disable-gpu" });

var _driver = new ChromeDriver(chromeOptions);
workerbee
  • 57
  • 8
Evaldas B
  • 2,464
  • 3
  • 17
  • 25
6

For you that did not get reference for ChromeDriver. Use this step :

  1. Download the dll from this: http://seleniumtestings.com/selenium-download/

  2. Extract, and you should see: Selenium.WebDriverBackedSelenium.dll, ThoughtWorks.Selenium.Core.dll, WebDriver.dll and WebDriver.Support.dll

  3. Add those files via "Add Reference"

Now you can use it:

String url = "http://www.google.com";
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() {
    "--silent-launch",
    "--no-startup-window",
    "no-sandbox",
    "headless",});

var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;    // This is to hidden the console.
ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl(url);   

====

If after you run, you are still facing error about no ChromeDriver.exe file, try to add the Selenium.WebDriver.ChromeDriver, WebDriver.ChromeDriver, WebDriver.ChromeDriver.win32, Selenium.Chrome.WebDriver via nuget.

Haryono
  • 2,184
  • 1
  • 21
  • 14
4

As alternative:

  • Add 2 libraries via NuGet like below picture.

  • Try below Code:

    String url = "http://www.google.com";
    var chromeOptions = new ChromeOptions();
    chromeOptions.AddArguments(new List<string>() { "headless" });
    
    var chromeDriverService = ChromeDriverService.CreateDefaultService();
    ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
    driver.Navigate().GoToUrl(url);
    

Selenium Chrome WebDriver

Haryono
  • 2,184
  • 1
  • 21
  • 14
2

What OS you're running? I see on developers.google.com/web/updates/2017/04/headless-chrome that headless won't be available on Windows until Chrome 60.

Kapil
  • 397
  • 4
  • 6
1

Below i have given how to set the headless to true for firefox and chrome browsers.

FirefoxOptions ffopt = new FirefoxOptions();
FirefoxOptions option = ffopt.setHeadless(true);
WebDriver driver = new FirefoxDriver(option);


ChromeOptions coptions = new ChromeOptions();
ChromeOptions options = coptions.setHeadless(true);
WebDriver driver = new ChromeDriver(options);
  • 3
    It looks like this code is in Java. The question was for C#. There is no `setHeadless()` method (or equivalent) in C#'s `ChromeDriver` class. – urig Mar 06 '20 at 11:37