browser_use/tools/
screenshot.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 ScreenshotParams {
8    /// Path to save the screenshot
9    pub path: String,
10
11    /// Capture full page (default: false)
12    #[serde(default)]
13    pub full_page: bool,
14}
15
16#[derive(Default)]
17pub struct ScreenshotTool;
18
19impl Tool for ScreenshotTool {
20    type Params = ScreenshotParams;
21
22    fn name(&self) -> &str {
23        "screenshot"
24    }
25
26    fn execute_typed(
27        &self,
28        params: ScreenshotParams,
29        context: &mut ToolContext,
30    ) -> Result<ToolResult> {
31        let screenshot_data = context
32            .session
33            .tab()?
34            .capture_screenshot(
35                headless_chrome::protocol::cdp::Page::CaptureScreenshotFormatOption::Png,
36                None,
37                None,
38                params.full_page,
39            )
40            .map_err(|e| BrowserError::ScreenshotFailed(e.to_string()))?;
41
42        std::fs::write(&params.path, &screenshot_data).map_err(|e| {
43            BrowserError::ScreenshotFailed(format!("Failed to save screenshot: {}", e))
44        })?;
45
46        Ok(ToolResult::success_with(serde_json::json!({
47            "path": params.path,
48            "size_bytes": screenshot_data.len(),
49            "full_page": params.full_page
50        })))
51    }
52}