Headless Browser for C# (.Net)

Headless browser for C# (.NET)?

There are some options:

  • WebKit.Net (free)

  • Awesomium

    It is based on Chrome/WebKit and works like a charm.
    There is a free license available but also a commercial one and if need be you can buy the source code :-)

  • HTML Agility Pack (free) (An HTML Parser library, NOT a headless browser)

    This helps with extracting information from HTML etc. and might be useful in your case (possibly in combination with HttpWebRequest)

How can i use Headless Browser with Selenium and C#.Net

While there are several such headless browsers available in the market, the following are the most popular ones:

  • Headless Chrome
  • PhantomJS
  • SlimerJS
  • TrifleJS
  • HTMLUnit driver

Pseudo code:

  package chrome;
public class HeadlessTesting {
public static void main(String[] args) throws IOException{
System.setProperty("webdriver.chrome.driver",
"ChromeDriverPath");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1200x600");
WebDriver driver = new ChromeDriver(options);
driver.get("https://contentstack.built.io");
driver.get("https://www.google.co.in/");
System.out.println("title is: " + driver.getTitle());
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("pathTOSaveFile"));
driver.quit();
}
}

How to start ChromeDriver in headless mode

UPDATE

Chrome version 60 is out so all you need to do is to download Chromdriver 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 will be released. 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 timeout):

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


Related Topics



Leave a reply



Submit