MyNixOS website logo
Description

Concise, Lazy and Reliable Wrapper for 'chromote' and 'selenium'.

A user-friendly wrapper for web automation, using either 'chromote' or 'selenium'. Provides a simple and consistent API to make web scraping and testing scripts easy to write and understand. Elements are lazy, and automatically wait for the website to be valid, resulting in reliable and reproducible code, with no visible impact on the experience of the programmer.

selenider

R-CMD-check Codecov testcoverage CRANstatus

Traditionally, automating a web browser is often unreliable, especially when using R. Programmers are forced to write verbose code, utilising inconsistent workarounds (such as using Sys.sleep() to wait for something to happen).

selenider aims to make web testing and scraping in R much simpler, providing a wrapper for either chromote or selenium. It is inspired by Java’s Selenide and Python’s Selene.

Code reliability and reproducibility are essential when writing R code. selenider provides features to make your scripts work every time they are run, without any extra code:

  • Lazy elements: selenider will only try to find an element on the page when it is absolutely necessary. Your definitions of HTML elements are separated from their existence on the page, only allowing the two to converge when absolutely necessary. In selenider, HTML elements are stored as the directions to the element on the page, rather than the element itself. This is much more reliable than the alternative since the webpage can constantly change, resulting in elements becoming invalid between their creation and use (e.g. the dreaded StaleElementReferenceException in Selenium).
  • Automatic waiting: selenider will automatically wait for your code to work (e.g. waiting for an input to exist and be clickable before actually clicking it), allowing you to write scripts as if your website always responds instantly to your interactions.

selenider’s other main focus is its API. Its design choices result in concise yet expressive code that is easy to read and easy to write:

  • A global session object results in shorter, more declarative code. It also allows the session to be created at the beginning of your script or test, and closed at the end.
  • All functions are designed for use with the pipe operator (|> or %>%); elements can be selected, tested and operated on in a single pipeline.
  • elem_expect() is a powerful way to specify test expectations, with a simple but extensible syntax and informative error messages.
  • selenider is compatible with automated testing frameworks like testthat and shinytest2.

Installation

# Install selenider from CRAN
install.packages("selenider")

# Or the development version from Github
# install.packages("remotes")
remotes::install_github("ashbythorpe/selenider")

Additionally, you must install chromote or selenium. We recommend chromote, as it is quicker and easier to get up and running.

# Either:
install.packages("chromote")

# Or:
install.packages("selenium")

If you are using selenium, you must also have Java installed.

Finally, you must have a web browser installed. For chromote, Google Chrome is required. For selenium, any browser can be used, but Firefox is recommended.

Usage

library(selenider)

The following code navigates to the R project website, finds the link to the CRAN mirror list, checks that the link is correct, and clicks the link element.

open_url("https://www.r-project.org/")

s(".row") |>
  find_element("div") |>
  find_elements("a") |>
  elem_find(has_text("CRAN")) |>
  elem_expect(attr_contains("href", "cran.r-project.org")) |>
  elem_click()

Now that we’re in the mirror list page, let’s find the link to every CRAN mirror in the UK.

s("dl") |>
  find_elements("dt") |>
  elem_find(has_text("UK")) |>
  find_element(xpath = "./following-sibling::dd") |>
  find_elements("tr") |>
  elem_expect(has_at_least(1)) |>
  as.list() |>
  lapply(
    \(x) x |>
      find_element("a") |>
      elem_attr("href")
  )
#> [[1]]
#> [1] "https://www.stats.bris.ac.uk/R/"
#> 
#> [[2]]
#> [1] "https://cran.ma.imperial.ac.uk/"
#> 
#> [[3]]
#> [1] "https://anorien.csc.warwick.ac.uk/CRAN/"

Vignettes

  • Get started with selenider, and learn the basics: vignette("selenider").
  • Use selenider with testthat, shinytest2 and Github Actions: vignette("unit-testing", package = "selenider").
  • Use selenider with rvest: vignette("with-rvest", package = "selenider")
Metadata

Version

0.4.0

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