diff --git a/DESCRIPTION b/DESCRIPTION index d9680b1..ee5ddd7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,4 +42,4 @@ Config/testthat/parallel: true Encoding: UTF-8 Language: en-US Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/R/live.R b/R/live.R index 6f7828e..ee65ddc 100644 --- a/R/live.R +++ b/R/live.R @@ -127,17 +127,19 @@ LiveHTML <- R6::R6Class( #' @description Simulate a click on an HTML element. - #' @param css CSS selector or xpath expression. + #' @param css,xpath CSS selector or xpath expression. #' @param n_clicks Number of clicks - click = function(css, n_clicks = 1) { + click = function(css, xpath, n_clicks = 1) { private$check_active() + check_exclusive(css, xpath) + check_number_whole(n_clicks, min = 1) # Implementation based on puppeteer as described in # https://medium.com/@aslushnikov/automating-clicks-in-chromium-a50e7f01d3fb # With code from https://github.com/puppeteer/puppeteer/blob/b53de4e0942e93c/packages/puppeteer-core/src/cdp/Input.ts#L431-L459 - node <- private$wait_for_selector(css) + node <- private$wait_for_selector(css, xpath) self$session$DOM$scrollIntoViewIfNeeded(node) # Quad = location of four corners (x1, y1, x2, y2, x3, y3, x4, y4) @@ -277,17 +279,28 @@ LiveHTML <- R6::R6Class( } }, - wait_for_selector = function(css, timeout = 5) { + wait_for_selector = function(css, xpath, timeout = 5) { + check_exclusive(css, xpath) + done <- now() + timeout while(now() < done) { - nodes <- private$find_nodes(css) + if (!missing(css)) { + nodes <- private$find_nodes(css = css) + } else { + nodes <- private$find_nodes(xpath = xpath) + } if (length(nodes) > 0) { return(nodes) } Sys.sleep(0.1) } - cli::cli_abort("Failed to find selector {.str {css}} in {timeout} seconds.") + + if(!missing(css)){ + cli::cli_abort("Failed to find selector {.str {css}} in {timeout} seconds.") + } else { + cli::cli_abort("Failed to find xpath {.str {xpath}} in {timeout} seconds.") + } }, find_nodes = function(css, xpath) { diff --git a/man/LiveHTML.Rd b/man/LiveHTML.Rd index a568e66..0198d51 100644 --- a/man/LiveHTML.Rd +++ b/man/LiveHTML.Rd @@ -122,13 +122,13 @@ Extract HTML elements from the current page. \subsection{Method \code{click()}}{ Simulate a click on an HTML element. \subsection{Usage}{ -\if{html}{\out{