Project Status: Active – The project has reached a stable, usable state and is being actively developed. Signed by Signed commit %Linux build Status Coverage Status Minimal R VersionLicense

Java Archive Wrapper Supporting the ‘htmlunit’ Package


Contents of the ‘HtmlUnit’ & supporting Java archives ( Version number reflects the version number of the included ‘JAR’ file.

HtmlUnit is a “GUI-Less browser for Java programs”. 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 has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used.

It is typically used for testing purposes or to retrieve information from web sites.

HtmlUnit is not a generic unit testing framework. It is specifically a way to simulate a browser.

What’s Inside The Tin

Everything necessary to use the HtmlUnit library directly via rJava.

HtmlUnit Library JavaDoc:


install.packages("htmlunitjars", repos = "")
# or
# or
# or
# or
# or

NOTE: To use the ‘remotes’ install options you will need to have the {remotes} package installed.



# current verison
## [1] '2.36.0'

Give It A Go

xml2::read_html() cannot execute javascript so the traditional approach won’t work:


test_url <- ""

doc <- read_html(test_url)

## list()


We can do this with the classes from HtmlUnit proivided by this JAR wrapper package:


Tell HtmlUnit to work like FireFox:

browsers <- J("com.gargoylesoftware.htmlunit.BrowserVersion")

wc <- new(J("com.gargoylesoftware.htmlunit.WebClient"), browsers$CHROME)

Tell it to wait for javascript to execute and not throw exceptions on page resource errors:


wc_opts <- wc$getOptions()

Now, acccess the site again and get the table:

pg <- wc$getPage(test_url)

doc <- read_html(pg$asXml())

## [[1]]
##      X1   X2
## 1   One  Two
## 2 Three Four
## 3  Five  Six

No need for Selenium or Splash!

The ultimate goal is to have an htmlunit package that provides a nicer API than needing to know how to work with rJava directly.

htmlunitjars Metrics

Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
Java 2 0.2 28 0.30 5 0.11 18 0.17
Rmd 1 0.1 21 0.22 35 0.76 50 0.48
Maven 1 0.1 17 0.18 0 0.00 1 0.01
R 5 0.5 15 0.16 1 0.02 36 0.34
make 1 0.1 13 0.14 5 0.11 0 0.00