You construct an LiveHTML object with read_html_live() and then interact,
like you're a human, using the methods described below. When debugging a
scraping script it is particularly useful to use $view(), which will open
a live preview of the site, and you can actually see each of the operations
performed on the real site.
rvest provides relatively simple methods for scrolling, typing, and clicking. For richer interaction, you probably want to use a package that exposes a more powerful user interface, like selendir.
Methods
LiveHTML$new()
initialize the object
Arguments
urlURL to page.
mode, view, browserAs described in
read_html_live().timeoutNumber of seconds to wait for the page to load.
errorExecution environment used for error messages.
LiveHTML$wait_for()
Wait for an element to appear on the page. Useful when a page renders content with JavaScript after the initial page load.
LiveHTML$click()
Simulate a click on an HTML element.
Usage
LiveHTML$click(css, n_clicks = 1, method = c("mouse", "js"))Arguments
cssCSS selector.
n_clicksNumber of clicks
methodClick method.
"mouse"simulates a real mouse click and requires the element to be visible on the page."js"calls JavaScript'selement.click()directly, which works even for hidden or off-screen elements, but only fires theclickevent (nomousedown,mouseup, or hover events).
LiveHTML$download()
Click on an element that triggers a download, and wait for the download to complete.
Usage
LiveHTML$download(css, dir = tempdir(), timeout = 30)LiveHTML$press()
Simulate pressing a single key (including special keys).
Usage
LiveHTML$press(css, key_code, modifiers = character())Arguments
cssCSS selector.
key_codeName of key. You can see a complete list of known keys at https://pptr.dev/api/puppeteer.keyinput.
modifiersA character vector of modifiers. Must be one or more of
"Shift,"Control","Alt", or"Meta".
Examples
if (FALSE) { # \dontrun{
# To retrieve data for this paginated site, we need to repeatedly push
# the "Load More" button
sess <- read_html_live("https://www.bodybuilding.com/exercises/finder")
sess$view()
sess |> html_elements(".ExResult-row") |> length()
sess$click(".ExLoadMore-btn")
sess |> html_elements(".ExResult-row") |> length()
sess$click(".ExLoadMore-btn")
sess |> html_elements(".ExResult-row") |> length()
} # }
