pub struct ShadowRoot { /* private fields */ }Expand description
Handle for a shadow root.
Represents the root of a shadow DOM tree and allows querying within it. Supports both open and closed shadow roots.
Implementations§
Source§impl ShadowRoot
impl ShadowRoot
Sourcepub fn shadow_root_type(&self) -> Option<&ShadowRootType>
pub fn shadow_root_type(&self) -> Option<&ShadowRootType>
Returns the shadow root type (open or closed) if known.
Sourcepub async fn query_selector(
&self,
selector: &str,
) -> Result<Option<ElementHandle>>
pub async fn query_selector( &self, selector: &str, ) -> Result<Option<ElementHandle>>
Queries the shadow DOM for the first matching element.
§Parameters
selector- CSS selector or XPath expression.- CSS selectors such as
"div.class"or"#id"are recommended. - XPath expressions start with
"xpath:"or/, for example"//div[@class='test']".
- CSS selectors such as
§XPath Caveats
XPath support within the shadow DOM is limited. Because CDP’s
DOM.performSearch operates globally, results may include nodes outside
the shadow root. Prefer CSS selectors when possible.
§Returns
The first matching element handle, or None when nothing matches.
§Examples
let host = page.query_selector("#shadow-host").await?.unwrap();
let shadow_root = host.shadow_root().await?.unwrap();
// Preferred CSS selector path
if let Some(element) = shadow_root.query_selector(".target").await? {
element.click().await?;
}
// XPath fallback (limited support)
if let Some(element) = shadow_root.query_selector("//button[text()='Click']").await? {
element.click().await?;
}Sourcepub async fn query_selector_all(
&self,
selector: &str,
) -> Result<Vec<ElementHandle>>
pub async fn query_selector_all( &self, selector: &str, ) -> Result<Vec<ElementHandle>>
Queries the shadow DOM for all matching elements.
§Parameters
selector- CSS selector or XPath expression.- CSS selectors such as
"div.class"or"#id"work reliably. - XPath expressions start with
"xpath:"or/, for example"//div[@class='test']".
- CSS selectors such as
§XPath Caveats
XPath support within the shadow DOM is limited. Because CDP’s
DOM.performSearch is global, XPath queries may surface nodes outside
of the shadow root. Prefer CSS selectors when possible.
§Returns
A vector of matching element handles.
§Examples
let host = page.query_selector("#shadow-host").await?.unwrap();
let shadow_root = host.shadow_root().await?.unwrap();
// Preferred CSS selector path
let items = shadow_root.query_selector_all(".item").await?;
for (i, item) in items.iter().enumerate() {
println!("Item {}: {}", i + 1, item.text_content().await?);
}
// XPath fallback (limited support)
let buttons = shadow_root.query_selector_all("//button").await?;Sourcepub async fn get_html(&self) -> Result<String>
pub async fn get_html(&self) -> Result<String>
Retrieves the HTML markup contained within the shadow root.
§Returns
The HTML string for the shadow root.
§Examples
let host = page.query_selector("#shadow-host").await?.unwrap();
let shadow_root = host.shadow_root().await?.unwrap();
let html = shadow_root.get_html().await?;
println!("Shadow DOM HTML: {}", html);Trait Implementations§
Source§impl Clone for ShadowRoot
impl Clone for ShadowRoot
Source§fn clone(&self) -> ShadowRoot
fn clone(&self) -> ShadowRoot
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more