MyNixOS website logo
Description

'WebDriver' Client for 'PhantomJS'.

A client for the 'WebDriver' 'API'. It allows driving a (probably headless) web browser, and can be used to test web applications, including 'Shiny' apps. In theory it works with any 'WebDriver' implementation, but it was only tested with 'PhantomJS'.

webdriver

'WebDriver' Client for 'PhantomJS'

R-CMD-check CRAN RStudio mirror downloads Coverage Status

A client for the 'WebDriver' 'API'. It allows driving a (probably headless) web browser, and can be used to test web applications, including 'Shiny' apps. In theory it works with any 'WebDriver' implementation, but it was only tested with 'PhantomJS'.

Installation

install.packages("webdriver")

Usage

library(webdriver)

PhantomJS

webdriver uses PhantomJS as a headless web browser. (In theory in works with other WebDriver clients as well.) You can use the install_phantomjs() function to download and install PhantomJS on your system. Alternatively an installation that is in the PATH is sufficient.

The run_phantomjs() function starts PhantomJS, and waits until it is ready to serve queries. It returns a process object that you can terminate manually, and the port on which PhantomJS is listening.

pjs <- run_phantomjs()
pjs
## $process
## PROCESS 'phantomjs', running, pid 45932.
## 
## $port
## [1] 5793

Sessions

Use the Session class to connection to a running PhantomJS process. One process can save multiple sessions, and the sessions are independent of each other.

ses <- Session$new(port = pjs$port)

Once a session is established, you can manipulate the headless web browser through it:

ses$go("https://r-pkg.org/pkg/callr")
ses$getUrl()
## [1] "https://r-pkg.org/pkg/callr"
ses$getTitle()
## [1] "callr @ METACRAN"

You can also take a screenshot of the whole web page, and show it on R's graphics device, or save it to a PNG file:

ses$takeScreenshot()

HTML elements

The Session object has two methods to find HTML elements on the current web page, which can then be further manipulated: findElement() and findElements(). They work with CSS or XPATH selectors, and also with (possibly partial) HTML text.

install <- ses$findElement(".install-package")
install$getName()
## [1] "div"
install$getText()
## [1] "install.packages(\"callr\")"

If you have an HTML element that can receive keyboard keys, you can use the sendKeys() method to send them. The key list helps with sending special, characters, e.g. key$enter corresponds to pressing ENTER. For example we can type into a search box:

search <- ses$findElement("#cran-input")
search$sendKeys("html", key$enter)
ses$getUrl()
## [1] "https://r-pkg.org/search.html?q=html"
ses$getTitle()
## [1] "METACRAN search results"
ses$takeScreenshot()

JavaScript

The executeScript() method of a Session object runs arbitrary JavaScript in the headless browser. It puts the supplied code into the body of a JavaScript function, and the function will receive the additional arguments, in its arguments array. Element objects as arguments are automatically converted to the corresponding DOM elements in the browser.

The JavaScript function can return values to R. Returned HTML elements are automatically converted to Element objects.

ses$executeScript("return 42 + 'foobar';")
## [1] "42foobar"
search2 <- ses$executeScript("return document.getElementById('cran-input');")
search2$getName()
## [1] "input"

Element objects also have an executeScript() method, which works the same way as the Session method, but it automatically supplies the HTML element as the first argument of the JavaScript function.

executeScript() works synchronously. If you need asynchronous execution, you can use the executeScriptAsync() function.

License

MIT © Mango Solutions, RStudio.

Metadata

Version

1.0.6

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows