browser_use/tools/
navigate.rs

1use crate::error::Result;
2use crate::tools::snapshot::{RenderMode, render_aria_tree};
3use crate::tools::utils::normalize_url;
4use crate::tools::{Tool, ToolContext, ToolResult};
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// Parameters for the navigate tool
9#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
10pub struct NavigateParams {
11    /// URL to navigate to
12    pub url: String,
13
14    /// Wait for navigation to complete (default: true)
15    #[serde(default = "default_wait")]
16    pub wait_for_load: bool,
17}
18
19fn default_wait() -> bool {
20    true
21}
22
23/// Tool for navigating to a URL
24#[derive(Default)]
25pub struct NavigateTool;
26
27impl Tool for NavigateTool {
28    type Params = NavigateParams;
29
30    fn name(&self) -> &str {
31        "navigate"
32    }
33
34    fn execute_typed(
35        &self,
36        params: NavigateParams,
37        context: &mut ToolContext,
38    ) -> Result<ToolResult> {
39        // Normalize the URL
40        let normalized_url = normalize_url(&params.url);
41
42        // Navigate to normalized URL
43        context.session.navigate(&normalized_url)?;
44
45        // Wait for navigation if requested
46        if params.wait_for_load {
47            context.session.wait_for_navigation()?;
48        }
49
50        let snapshot = {
51            let dom = context.get_dom()?;
52            render_aria_tree(&dom.root, RenderMode::Ai, None)
53        };
54
55        Ok(ToolResult::success_with(serde_json::json!({
56            "snapshot": snapshot
57        })))
58    }
59}