| WebElement {selenium} | R Documentation |
Create a live element
Description
This class represents a single element on the page. It is created using an existing SeleniumSession instance.
Public fields
idThe id of the element, used to uniquely identify it on the page.
Methods
Public 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_idThe id of the session that the element belongs to.
req, verbosePrivate fields of a SeleniumSession object.
idThe element id.
Returns
A WebElement object.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
element <- session$find_element(using = "css selector", value = "#download")
session$close()
}
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.
Usage
WebElement$shadow_root(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
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
usingThe type of selector to use.
valueThe value of the selector: a string.
request_bodyA list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
A WebElement object.
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
usingThe type of selector to use.
valueThe value of the selector: a string.
request_bodyA list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
A list of WebElement objects.
Examples
\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 is_selected()
Check if an element is currently selected.
Usage
WebElement$is_selected(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
A boolean value: TRUE or FALSE.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$is_selected()
session$close()
}
Method get_attribute()
Get an attribute from an element.
Usage
WebElement$get_attribute(name, request_body = NULL, timeout = 20)
Arguments
nameThe name of the attribute.
request_bodyA list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The value of the attribute: a string.
Examples
\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 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.
Usage
WebElement$get_property(name, request_body = NULL, timeout = 20)
Arguments
nameThe name of the property.
request_bodyA list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The value of the property: a string.
Examples
\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 get_css_value()
Get the computed value of a CSS property.
Usage
WebElement$get_css_value(name, request_body = NULL, timeout = 20)
Arguments
nameThe name of the CSS property.
request_bodyA list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The value of the CSS property: a string.
Examples
\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 get_text()
Get the text content of an element.
Usage
WebElement$get_text(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The text content of the element: a string.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_text()
session$close()
}
Method get_tag_name()
Get the tag name of an element.
Usage
WebElement$get_tag_name(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The tag name of the element: a string.
Examples
\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 get_rect()
Get the dimensions and coordinates of an element.
Usage
WebElement$get_rect(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
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.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "#download")$get_rect()
session$close()
}
Method is_enabled()
Check if an element is currently enabled.
Usage
WebElement$is_enabled(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
A boolean value: TRUE or FALSE.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$is_enabled()
session$close()
}
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.
Usage
WebElement$computed_role(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
A string.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$computed_role()
session$close()
}
Method computed_label()
Get the computed label of an element (i.e. The text of the label element that points to the current element).
Usage
WebElement$computed_label(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
A string.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$computed_label()
session$close()
}
Method click()
Click on an element.
Usage
WebElement$click(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The element, invisibly.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$click()
session$close()
}
Method clear()
Clear the contents of a text input element.
Usage
WebElement$clear(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The element, invisibly.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.google.com")
session$find_element(using = "css selector", value = "textarea")$clear()
session$close()
}
Method send_keys()
Send keys to an element.
Usage
WebElement$send_keys(..., request_body = NULL, timeout = 20)
Arguments
...The keys to send (strings). Use keys for special keys, and use
key_chord()to send keys combinations.request_bodyA list of request body parameters to pass to the Selenium server, overriding the default body of the web request
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The element, invisibly.
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 screenshot()
Take a screenshot of an element.
Usage
WebElement$screenshot(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
The base64-encoded PNG screenshot, as a string.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$screenshot()
session$close()
}
Method is_displayed()
Check if an element is displayed. This function may not work on all platforms.
Usage
WebElement$is_displayed(timeout = 20)
Arguments
timeoutHow long to wait for a request to recieve a response before throwing an error.
Returns
A boolean.
Examples
\dontrun{
session <- SeleniumSession$new()
session$navigate("https://www.r-project.org")
session$find_element(using = "css selector", value = "a")$is_displayed()
session$close()
}
Method toJSON()
Convert an element to JSON. This is used by SeleniumSession$execute_script().
Usage
WebElement$toJSON()
Returns
A list, which can then be converted to JSON using
jsonlite::toJSON().
Examples
\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()
}
Method clone()
The objects of this class are cloneable with this method.
Usage
WebElement$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
## ------------------------------------------------
## 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)