Java Archive Wrapper Supporting the ‘htmlunit’ Package
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
boB Rudis 3920aa655a
rebuild for htmlunit 2.35.0
1 month ago
R example 6 months ago
docs initial commit 6 months ago
inst/java rebuild for htmlunit 2.35.0 1 month ago
java/htmlunit rebuild for htmlunit 2.35.0 1 month ago
man stuff 6 months ago
tests initial commit 6 months ago
.Rbuildignore 2.34.0 update 3 months ago
.codecov.yml R package repo initialization complete 6 months ago
.gitignore R package repo initialization complete 6 months ago
.travis.yml R package repo initialization complete 6 months ago
DESCRIPTION rebuild for htmlunit 2.35.0 1 month ago
LICENSE stuff 6 months ago
NAMESPACE initial commit 6 months ago
NEWS.md rebuild for htmlunit 2.35.0 1 month ago
README.Rmd stuff 6 months ago
README.md 2.34.0 update 3 months ago
htmlunitjars.Rproj R package repo initialization complete 6 months ago

README.md

htmlunitjars

Java Archive Wrapper Supporting the ‘htmlunit’ Package

Description

Contents of the HtmlUnit & supporting Java archives http://htmlunit.sourceforge.net/. 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: http://htmlunit.sourceforge.net/apidocs/index.html

Installation

devtools::install_github("hrbrmstr/htmlunitjars")

Usage

library(htmlunitjars)

# current verison
packageVersion("htmlunitjars")
## [1] '2.34.0'

Give It A Go

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

library(rvest)

test_url <- "https://hrbrmstr.github.io/htmlunitjars/index.html"

doc <- read_html(test_url)

html_table(doc)
## list()

☹️

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

library(htmlunitjars)

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:

invisible(wc$waitForBackgroundJavaScriptStartingBefore(.jlong(2000L)))

wc_opts <- wc$getOptions()
wc_opts$setThrowExceptionOnFailingStatusCode(FALSE)
wc_opts$setThrowExceptionOnScriptError(FALSE)

Now, acccess the site again and get the table:

pg <- wc$getPage(test_url)

doc <- read_html(pg$asXml())

html_table(doc)
## [[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.