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 class represents a shadow root object, allowing you to select elements within the shadow root.
Methods
Method new()
Initialize a new ShadowRoot
object. This should not be called
manually: instead use WebElement$shadow_root(), or
SeleniumSession$create_shadow_root().
Usage
ShadowRoot$new(session_id, req, verbose, id)
Arguments
session_id
The id of the session.
req, verbose
Private fields of a SeleniumSession object.
id
The id of the shadow root.
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")
element$shadow_root()
session$close()
}
Method find_element()
Find an element in the shadow root.
Usage
ShadowRoot$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.
Returns
A WebElement 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);
const shadowRoot = div.attachShadow({mode: 'open'});
const span = document.createElement('span');
span.textContent = 'Hello';
shadowRoot.appendChild(span);
")
element <- session$find_element(using = "css selector", value = "div")
shadow_root <- element$shadow_root()
shadow_root$find_element(using = "css selector", value = "span")
session$close()
}
Method find_elements()
Find all elements in a shadow root matching a selector.
Usage
ShadowRoot$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.
Returns
A list of WebElement objects.
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);
const shadowRoot = div.attachShadow({mode: 'open'});
const span = document.createElement('span');
span.textContent = 'Hello';
shadowRoot.appendChild(span);
const p = document.createElement('p');
p.textContent = 'Me too!';
shadowRoot.appendChild(p);
")
element <- session$find_element(using = "css selector", value = "div")
shadow_root <- element$shadow_root()
shadow_root$find_elements(using = "css selector", value = "*")
session$close()
}
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
\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()
result <- shadow_root$toJSON()
result
jsonlite::toJSON(result, auto_unbox = TRUE)
session$close()
}
Examples
## ------------------------------------------------
## Method `ShadowRoot$new`
## ------------------------------------------------
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")
element$shadow_root()
session$close()
} # }
## ------------------------------------------------
## Method `ShadowRoot$find_element`
## ------------------------------------------------
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);
const shadowRoot = div.attachShadow({mode: 'open'});
const span = document.createElement('span');
span.textContent = 'Hello';
shadowRoot.appendChild(span);
")
element <- session$find_element(using = "css selector", value = "div")
shadow_root <- element$shadow_root()
shadow_root$find_element(using = "css selector", value = "span")
session$close()
} # }
## ------------------------------------------------
## Method `ShadowRoot$find_elements`
## ------------------------------------------------
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);
const shadowRoot = div.attachShadow({mode: 'open'});
const span = document.createElement('span');
span.textContent = 'Hello';
shadowRoot.appendChild(span);
const p = document.createElement('p');
p.textContent = 'Me too!';
shadowRoot.appendChild(p);
")
element <- session$find_element(using = "css selector", value = "div")
shadow_root <- element$shadow_root()
shadow_root$find_elements(using = "css selector", value = "*")
session$close()
} # }
## ------------------------------------------------
## Method `ShadowRoot$toJSON`
## ------------------------------------------------
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()
result <- shadow_root$toJSON()
result
jsonlite::toJSON(result, auto_unbox = TRUE)
session$close()
} # }