selenium-webdriver Headless Browsers PhantomJS [C#]


Example

PhantomJS is a fully featured headless web browser with JavaScript support.

Before you start you will need to download a PhantomJS driver, and make sure to put this in the beginning of your code:

using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;

Great, now onto the initialization:

var driver = new PhantomJSDriver();

This will simply create a new instance of the PhantomJSDriver class. You can then use it the same way as every WebDriver such as:

using (var driver = new PhantomJSDriver())
{
    driver.Navigate().GoToUrl("http://stackoverflow.com/");

    var questions = driver.FindElements(By.ClassName("question-hyperlink"));

    foreach (var question in questions)
    {
        // This will display every question header on StackOverflow homepage.
        Console.WriteLine(question.Text);
    }
}

This works fine. However, the problem you probably encountered is, when working with UI, PhantomJS opens a new console window, which is not really wanted in most cases. Luckily, we can hide the window, and even slightly improve performance using PhantomJSOptions, and PhantomJSDriverService. Full working example below:

// Options are used for setting "browser capabilities", such as setting a User-Agent
// property as shown below:
var options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent", 
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");

// Services are used for setting up the WebDriver to your likings, such as
// hiding the console window and restricting image loading as shown below:
var service = PhantomJSDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
service.LoadImages = false;

// The same code as in the example above:
using (var driver = new PhantomJSDriver(service, options))
{
    driver.Navigate().GoToUrl("http://stackoverflow.com/");

    var questions = driver.FindElements(By.ClassName("question-hyperlink"));

    foreach (var question in questions)
    {
        // This will display every question header on StackOverflow homepage.
        Console.WriteLine(question.Text);
    }
}

Pro tip: click on a class (e.g the PhantomJSDriverService), and press F12 to see exactly what they contain along with a brief description of what they do.