Quick Tip: Running Chrome In Headless Mode With Selenium In C#

Friday, 18 August 2017

Quick Tip: Running Chrome In Headless Mode With Selenium In C#

Chrome can now be run in headless mode. To do this with Selenium, create a chrome options object and add the '--headless' arg to it. When creating an instance of the chrome driver, pass in the options to the constructor of the driver. Chromedriver will now run without a UI. Code sample below.

      
 static void Main(string[] args)
    {
      //Create chrome options with headles arg
      ChromeOptions options = new ChromeOptions();
      options.AddArgument("--headless");

      var driver = new ChromeDriver(options);
      driver.Url = "http://www.paulsodimu.co.uk";

      //Take Screenshot 
      Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
      ss.SaveAsFile($"{AppContext.BaseDirectory}\\Shot.png", ScreenshotImageFormat.Png);

      Console.ReadKey();
      driver.Quit();
    }