pub struct Session { /* private fields */ }Expand description
A browser session that owns its browser and page.
This is the primary API for library usage. The MCP server uses raw Page directly.
Implementations§
Source§impl Session
impl Session
Sourcepub async fn launch_with_config(stealth: StealthConfig) -> Result<Self>
pub async fn launch_with_config(stealth: StealthConfig) -> Result<Self>
Launch with custom stealth config.
Sourcepub fn set_observe_config(&mut self, config: ObserveConfig)
pub fn set_observe_config(&mut self, config: ObserveConfig)
Set observation config.
Sourcepub async fn observe(&mut self) -> Result<&[InteractiveElement]>
pub async fn observe(&mut self) -> Result<&[InteractiveElement]>
Snapshot the page: enumerate all interactive elements.
Sourcepub async fn screenshot(&mut self) -> Result<Vec<u8>>
pub async fn screenshot(&mut self) -> Result<Vec<u8>>
Take an annotated screenshot with numbered boxes on each element.
Sourcepub fn element_list(&self) -> String
pub fn element_list(&self) -> String
Compact text list for LLM consumption.
Sourcepub fn get(&self, index: usize) -> Option<&InteractiveElement>
pub fn get(&self, index: usize) -> Option<&InteractiveElement>
Get element info by index.
Sourcepub fn elements(&self) -> &[InteractiveElement]
pub fn elements(&self) -> &[InteractiveElement]
Get all observed elements.
Sourcepub fn find_by_text(&self, needle: &str) -> Option<usize>
pub fn find_by_text(&self, needle: &str) -> Option<usize>
Find first element whose text contains the given substring (case-insensitive).
Sourcepub fn find_all_by_text(&self, needle: &str) -> Vec<usize>
pub fn find_all_by_text(&self, needle: &str) -> Vec<usize>
Find all elements whose text contains the given substring (case-insensitive).
Sourcepub async fn observe_diff(&mut self) -> Result<ObserveDiff>
pub async fn observe_diff(&mut self) -> Result<ObserveDiff>
Observe and return a diff against the previous observation.
Use this in multi-step sessions to minimize tokens — only send
added_element_list() to the LLM instead of the full list.
Sourcepub fn added_element_list(&self, diff: &ObserveDiff) -> String
pub fn added_element_list(&self, diff: &ObserveDiff) -> String
Compact text list of only the added elements from the last observe_diff().
Sourcepub async fn screenshot_plain(&self) -> Result<Vec<u8>>
pub async fn screenshot_plain(&self) -> Result<Vec<u8>>
Take a plain screenshot without annotations.
Sourcepub async fn click(&mut self, index: usize) -> Result<()>
pub async fn click(&mut self, index: usize) -> Result<()>
Click an element, auto-recovering if stale. Clears element cache since clicks often trigger navigation/DOM changes.
Sourcepub async fn fill(&mut self, index: usize, text: &str) -> Result<()>
pub async fn fill(&mut self, index: usize, text: &str) -> Result<()>
Fill an element, auto-recovering if stale. Does NOT clear element cache (typing rarely changes DOM structure).
Sourcepub async fn select(&mut self, index: usize, value: &str) -> Result<()>
pub async fn select(&mut self, index: usize, value: &str) -> Result<()>
Select a dropdown option, auto-recovering if stale. Clears element cache since onChange handlers may modify DOM.
Sourcepub async fn try_click(&mut self, index: usize) -> Result<bool>
pub async fn try_click(&mut self, index: usize) -> Result<bool>
Try to click — returns Ok(false) if element is missing or not visible.
Sourcepub async fn human_click(&mut self, index: usize) -> Result<()>
pub async fn human_click(&mut self, index: usize) -> Result<()>
Human-like click by index.
Sourcepub async fn human_fill(&mut self, index: usize, text: &str) -> Result<()>
pub async fn human_fill(&mut self, index: usize, text: &str) -> Result<()>
Human-like fill by index.
Sourcepub async fn submit(&mut self, index: usize) -> Result<()>
pub async fn submit(&mut self, index: usize) -> Result<()>
Focus element by index and press Enter (common for form submission).
Sourcepub async fn options(&mut self, index: usize) -> Result<Vec<(String, String)>>
pub async fn options(&mut self, index: usize) -> Result<Vec<(String, String)>>
Get dropdown options for a select element. Returns vec of (value, text) pairs.
Sourcepub async fn scroll_down(&self) -> Result<()>
pub async fn scroll_down(&self) -> Result<()>
Scroll down by approximately one viewport height.
Sourcepub async fn scroll_to_top(&self) -> Result<()>
pub async fn scroll_to_top(&self) -> Result<()>
Scroll to top.
Sourcepub async fn scroll_to_bottom(&self) -> Result<()>
pub async fn scroll_to_bottom(&self) -> Result<()>
Scroll to bottom.
Sourcepub async fn wait_for_stable(&self) -> Result<()>
pub async fn wait_for_stable(&self) -> Result<()>
Wait for the page to stabilize after an action. Waits up to 2s for network idle, then 50ms for DOM settle. Intentionally succeeds even if network doesn’t fully idle (some sites never stop polling).
Sourcepub async fn wait_for_text(&self, text: &str, timeout_ms: u64) -> Result<()>
pub async fn wait_for_text(&self, text: &str, timeout_ms: u64) -> Result<()>
Wait for text to appear on the page.
Sourcepub async fn wait_for_url(&self, pattern: &str, timeout_ms: u64) -> Result<()>
pub async fn wait_for_url(&self, pattern: &str, timeout_ms: u64) -> Result<()>
Wait for a URL pattern (substring match).
Sourcepub async fn wait_for_idle(&self, timeout_ms: u64) -> Result<()>
pub async fn wait_for_idle(&self, timeout_ms: u64) -> Result<()>
Wait for network activity to settle.
Sourcepub async fn eval<T: DeserializeOwned>(&self, js: &str) -> Result<T>
pub async fn eval<T: DeserializeOwned>(&self, js: &str) -> Result<T>
Evaluate JavaScript and return the result.
Sourcepub async fn extract<T: DeserializeOwned>(
&self,
js_expression: &str,
) -> Result<T>
pub async fn extract<T: DeserializeOwned>( &self, js_expression: &str, ) -> Result<T>
Extract structured data from the page using a JS expression that returns JSON.
Example:
let titles: Vec<String> = session.extract(
"Array.from(document.querySelectorAll('h2')).map(h => h.textContent.trim())"
).await?;Sourcepub async fn spa_info(&self) -> Result<SpaRouterInfo>
pub async fn spa_info(&self) -> Result<SpaRouterInfo>
Detect the SPA router type and current route state.
Navigate the SPA to a new path without page reload. Automatically detects the router type and uses the appropriate navigation method. Clears element cache since the DOM will change.
Sourcepub async fn history_go(&mut self, delta: i32) -> Result<()>
pub async fn history_go(&mut self, delta: i32) -> Result<()>
Navigate browser history by delta steps. delta = -1 goes back, delta = 1 goes forward. Clears element cache since the DOM will change.