pub struct NodeHandle { /* private fields */ }Expand description
A handle to a live DOM node backed by a CDP RemoteObjectId.
Obtained via PageHandle::query_selector_all. Each method issues one or
more CDP Runtime.callFunctionOn calls against the held V8 remote object
reference — no HTML serialisation occurs.
A handle becomes stale after page navigation or if the underlying DOM
node is removed. Stale calls return BrowserError::StaleNode so callers
can distinguish them from other CDP failures.
§Example
use stygian_browser::{BrowserPool, BrowserConfig, WaitUntil};
use std::time::Duration;
let pool = BrowserPool::new(BrowserConfig::default()).await?;
let handle = pool.acquire().await?;
let mut page = handle.browser().expect("valid browser").new_page().await?;
page.navigate("https://example.com", WaitUntil::DomContentLoaded, Duration::from_secs(30)).await?;
for node in page.query_selector_all("a[href]").await? {
let href = node.attr("href").await?;
let text = node.text_content().await?;
println!("{text}: {href:?}");
}Implementations§
Source§impl NodeHandle
impl NodeHandle
Sourcepub async fn attr(&self, name: &str) -> Result<Option<String>>
pub async fn attr(&self, name: &str) -> Result<Option<String>>
Return a single attribute value, or None if the attribute is absent.
Issues one Runtime.callFunctionOn CDP call (el.getAttribute(name)).
§Errors
Returns BrowserError::StaleNode when the remote object has been
invalidated, or BrowserError::Timeout / BrowserError::CdpError
on transport-level failures.
Sourcepub async fn attr_map(&self) -> Result<HashMap<String, String>>
pub async fn attr_map(&self) -> Result<HashMap<String, String>>
Return all attributes as a HashMap<name, value> in a single
CDP round-trip.
Uses DOM.getAttributes (via the chromiumoxide attributes() API)
which returns a flat [name, value, name, value, …] list from the node
description — no per-attribute calls are needed.
§Errors
Returns BrowserError::StaleNode when the remote object has been
invalidated.
Sourcepub async fn text_content(&self) -> Result<String>
pub async fn text_content(&self) -> Result<String>
Return the element’s textContent (all text inside, no markup).
Returns an empty string when the property is absent or null.
§Errors
Returns BrowserError::StaleNode when the remote object has been
invalidated.
Sourcepub async fn inner_html(&self) -> Result<String>
pub async fn inner_html(&self) -> Result<String>
Return the element’s innerHTML.
Returns an empty string when the property is absent or null.
§Errors
Returns BrowserError::StaleNode when the remote object has been
invalidated.
Sourcepub async fn outer_html(&self) -> Result<String>
pub async fn outer_html(&self) -> Result<String>
Return the element’s outerHTML.
Returns an empty string when the property is absent or null.
§Errors
Returns BrowserError::StaleNode when the remote object has been
invalidated.
Sourcepub async fn ancestors(&self) -> Result<Vec<String>>
pub async fn ancestors(&self) -> Result<Vec<String>>
Return the ancestor tag-name chain, root-last.
Executes a single Runtime.callFunctionOn JavaScript function that
walks parentElement and collects tag names — no repeated CDP calls.
// for <span> inside <p> inside <article> inside <body> inside <html>
["p", "article", "body", "html"]§Errors
Returns BrowserError::StaleNode when the remote object has been
invalidated, or BrowserError::ScriptExecutionFailed when the
JSON returned by the script cannot be parsed.
Sourcepub async fn children_matching(&self, selector: &str) -> Result<Vec<Self>>
pub async fn children_matching(&self, selector: &str) -> Result<Vec<Self>>
Return child elements matching selector as new NodeHandles.
Issues a single Runtime.callFunctionOn + DOM.querySelectorAll
call scoped to this element — not to the entire document.
Returns an empty Vec when no children match (consistent with the JS
querySelectorAll contract).
§Errors
Returns BrowserError::StaleNode when the remote object has been
invalidated, or BrowserError::CdpError on transport failure.