Browse Source

forms & clicks

master
boB Rudis 5 years ago
parent
commit
859adf6520
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 2
      NAMESPACE
  2. 43
      R/wc-click.R
  3. 38
      R/wc-forms.R
  4. 44
      R/web-client.R
  5. 20
      man/wc_click_on.Rd
  6. 20
      man/wc_fill_in.Rd

2
NAMESPACE

@ -5,10 +5,12 @@ S3method(print,webclient)
export("%>%")
export(hu_read_html)
export(wc_browser_info)
export(wc_click_on)
export(wc_content_length)
export(wc_content_type)
export(wc_css)
export(wc_dnt)
export(wc_fill_in)
export(wc_geo)
export(wc_go)
export(wc_headers)

43
R/wc-click.R

@ -0,0 +1,43 @@
#' Click on a DOM element in a webclient loaded page
#'
#' @note The caller does not have to assign the output of this function to a
#' variable as the browser state is managed internally by HtmlUnit.
#' @param wc_obj a `webclient` object
#' @param css,xpath Node to click on. Supply one of css or xpath depending on whether you want to use a css or xpath 1.0 selector.
#' @export
wc_click_on <- function(wc_obj, css, xpath) {
pg <- wc_obj$wc$getCurrentWindow()$getEnclosedPage()
if (.jnull() == pg) return(NULL)
if (missing(css) && missing(xpath))
stop("Please supply one of css or xpath", call. = FALSE)
if (!missing(css) && !missing(xpath))
stop("Please supply css or xpath, not both", call. = FALSE)
if (!missing(css)) {
if (!is.character(css) && length(css) == 1) stop("`css` must be a string")
item <- pg$querySelector(css)
} else {
if (!is.character(xpath) && length(xpath) == 1)
stop("`xpath` must be a string")
item <- as.list(pg$getByXPath(xpath))[[1]]
}
if (length(item) == 0) {
warning("No item found with that selector.")
} else if (length(item) > 1) {
warning("More than one item found with that selector.")
} else {
item$click()
}
return(wc_obj)
}

38
R/wc-forms.R

@ -1,17 +1,43 @@
#' Retrieve a form on a webclient page by name
#' Fill in a input box in a form field
#'
#' @note This returns a `webclient_form` object _not_ the `wc_obj` passed in
#' @note The caller does not have to assign the output of this function to a
#' variable as the browser state is managed internally by HtmlUnit.
#' @param wc_obj a `webclient` object
#' @param name form name
#' @param css,xpath Node to select. Supply one of css or xpath depending on whether you want to use a css or xpath 1.0 selector.
#' @export
wc_get_form_by_name <- function(wc_obj, name) {
wc_fill_in <- function(wc_obj, value, css, xpath) {
pg <- wc_obj$wc$getCurrentWindow()$getEnclosedPage()
if (.jnull() == pg) return(NULL)
f <- pg$getFormByName(name)
if (missing(css) && missing(xpath))
stop("Please supply one of css or xpath", call. = FALSE)
f
if (!missing(css) && !missing(xpath))
stop("Please supply css or xpath, not both", call. = FALSE)
if (!missing(css)) {
if (!is.character(css) && length(css) == 1) stop("`css` must be a string")
item <- pg$querySelector(css)
} else {
if (!is.character(xpath) && length(xpath) == 1)
stop("`xpath` must be a string")
item <- as.list(pg$getByXPath(xpath))[[1]]
}
if (length(item) == 0) {
warning("No item found with that selector.")
} else if (length(item) > 1) {
warning("More than one item found with that selector.")
} else {
}
return(wc_obj)
}

44
R/web-client.R

@ -26,14 +26,21 @@ web_client <- function(emulate = c("best", "chrome", "firefox", "ie"),
) -> use_browser
wc <- new(J("com.gargoylesoftware.htmlunit.WebClient"), use_browser)
wc_opts <- wc$getOptions()
wc_opts$setThrowExceptionOnFailingStatusCode(FALSE)
wc_opts$setThrowExceptionOnScriptError(FALSE)
wc_opts$setDownloadImages(FALSE)
wc$getOptions()$setThrowExceptionOnFailingStatusCode(FALSE)
wc$getOptions()$setThrowExceptionOnScriptError(FALSE)
wc$getOptions()$setDownloadImages(FALSE)
wc$getOptions()$setJavaScriptEnabled(TRUE)
wc$getOptions()$setCssEnabled(TRUE)
wc$getOptions()$setDoNotTrackEnabled(FALSE)
wc$getOptions()$setGeolocationEnabled(TRUE)
wc$getOptions()$setPopupBlockerEnabled(FALSE)
wc$getOptions()$setPrintContentOnFailingStatusCode(TRUE)
wc$getOptions()$setRedirectEnabled(TRUE)
list(
wc = wc,
wc_opts = wc_opts
wc_opts = wc$getOptions()
) -> wc_obj
class(wc_obj) <- c("webclient")
@ -120,6 +127,31 @@ print.browserinfo <- function(x, ...) {
#' @export
print.webclient <- function(x, ...) {
cat("<webclient>\n")
bv <- x$wc$getBrowserVersion()
cat(
sprintf(
"<webclient - %s %s; %s>\n",
bv$getApplicationName(),
bv$getApplicationVersion(),
bv$getBrowserLanguage())
)
pg <- x$wc$getCurrentWindow()$getEnclosedPage()
if (!(.jnull() == pg)) {
cat(sprintf(" Current URL: <%s>\n", pg$getUrl()$toString()))
if (pg$getTitleText() != "") cat(sprintf(" Page Title: <%s>\n", pg$getTitleText()))
r <- pg$getWebResponse()
cat(sprintf(" Status Code: %s\n", r$getStatusCode()))
cat(sprintf(" Content Type: %s\n", r$getContentType()))
cat(sprintf(" Content Length: %s bytes\n", prettyNum(r$getContentLength(), big.mark=",")))
cat(sprintf(" Load Time: %s ms\n", prettyNum(r$getLoadTime(), big.mark=",")))
}
}

20
man/wc_click_on.Rd

@ -0,0 +1,20 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/wc-click.R
\name{wc_click_on}
\alias{wc_click_on}
\title{Click on a DOM element in a webclient loaded page}
\usage{
wc_click_on(wc_obj, css, xpath)
}
\arguments{
\item{wc_obj}{a \code{webclient} object}
\item{css, xpath}{Node to click on. Supply one of css or xpath depending on whether you want to use a css or xpath 1.0 selector.}
}
\description{
Click on a DOM element in a webclient loaded page
}
\note{
The caller does not have to assign the output of this function to a
variable as the browser state is managed internally by HtmlUnit.
}

20
man/wc_fill_in.Rd

@ -0,0 +1,20 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/wc-forms.R
\name{wc_fill_in}
\alias{wc_fill_in}
\title{Fill in a input box in a form field}
\usage{
wc_fill_in(wc_obj, value, css, xpath)
}
\arguments{
\item{wc_obj}{a \code{webclient} object}
\item{css, xpath}{Node to select. Supply one of css or xpath depending on whether you want to use a css or xpath 1.0 selector.}
}
\description{
Fill in a input box in a form field
}
\note{
The caller does not have to assign the output of this function to a
variable as the browser state is managed internally by HtmlUnit.
}
Loading…
Cancel
Save