WebElement | R Documentation |
This class represents a single element on the page. It is created using an existing SeleniumSession instance.
id
The id of the element, used to uniquely identify it on the page.
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().
WebElement$new(session_id, req, verbose, id)
session_id
The id of the session that the element belongs to.
req, verbose
Private fields of a SeleniumSession object.
id
The element id.
A WebElement
object.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") element <- session$find_element(using = "css selector", value = "#download") session$close() }
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.
WebElement$shadow_root(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
A ShadowRoot object.
\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() }
find_element()
Find the first element matching a selector, relative to the current element.
WebElement$find_element( using = c("css selector", "xpath", "tag name", "link text", "partial link text"), value, request_body = NULL, timeout = 20 )
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.
A WebElement
object.
\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() }
find_elements()
Find all elements matching a selector, relative to the current element.
WebElement$find_elements( using = c("css selector", "xpath", "tag name", "link text", "partial link text"), value, request_body = NULL, timeout = 20 )
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.
A list of WebElement
objects.
\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() }
is_selected()
Check if an element is currently selected.
WebElement$is_selected(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
A boolean value: TRUE
or FALSE
.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "#download")$is_selected() session$close() }
get_attribute()
Get an attribute from an element.
WebElement$get_attribute(name, request_body = NULL, timeout = 20)
name
The name of the attribute.
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.
The value of the attribute: a string.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$get_attribute("href") session$close() }
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.
WebElement$get_property(name, request_body = NULL, timeout = 20)
name
The name of the property.
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.
The value of the property: a string.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$get_property("href") session$close() }
get_css_value()
Get the computed value of a CSS property.
WebElement$get_css_value(name, request_body = NULL, timeout = 20)
name
The name of the CSS property.
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.
The value of the CSS property: a string.
\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() }
get_text()
Get the text content of an element.
WebElement$get_text(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
The text content of the element: a string.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "#download")$get_text() session$close() }
get_tag_name()
Get the tag name of an element.
WebElement$get_tag_name(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
The tag name of the element: a string.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "#download")$get_tag_name() session$close() }
get_rect()
Get the dimensions and coordinates of an element.
WebElement$get_rect(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
A list containing the following elements:
x
: The x-coordinate of the element.
y
: The y-coordinate of the element.
width
: The width of the element in pixels.
height
: The height of the element in pixels.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "#download")$get_rect() session$close() }
is_enabled()
Check if an element is currently enabled.
WebElement$is_enabled(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
A boolean value: TRUE
or FALSE
.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$is_enabled() session$close() }
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.
WebElement$computed_role(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
A string.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$computed_role() session$close() }
computed_label()
Get the computed label of an element (i.e. The text of the label element that points to the current element).
WebElement$computed_label(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
A string.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$computed_label() session$close() }
click()
Click on an element.
WebElement$click(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
The element, invisibly.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$click() session$close() }
clear()
Clear the contents of a text input element.
WebElement$clear(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
The element, invisibly.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.google.com") session$find_element(using = "css selector", value = "textarea")$clear() session$close() }
send_keys()
Send keys to an element.
WebElement$send_keys(..., request_body = NULL, timeout = 20)
...
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.
The element, invisibly.
\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() }
screenshot()
Take a screenshot of an element.
WebElement$screenshot(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
The base64-encoded PNG screenshot, as a string.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$screenshot() session$close() }
is_displayed()
Check if an element is displayed. This function may not work on all platforms.
WebElement$is_displayed(timeout = 20)
timeout
How long to wait for a request to recieve a response before throwing an error.
A boolean.
\dontrun{ session <- SeleniumSession$new() session$navigate("https://www.r-project.org") session$find_element(using = "css selector", value = "a")$is_displayed() session$close() }
toJSON()
Convert an element to JSON. This is used by SeleniumSession$execute_script().
WebElement$toJSON()
A list, which can then be converted to JSON using
jsonlite::toJSON()
.
\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() }
clone()
The objects of this class are cloneable with this method.
WebElement$clone(deep = FALSE)
deep
Whether to make a deep clone.
## ------------------------------------------------
## Method `WebElement$new`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
element <- session$find_element(using = "css selector", value = "#download")
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$shadow_root`
## ------------------------------------------------
## Not run:
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()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$find_element`
## ------------------------------------------------
## Not run:
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()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$find_elements`
## ------------------------------------------------
## Not run:
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()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$is_selected`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$is_selected()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$get_attribute`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$get_attribute("href")
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$get_property`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$get_property("href")
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$get_css_value`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$get_css_value("color")
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$get_text`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_text()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$get_tag_name`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_tag_name()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$get_rect`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_rect()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$is_enabled`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$is_enabled()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$computed_role`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$computed_role()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$computed_label`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$computed_label()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$click`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$click()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$clear`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.google.com")
session$find_element(using = "css selector", value = "textarea")$clear()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$send_keys`
## ------------------------------------------------
## Not run:
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()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$screenshot`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$screenshot()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$is_displayed`
## ------------------------------------------------
## Not run:
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$is_displayed()
session$close()
## End(Not run)
## ------------------------------------------------
## Method `WebElement$toJSON`
## ------------------------------------------------
## Not run:
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()
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.