Skip to main content

AgentPage

Struct AgentPage 

Source
pub struct AgentPage<'a> { /* private fields */ }
Expand description

Wraps a Page with agent-friendly observation and interaction methods.

The core loop is: observe() → read element_list()click(i) / fill(i, text).

Implementations§

Source§

impl<'a> AgentPage<'a>

Source

pub fn new(page: &'a Page) -> Self

Create an AgentPage wrapping an existing eoka Page.

Source

pub fn with_config(page: &'a Page, config: ObserveConfig) -> Self

Create with custom observation config.

Source

pub fn page(&self) -> &Page

Get a reference to the underlying Page.

Source

pub async fn observe(&mut self) -> Result<&[InteractiveElement]>

Snapshot the page: enumerate all interactive elements.

Source

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.

Source

pub fn added_element_list(&self, diff: &ObserveDiff) -> String

Compact text list of only the added elements from the last observe_diff().

Source

pub async fn screenshot(&mut self) -> Result<Vec<u8>>

Take an annotated screenshot with numbered boxes on each element. Calls observe() first if no elements have been enumerated yet.

Source

pub async fn screenshot_plain(&self) -> Result<Vec<u8>>

Take a plain screenshot without annotations.

Source

pub fn element_list(&self) -> String

Compact text list for LLM consumption. Each line: [index] <tag type="x"> "text" placeholder="y"

Source

pub fn get(&self, index: usize) -> Option<&InteractiveElement>

Get element info by index.

Source

pub fn elements(&self) -> &[InteractiveElement]

Get all observed elements.

Source

pub fn len(&self) -> usize

Number of observed elements.

Source

pub fn is_empty(&self) -> bool

Whether the element list is empty.

Source

pub fn find_by_text(&self, needle: &str) -> Option<usize>

Find first element whose text contains the given substring (case-insensitive). Returns the element index, or None.

Source

pub fn find_all_by_text(&self, needle: &str) -> Vec<usize>

Find all elements whose text contains the given substring (case-insensitive).

Source

pub async fn click(&self, index: usize) -> Result<()>

Click an element by its index.

Source

pub async fn try_click(&self, index: usize) -> Result<bool>

Try to click — returns Ok(false) if element is missing or not visible.

Source

pub async fn human_click(&self, index: usize) -> Result<()>

Human-like click by index.

Source

pub async fn fill(&self, index: usize, text: &str) -> Result<()>

Clear and type into an element by index.

Source

pub async fn human_fill(&self, index: usize, text: &str) -> Result<()>

Human-like fill by index.

Source

pub async fn focus(&self, index: usize) -> Result<()>

Focus an element by index.

Source

pub async fn select(&self, index: usize, value: &str) -> Result<()>

Select a dropdown option by index. value matches the option’s value or visible text.

Source

pub async fn options(&self, index: usize) -> Result<Vec<(String, String)>>

Get dropdown options for a select element. Returns vec of (value, text) pairs.

Source

pub async fn scroll_to(&self, index: usize) -> Result<()>

Scroll element at index into view.

Source

pub async fn goto(&mut self, url: &str) -> Result<()>

Navigate to a URL. Clears element list (call observe() after navigation).

Source

pub async fn back(&mut self) -> Result<()>

Go back in history. Clears element list (call observe() after navigation).

Source

pub async fn forward(&mut self) -> Result<()>

Go forward in history. Clears element list (call observe() after navigation).

Source

pub async fn reload(&mut self) -> Result<()>

Reload the page. Clears element list (call observe() after navigation).

Source

pub async fn url(&self) -> Result<String>

Get the current URL.

Source

pub async fn title(&self) -> Result<String>

Get the page title.

Source

pub async fn text(&self) -> Result<String>

Get visible text content of the page.

Source

pub async fn scroll_down(&self) -> Result<()>

Scroll down by approximately one viewport height.

Source

pub async fn scroll_up(&self) -> Result<()>

Scroll up by approximately one viewport height.

Source

pub async fn scroll_to_top(&self) -> Result<()>

Scroll to the top of the page.

Source

pub async fn scroll_to_bottom(&self) -> Result<()>

Scroll to the bottom of the page.

Source

pub async fn wait_for_text(&self, text: &str, timeout_ms: u64) -> Result<()>

Wait for text to appear on the page.

Source

pub async fn wait_for_url(&self, pattern: &str, timeout_ms: u64) -> Result<()>

Wait for a URL pattern (substring match).

Source

pub async fn wait_for_idle(&self, timeout_ms: u64) -> Result<()>

Wait for network activity to settle.

Source

pub async fn wait(&self, ms: u64)

Fixed delay in milliseconds.

Source

pub async fn eval<T: DeserializeOwned>(&self, js: &str) -> Result<T>

Evaluate JavaScript and return the result.

Source

pub async fn exec(&self, js: &str) -> Result<()>

Execute JavaScript (no return value).

Source

pub async fn press_key(&self, key: &str) -> Result<()>

Press a key (e.g. “Enter”, “Tab”, “Escape”, “ArrowDown”, “Backspace”).

Source

pub async fn submit(&self, index: usize) -> Result<()>

Focus element by index and press Enter (common for form submission).

Source

pub async fn hover(&self, index: usize) -> Result<()>

Hover over element by index (triggers hover states, tooltips, menus).

Source

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> = agent.extract(
    "Array.from(document.querySelectorAll('h2')).map(h => h.textContent.trim())"
).await?;
Source

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).

Source

pub async fn click_and_wait(&mut self, index: usize) -> Result<()>

Click an element and wait for page to stabilize.

Source

pub async fn fill_and_wait(&mut self, index: usize, text: &str) -> Result<()>

Fill an element and wait for page to stabilize.

Source

pub async fn select_and_wait(&mut self, index: usize, value: &str) -> Result<()>

Select an option and wait for page to stabilize.

Source

pub async fn spa_info(&self) -> Result<SpaRouterInfo>

Detect the SPA router type and current route state.

Source

pub async fn spa_navigate(&mut self, path: &str) -> Result<String>

Navigate the SPA to a new path without page reload. Automatically detects the router type and uses the appropriate navigation method. Clears element list since the DOM will change.

Source

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 list since the DOM will change.

Auto Trait Implementations§

§

impl<'a> Freeze for AgentPage<'a>

§

impl<'a> !RefUnwindSafe for AgentPage<'a>

§

impl<'a> Send for AgentPage<'a>

§

impl<'a> Sync for AgentPage<'a>

§

impl<'a> Unpin for AgentPage<'a>

§

impl<'a> UnsafeUnpin for AgentPage<'a>

§

impl<'a> !UnwindSafe for AgentPage<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more