Check out my iOS apps here
As an Amazon Associate I earn from qualifying purchases.

What is HtmlUnitDriver, how to use it with Selenium Web Driver & Java, and what are its advantages & disadvantages?

What Is HtmlUnitDriver?

HtmlUnitDriver is headless browser you could launch with Selenium Web Driver, meaning that it’s a browser without a user interface. Such browsers have many advantages, like taking less resources, since they don’t have to render the web elements visually on the screen. Although some pages may not render correctly in such browsers, they tend to work well most of the time.

In this tutorial, we will look on the various ways to add HtmlUnitDriver to your project, and how to launch it with various configurations. It’s worth noting that HtmlUnitDriver is one of multple ways of launching headless browser. We will look at other ways to launch these browsers thorough the series. ☇

This tutorial is part of a series about Selenium Web Driver. You can go to the first part in the series by clicking here.

Advantages Of HtmlUnitDriver

  • Lightweight & runs faster for most of the tests.
  • Takes less recourses, since it won’t need to render the GUI.
  • Can run more instances using the same resources, compared to browsers with GUI.
  • Supports Java script
  • Can run known browsers, such as Firefox, Chrome & Edge.

Disadvantages Of HtmlUnitDriver

  • Not having a rendered GUI can cause issues sometimes.
  • Sometimes it’s hard to fix its issues, specially with our inability to see the screen.
  • Codes written for web driver with GUI may not always work on HtmlUnitDriver right away.

Adding HtmlUnitDriver To Your Project

To use the HTML Unit browser, simply add the Maven or Gradle dependency to your project. On Maven, you simply add the following dependency to your pom.xml file:-


If you use Gradle, add the following dependency to your build.gradle file:-

compile group: 'org.seleniumhq.selenium', name: 'selenium-htmlunit-driver', version: '2.52.0'

Launching Htmlunitdriver Using Java

Once we added HtmlUnitDriver to our project, We can launch browser instances with it, which is done by creating an instance of the class HtmlUnitDriver. We can interact with it like any other WebDriver instance. For a starter, we can use the getTitle() function to get the page title & print it to console, since that’s the easiest way to make sure the page was loaded correctly:-

WebDriver HTMLUnitdriver = new HtmlUnitDriver();
String PageTitle = HTMLUnitdriver.getTitle();

Htmlunitdriver Options

You can specify what browser HTML Unit launches in the constructor. We do that for Firefox here:-

WebDriver HTMLUnitdriver = new HtmlUnitDriver(BrowserVersion.FIREFOX);

HTML Unit has JavaScript is disabled by default, but you can enable it in the constructor:-

WebDriver HTMLUnitdriver = new HtmlUnitDriver(true);

You can enable JavaScript & specify a browser at the same time in the constructor:-

WebDriver HTMLUnitdriver  = new HtmlUnitDriver(BrowserVersion.FIREFOX, true);

And Finally

I hope you liked my tutorial has helped you learn more about HtmlUnitDriver, and see you again in another one.

See Also:-

Leave a Comment

Your email address will not be published. Required fields are marked *