HtmlUnitDriver – Non GUI Browser in Selenium

HtmlUnitDriver – Non GUI Browser in Selenium

What is HtmlUnitDriver?

HtmlUnitDriver is headless driver providing non-GUI implementation of Selenium WebDriver. It is based on HtmlUnit, fastest and light-weight browser implemented in Java.

WebDriver driver = new HtmlUnitDriver();

Advantages of HtmlUnitDriver

  1. Test execution using HtmlUnitDriver is very fast. Since, it is the fastest implementation of Selenium WebDriver.
  2. Being headless and fast, it is an ideal choice for web scrapping.
  3. Its browser – htmlUnit is Java-based. Hence, it is platform-independent.
  4. It also supports JavaScript execution through an in-built Rhino javascript engine.

Limitations of HtmlUnitDriver

  1. Being non-GUI makes it difficult to create scripts and debug issues while scripting.
  2. The Rhino javascript engine of HtmlUnitDriver makes it unsuitable to emulate other popular browser’s javascript behavior.

Code Snippet for HtmlUnitDriver

The script creation in HtmlUnitDriver is similar to scripting in any other driver, only the driver instantiation part is different. The below script, when executed will run the test in non-GUI mode without opening any browser.

public class HtmlUnitDriverDemo {

public void htmlUnitDriverTest(){

//Instantiating HtmlUnitDriver
WebDriver driver = new HtmlUnitDriver();

//Navigate to

//Printing page title


That’s all we have in this post, comment below if you have any queries. Also please share this post with your friends and don’t forget to check our complete selenium tutorial here.

Selenium with Java – Complete Tutorial

От QA genius