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
Method html_elements()
Extract HTML elements from the current page.
Method press()
Simulate pressing a single key (including special keys).
Usage
LiveHTML$press(css, key_code, modifiers = character())
Arguments
css
CSS selector or xpath expression. Set to
NULL
key_code
Name of key. You can see a complete list of known keys at https://pptr.dev/api/puppeteer.keyinput/.
modifiers
A character vector of modifiers. Must be one or more of
"Shift
,"Control"
,"Alt"
, or"Meta"
.
Examples
if (FALSE) {
# 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()
}