R/xml2_find.R

Defines functions ele_children ele_child go getElementByClass getElementByName getElementById ele_find_all.character ele_find_all.remoteDriver ele_find_all ele_find_first.character ele_find_first.remoteDriver ele_find_first

Documented in ele_child ele_children ele_find_all ele_find_all.character ele_find_all.remoteDriver ele_find_first ele_find_first.character ele_find_first.remoteDriver getElementByClass getElementById getElementByName

#' Search for an element on the page
#'
#' @inherit  seleniumPipes::findElement details
#'
#' @param x A remoteDriver object
#' @param value The search target.
#' @param type Locator scheme to use to search the element, available schemes:
#' `c("xpath", "class name", "css selector", "id", "name", "link text", "partial link text",
#' "tag name")`. Defaults to 'xpath'. Partial string matching is
#' accepted.
#'
#' @seealso [seleniumPipes::findElement()]
#' @import xml2
#' @export
ele_find_first <- function(x, ...) UseMethod("ele_find_first")

#' @export
#' @rdname ele_find_first
ele_find_first.remoteDriver <- function(x, value, type = "css selector", ...) {
  elem <- x$findElement(type, value)
  elem
}

#' @export
#' @rdname ele_find_first
ele_find_first.character <- function(x, type = "css selector", ...) {
  x <- get_browser()
  elem <- x$findElement(type, value)
  elem
}

#' @rdname ele_find_first
#' @export
ele_find_all <- function(x, value, type = "css selector", ...) UseMethod("ele_find_all")

#' @rdname ele_find_first
#' @export
ele_find_all.remoteDriver <- function(x, value, type = "css selector", ...) {
  if (type == "css") type <- "css selector"
  elem <- x$findElements(type, value)
  elem
  # lapply(elem, xml_html)
}

#' @rdname ele_find_first
#' @export
ele_find_all.character <- function(x, type = "css selector", ...) {
  if (type == "css") type <- "css selector"
  elem <- get_browser()$findElements(type, x)
  elem
  # lapply(elem, xml_html)
}

#' @rdname ele_find_first
#' @export
getElementById <- function(x, value, all = FALSE) {
  fun <- ifelse(all, ele_find_all, ele_find_first)
  fun(x, value, "id")
}

#' @rdname ele_find_first
#' @export
getElementByName <- function(x, value, all = FALSE) {
  fun <- ifelse(all, ele_find_all, ele_find_first)
  fun(x, value, "name")
}

#' @rdname ele_find_first
#' @export
getElementByClass <- function(x, value, all = FALSE) {
  value <- sprintf("//*[contains(@class, '%s')]", value)
  fun <- ifelse(all, ele_find_all, ele_find_first)
  fun(x, value, "xpath")
}

# #' @export
# getElementById <- function(p, Id) xml_check(p) %>% xml_find_all(sprintf("//*[@id='%s']", Id))

# #' @export
# getElementByName <- function(p, Id) xml_check(p) %>% xml_find_all(sprintf("//*[@name='%s']", Id))

#' @export
go <- function(x, url) {
  x$navigate(url)
}

#' @rdname ele_find_first
#' @export
ele_child <- function(x, value = "*", type = "xpath") {
  x$findChildElement(type, value)
}

#' @rdname ele_find_first
#' @export
ele_children <- function(x, value = "*", type = "xpath") {
  x$findChildElements(type, value)
}

#' @rdname ele_find_first
#' @export
querySelector <- ele_find_first

#' @rdname ele_find_first
#' @export
querySelectorAll <- ele_find_all
rpkgs/curlR documentation built on Jan. 30, 2023, 7:26 p.m.