HTMLUnitDriver Example To Run Selenium Webdriver Test Cases

HTMLUnitDriver is not like other web browser driver, it is light weight and headless. It will not display graphic user interface at run time, this can save a lot of system resources and make your test application run faster. But it is a full feature browser driver.

HTML Unit Driver Featrures

  1. Support HtmlUnit web browser. HtmlUnit is a pure java headless web browser.
  2. Support both Http and Https protocal.
  3. Support Cookies, JavaScript and Proxy server.
  4. Can interact with html response page such as submit form, click links and walk through the page dom tree.
  5. Can simulate both Get and Post web request method.
  6. Request header name and values can be customized.

Html Unit Driver Example

This example will use HtmUnitDriver to browse and type search keyword in the search box. Then parse out the search result data list in first search result page and print the result title and description in the Eclipse console.

It will use selenium-server-standalone-3.5.3.jar, because it support both HtmlUnitDriver and FirefoxDriver. It execute well with the latest Firefox browser such as Firefox Setup 55.0b9.

Example Steps

  1. Download selenium-server-standalone-3.5.3.jar.
  2. Add above jar file in java project build path.
    add selenium 3.5.3 jar in java project build path
  3. Download Firefox Setup 55.0b9 and install.
  4. Because webdriver 3 will use GeckoDriver to initiate Firefox Driver, so you need to download also. You can read How to Correctly Use GeckoDriver To Run Webdriver Automation Test Script to learn more.

Example Code

public class TestHtmlUnitDriver {

	public static void main(String[] args) {
		TestHtmlUnitDriver example = new TestHtmlUnitDriver();
	public void testHtmlUnitDriver()
		WebDriver driver = null;
			// Initiate HtmlUnitDriver object.
			driver = new HtmlUnitDriver();
			/* If you want to see the browser interaction, you can uncomment below code to use Firefox to run this test script.
			//assign webdriver executable file path to the value of system variable
			System.setProperty("webdriver.gecko.driver", "C:\\Workspace\\\\Lib\\geckodriver-v0.16.1-win64\\geckodriver.exe");
			//Initiate a browser
			driver = new FirefoxDriver();
			// Get input search text box.
			By byIdSearchInput ="sb_form_q");
			WebElement searchInput = driver.findElement(byIdSearchInput);
				// Type search keyword in search text box.
			// Get search form submit button.
			By byIdSubmitBtn ="sb_form_go");
			WebElement submitBtn = driver.findElement(byIdSubmitBtn);
				// Click submit button to search.;
			// Get search result list in first result page by xpath.
			By byXPathResultList = By.xpath("//*[@id=\"b_results\"]/li[@class=\"b_algo\"]");
			List resultElementList = driver.findElements(byXPathResultList);
				// Loop the search result list.
				int size = resultElementList.size();
				for(int i=0;i<size;i++)
					WebElement resultElement = resultElementList.get(i);
					// Get the result title from current result item by xpath.
					By byXPathTitle = By.xpath(".//a");
					WebElement titleElement = resultElement.findElement(byXPathTitle);
					String title = titleElement.getText();
					// Get the result description from curent result item by xpath.
					By byXPathDesc = By.xpath(".//p");
					WebElement descElement = resultElement.findElement(byXPathDesc);
					String description = descElement.getText();
		}catch(Exception ex)


If you want to know why use the xpath in above example, you can read How To Select The Effective XPath For Web Element In Webdriver.

Console Output

html unit driver example console output

Source Code

  1. [download id=”2640″]


  1. org.openqa.selenium.htmlunit.HtmlUnitDriver
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x