Search code examples
c#seleniumselenium-chromedriver

How to start ChromeDriver in headless mode


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);

Solution

  • 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);