browser_use/tools/
input.rs

1use crate::error::{BrowserError, Result};
2use crate::tools::{Tool, ToolContext, ToolResult};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
7pub struct InputParams {
8    /// CSS selector for the input element
9    pub selector: String,
10
11    /// Text to type into the element
12    pub text: String,
13
14    /// Clear existing content first (default: false)
15    #[serde(default)]
16    pub clear: bool,
17}
18
19#[derive(Default)]
20pub struct InputTool;
21
22impl Tool for InputTool {
23    type Params = InputParams;
24
25    fn name(&self) -> &str {
26        "input"
27    }
28
29    fn execute_typed(&self, params: InputParams, context: &mut ToolContext) -> Result<ToolResult> {
30        let element = context.session.find_element(&params.selector)?;
31
32        if params.clear {
33            element.click().ok(); // Focus
34            // Clear with Ctrl+A and Delete
35            context.session.tab().press_key("End").ok();
36            for _ in 0..params.text.len() + 100 {
37                context.session.tab().press_key("Backspace").ok();
38            }
39        }
40
41        element
42            .type_into(&params.text)
43            .map_err(|e| BrowserError::ToolExecutionFailed {
44                tool: "input".to_string(),
45                reason: e.to_string(),
46            })?;
47
48        Ok(ToolResult::success_with(serde_json::json!({
49            "selector": params.selector,
50            "text_length": params.text.len()
51        })))
52    }
53}