phantomjs Getting started with phantomjs

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

PhantomJS is a headless Selenium WebDriver with JavaScript support.

It is based on WebKit, making it behave similarly to Google Chrome or Safari.

It is slightly faster than a regular WebDriver like ChromeDriver or FirefoxDriver in both startup time and performance.

PhantomJS has many options and services that alter the behavior of the test, such as hiding the command prompt or not loading images.

Installation or Setup

For Visual Studio [NuGet]:

The easiest way of installing PhantomJS is by using a NuGet Package Manager.

In your project, right click "References", and click on "Manage NuGet Packages" as shown:

Visual Studio screenshot

Then, type "PhantomJS" to the search bar, select it and install it as shown below.

NuGet Package Manager

Here's a list of other recommended packages:

  • Selenium.WebDriver - To use PhantomJS with Selenium
  • Selenium.Support - To further extend capabilities of Selenium

Now, add these references at the beginning:

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

Now you can test it with a simple program like this [C#]:

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

Loading a Webpage

var page = require('webpage').create();
page.open('http://www.google.com', function(status) {
  console.log("Status: " + status);
  var title = page.evaluate(function() {
    return document.title;
  });
  console.log("Loaded page: " + title);
  phantom.exit();
});
 


Got any phantomjs Question?