This class represents a single element on the page. It is created using an existing SeleniumSession instance.
Methods
Method new()
Initialize a WebElement
object. This should not be called manually:
instead use SeleniumSession$create_webelement() if
you have an element id. To find elements on the page, use
SeleniumSession$find_element() and
SeleniumSession$find_elements().
Usage
WebElement$new(session_id, req, verbose, id)
Arguments
session_id
The id of the session that the element belongs to.
req, verbose
Private fields of a SeleniumSession object.
id
The element id.
Method shadow_root()
A shadow DOM is a self-contained DOM tree, contained within another DOM tree. A shadow root is an element that contains a DOM subtree. This method gets the shadow root property of an element.
Returns
A ShadowRoot object.
Examples
\dontrun{
session <- SeleniumSession$new()
# Let's create our own Shadow Root using JavaScript
session$execute_script("
const div = document.createElement('div');
document.body.appendChild(div);
div.attachShadow({mode: 'open'});
")
element <- session$find_element(using = "css selector", value = "div")
shadow_root <- element$shadow_root()
session$close()
}
Method find_element()
Find the first element matching a selector, relative to the current element.
Usage
WebElement$find_element(
using = c("css selector", "xpath", "tag name", "link text", "partial link text"),
value,
request_body = NULL,
timeout = 20
)
Arguments
using
The type of selector to use.
value
The value of the selector: a string.
request_body
A list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeout
How long to wait for a request to recieve a response before throwing an error.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
row <- session$find_element(using = "css selector", value = ".row")
logo_container <- row$find_element(using = "css selector", value = "p")
logo <- logo_container$find_element(using = "css selector", value = "img")
session$close()
}
Method find_elements()
Find all elements matching a selector, relative to the current element.
Usage
WebElement$find_elements(
using = c("css selector", "xpath", "tag name", "link text", "partial link text"),
value,
request_body = NULL,
timeout = 20
)
Arguments
using
The type of selector to use.
value
The value of the selector: a string.
request_body
A list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeout
How long to wait for a request to recieve a response before throwing an error.
Method get_attribute()
Get an attribute from an element.
Method get_property()
Get a property from an element. Properties are similar to attributes, but represent the HTML source code of the page, rather than the current state of the DOM.
Method get_css_value()
Get the computed value of a CSS property.
Method get_rect()
Get the dimensions and coordinates of an element.
Method computed_role()
Get the computed role of an element. The role of an element is usually "generic", but is often used when an elements tag name differs from its purpose. For example, a link that is "button-like" in nature may have a "button" role.
Method computed_label()
Get the computed label of an element (i.e. The text of the label element that points to the current element).
Method send_keys()
Send keys to an element.
Arguments
...
The keys to send (strings). Use keys for special keys, and use
key_chord()
to send keys combinations.request_body
A list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeout
How long to wait for a request to recieve a response before throwing an error.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.google.com")
input <- session$find_element(using = "css selector", value = "textarea")
input$send_keys("Hello")
input$send_keys(key_chord(keys$control, "a"), key_chord(keys$control, "c"))
input$send_keys(keys$control, "v")
input$get_attribute("value")
session$close()
}
Method is_displayed()
Check if an element is displayed. This function may not work on all platforms.
Method toJSON()
Convert an element to JSON. This is used by SeleniumSession$execute_script().
Returns
A list, which can then be converted to JSON using
jsonlite::toJSON()
.
Examples
## ------------------------------------------------
## Method `WebElement$new`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
element <- session$find_element(using = "css selector", value = "#download")
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$shadow_root`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
# Let's create our own Shadow Root using JavaScript
session$execute_script("
const div = document.createElement('div');
document.body.appendChild(div);
div.attachShadow({mode: 'open'});
")
element <- session$find_element(using = "css selector", value = "div")
shadow_root <- element$shadow_root()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$find_element`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
row <- session$find_element(using = "css selector", value = ".row")
logo_container <- row$find_element(using = "css selector", value = "p")
logo <- logo_container$find_element(using = "css selector", value = "img")
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$find_elements`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
row <- session$find_element(using = "css selector", value = ".row")
links <- row$find_elements(using = "css selector", value = "a")
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$is_selected`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$is_selected()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$get_attribute`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$get_attribute("href")
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$get_property`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$get_property("href")
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$get_css_value`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$get_css_value("color")
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$get_text`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_text()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$get_tag_name`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_tag_name()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$get_rect`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_rect()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$is_enabled`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$is_enabled()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$computed_role`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$computed_role()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$computed_label`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$computed_label()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$click`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$click()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$clear`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.google.com")
session$find_element(using = "css selector", value = "textarea")$clear()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$send_keys`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.google.com")
input <- session$find_element(using = "css selector", value = "textarea")
input$send_keys("Hello")
input$send_keys(key_chord(keys$control, "a"), key_chord(keys$control, "c"))
input$send_keys(keys$control, "v")
input$get_attribute("value")
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$screenshot`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$screenshot()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$is_displayed`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$is_displayed()
session$close()
} # }
## ------------------------------------------------
## Method `WebElement$toJSON`
## ------------------------------------------------
if (FALSE) { # \dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
result <- session$find_element(using = "css selector", value = "a")$toJSON()
result
jsonlite::toJSON(result, auto_unbox = TRUE)
session$close()
} # }