selenium-webdriver Headless Browsers Headless browser in Java


Example

HTMLUnitDriver

HTMLUnitDriver is the most lightweight implementation of headless(GUI-less) browser for Webdriver based on HtmlUnit. It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc. just like you do in your normal browser. It supports JavaScript and works with AJAX libraries. It is used for testing and retrieving data from website.


Example: Use of HTMLUnitDriver to fetch list of questions from http://stackoverflow.com/.


import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
        
class testHeadlessDriver{
            private void getQuestions() {
                    WebDriver driver = new HtmlUnitDriver();
                    driver.get("http://stackoverflow.com/");
                    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                    List<WebElement> questions = driver.findElements(By.className("question-hyperlink"));
                    questions.forEach((question) -> {
                        System.out.println(question.getText());
                    });
                   driver.close();
                }
    }

It is same as any other browser(Mozilla Firefox, Google Chrome, IE), but it does not have GUI, execution is not visible on screen.