为智能体应用添加浏览器操作能力

December 25, 2025

需要与现实世界交互的 AI 智能体,越来越离不开浏览器自动化,例如搜索信息、填写表单、提取数据以及在 Web 应用中浏览。本指南介绍怎样把浏览器能力加入智能体应用。

浏览器智能体方案概览

多种框架提供面向 AI 智能体的浏览器自动化:

方案框架浏览器引擎主要特性
MultimodalWebSurferAutoGenPlaywright/Chromium标注交互目标的截图、内置工具
Browser Use兼容 LangChainPlaywrightDOM 提取、动作链
Playwright MCPModel Context ProtocolPlaywright标准化工具接口
Puppeteer独立工具Chrome DevTools底层控制
Selenium独立工具WebDriver跨浏览器支持

本文以 AutoGen 的 MultimodalWebSurfer 为例,其基本思路也适用于其他方案。


AutoGen MultimodalWebSurfer

AutoGen 的 MultimodalWebSurfer 是能自主浏览网页的多模态智能体,可以搜索、浏览页面、点击链接、填写表单和提取信息。

安装

pip install "autogen-ext[web-surfer]"

快速开始

import asyncio
from autogen_agentchat.ui import Console
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.agents.web_surfer import MultimodalWebSurfer


async def main() -> None:
    web_surfer_agent = MultimodalWebSurfer(
        name="MultimodalWebSurfer",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-2024-08-06"),
    )

    agent_team = RoundRobinGroupChat([web_surfer_agent], max_turns=3)

    stream = agent_team.run_stream(task="Navigate to the AutoGen readme on GitHub.")
    await Console(stream)

    # Important: Close the browser when done
    await web_surfer_agent.close()


asyncio.run(main())

工作原理

  1. 初始化浏览器:首次调用时 Playwright 启动 Chromium,后续调用复用浏览器。
  2. 截图与分析:截取页面,提取可交互元素,创建用边界框标记目标的 set-of-mark(SOM)图像。
  3. 模型决策:把 SOM 截图、消息历史以及可用工具发送给多模态模型。
  4. 执行操作:通过 PlaywrightController 执行工具调用。
  5. 生成响应:返回截图、页面元数据、操作描述和网页文本。

配置

web_surfer = MultimodalWebSurfer(
    name="WebSurfer",
    model_client=model_client,

    # Browser
    headless=True,                       # No visible window
    start_page="https://www.bing.com/",  # Initial page
    browser_channel=None,                # e.g., "chrome"
    browser_data_dir=None,               # Persistent data

    # Viewport (default: 1440x900)
    to_resize_viewport=True,

    # Features
    downloads_folder=None,     # Save downloads here
    animate_actions=False,     # Visual cursor animations
    to_save_screenshots=False, # Save screenshots to disk
    use_ocr=False,             # OCR for text extraction

    # Debug
    debug_dir=None,

    # Advanced: existing Playwright instance
    playwright=None,
    context=None,
)

PlaywrightController API

PlaywrightController 负责与浏览器交互:

导航

  • visit_page(page, url):访问 URL
  • back(page):后退
  • page_down(page) / page_up(page):滚动视口

交互

  • click_id(page, identifier):点击元素
  • hover_id(page, identifier):悬停于元素上
  • fill_id(page, identifier, value, press_enter=True):填写表单
  • scroll_id(page, identifier, direction):在元素内滚动

提取内容

  • get_webpage_text(page, n_lines=50):获取页面文本
  • get_visible_text(page):获取视口中的文本
  • get_page_metadata(page):获取标题、URL 等元数据

检测元素

  • get_interactive_rects(page):获取可交互元素及其边界框
  • get_focused_rect_id(page):获取当前焦点元素的 ID

实用工具

  • sleep(page, duration):等待,例如等待页面加载

Windows 兼容性

import sys
import asyncio

if sys.platform == "win32":
    asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

模型要求

模型必须具备:

  • 多模态能力:能处理截图
  • 函数调用能力:能执行浏览器操作

推荐 GPT-4o(gpt-4o-2024-08-06 或更高版本)。


安全注意事项

浏览器智能体会与真实网站交互,因此会面临一些风险:

风险

  • 智能体可能接受 Cookie 协议或点击意料之外的按钮。
  • 恶意网页可能发起提示注入。
  • 智能体可能无意间暴露凭据或敏感数据。

缓解措施

  1. 在沙箱或容器环境中运行。
  2. 为允许访问的域名设置白名单。
  3. 实时监控智能体的操作。
  4. 不向智能体提供敏感凭据。
  5. 尽可能使用只读模式。

示例:研究智能体

import asyncio
from autogen_agentchat.ui import Console
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.agents.web_surfer import MultimodalWebSurfer


async def research_topic(topic: str) -> None:
    web_surfer = MultimodalWebSurfer(
        name="Researcher",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-2024-08-06"),
        headless=True,
        to_save_screenshots=True,
        debug_dir="./research_debug",
    )

    team = RoundRobinGroupChat([web_surfer], max_turns=5)

    task = f"""
    Research: {topic}
    1. Search on Bing
    2. Visit 2-3 relevant pages
    3. Summarize key findings
    """

    stream = team.run_stream(task=task)
    await Console(stream)
    await web_surfer.close()


if __name__ == "__main__":
    asyncio.run(research_topic("AutoGen multi-agent framework"))

参考资料

  1. AutoGen MultimodalWebSurfer Documentation. (n.d.). Microsoft. 检索于 2025 年 12 月 25 日,来源: https://microsoft.github.io/autogen/stable/reference/python/autogen_ext.agents.web_surfer.html

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-12-25). 为智能体应用添加浏览器操作能力. https://caiborui.com/blog/2025-12-25-en-autogen-multimodal-web-surfer-browser-agent-zhtrans

@misc{borui2025-43ea5e98,
  author = {borui},
  title = {{为智能体应用添加浏览器操作能力}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-12-25-en-autogen-multimodal-web-surfer-browser-agent-zhtrans}
}