Skip to content

This set of functions allows you to simulate a user interacting with a website, using forms and navigating from page to page.

  • Create a session with session(url)

  • Navigate to a specified url with session_jump_to(), or follow a link on the page with session_follow_link().

  • Submit an html_form with session_submit().

  • View the history with session_history() and navigate back and forward with session_back() and session_forward().

  • Extract page contents with html_element() and html_elements(), or get the complete HTML document with read_html().

  • Inspect the HTTP response with httr::cookies(), httr::headers(), and httr::status_code().

Usage

session(url, ...)

is.session(x)

session_jump_to(x, url, ...)

session_follow_link(x, i, css, xpath, ...)

session_back(x)

session_forward(x)

session_history(x)

session_submit(x, form, submit = NULL, ...)

Arguments

url

A URL, either relative or absolute, to navigate to.

...

Any additional httr config to use throughout the session.

x

A session.

i

A integer to select the ith link or a string to match the first link containing that text (case sensitive).

css, xpath

Elements to select. Supply one of css or xpath depending on whether you want to use a CSS selector or XPath 1.0 expression.

form

An html_form to submit

submit

Which button should be used to submit the form?

  • NULL, the default, uses the first button.

  • A string selects a button by its name.

  • A number selects a button using its relative position.

Examples

s <- session("http://hadley.nz")
s %>%
  session_jump_to("hadley-wickham.jpg") %>%
  session_jump_to("/") %>%
  session_history()
#> Warning: Not Found (HTTP 404).
#>   https://hadley.nz/
#>   https://hadley.nz/hadley-wickham.jpg
#> - https://hadley.nz/

s %>%
  session_jump_to("hadley-wickham.jpg") %>%
  session_back() %>%
  session_history()
#> Warning: Not Found (HTTP 404).
#> - https://hadley.nz/
#>   https://hadley.nz/hadley-wickham.jpg

# \donttest{
s %>%
  session_follow_link(css = "p a") %>%
  html_elements("p")
#> Navigating to http://rstudio.com
#> {xml_nodeset (68)}
#>  [1] <p class="d-inline">\n            <b><a style="color: #ffffff; fon ...
#>  [2] <p class="d-inline pl-0 pt-1 pr-3">\n            <b class="pr-3">< ...
#>  [3] <p><a style="color: #ffffff; font-size: .9em;" href="https://posit ...
#>  [4] <p>The premier IDE for R</p>
#>  [5] <p>RStudio anywhere using a web browser</p>
#>  [6] <p>Put Shiny applications online</p>
#>  [7] <p>Shiny, R Markdown, Tidyverse and more</p>
#>  [8] <p>Next level training for you and your team</p>
#>  [9] <p>Do, share, teach and learn data science</p>
#> [10] <p>An easy way to access R packages</p>
#> [11] <p>Let us host your Shiny applications</p>
#> [12] <p>A single home for R &amp; Python Data Science Teams</p>
#> [13] <p>Scale, develop, and collaborate across R &amp; Python</p>
#> [14] <p>Easily share your insights</p>
#> [15] <p>Control and distribute packages</p>
#> [16] <p>RStudio</p>
#> [17] <p>RStudio Server</p>
#> [18] <p>Shiny Server</p>
#> [19] <p>R Packages</p>
#> [20] <p>RStudio Academy</p>
#> ...
# }