> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Browser Use 集成

[BrowserUse](https://github.com/browser-use/browser-use) 是一个强大的 AI 浏览器 agent。结合 PPIO Sandbox 提供的隔离沙箱环境,你可以构建高并发、多任务的浏览器 AI agent。

本文档详细介绍如何基于 PPIO Sandbox 服务运行 BrowserUse 项目。

本文档使用 PPIO 发布的 `browser-chromium` Sandbox Template。

### 1. 获取 PPIO API Key

### 2. 安装依赖

安装所需的 Python 包:

```bash Python theme={null}
pip install browser-use
```

### 3. 示例代码

开始之前,你需要先配置必要的环境变量:

```bash Bash theme={null}
export PPIO_API_KEY=""
export LLM_API_KEY=""
export LLM_BASE_URL=https://api.ppinfra.com/v3/openai
export LLM_MODEL=""
```

保存以下代码:

**agent.py**

```python theme={null}
import asyncio
import base64
import os
import time

import dotenv

dotenv.load_dotenv(override=True)

from browser_use import Agent, BrowserSession
from browser_use.llm import ChatOpenAI
from ppio_sandbox import PPIO

ppio = PPIO()

async def screenshot(agent: Agent):
    # 截图函数
    print("Taking screenshot...")
    page = await agent.browser_session.get_current_page()
    screenshot_bytes = await page.screenshot(format='png')
    # screenshot 方法返回图片的二进制数据，需将其保存为 PNG 文件
    screenshots_dir = os.path.join(".", "screenshots")
    os.makedirs(screenshots_dir, exist_ok=True)
    screenshot_path = os.path.join(screenshots_dir, f"{time.time()}.png")
    if isinstance(screenshot_bytes, str):
        screenshot_data = base64.b64decode(screenshot_bytes)
    else:
        screenshot_data = screenshot_bytes
    with open(screenshot_path, "wb") as f:
        f.write(screenshot_data)
    print(f"Screenshot saved to {screenshot_path}")

async def main():
    # 创建 PPIO Sandbox 实例
    sandbox = ppio.sandbox.create(
        "browser-chromium",  # This template contains chromium browser and exposes port 9223 for remote connection
        timeout=600,  # Timeout in seconds
    )
    try:
        # 从 Sandbox 获取 Chrome 调试端口地址
        host = sandbox.get_host(9223)  # Get sandbox port 9223 address
        cdp_url = f"https://{host}"
        print(f"Chrome Debug Protocol URL: {cdp_url}")

        # 等待 Chrome 和反向代理就绪
        print("Waiting for Chrome and reverse proxy to be ready...")
        import requests
        max_retries = 20
        for i in range(max_retries):
            try:
                response = requests.get(f"{cdp_url}/json/version", verify=False, timeout=5)
                if response.status_code == 200:
                    data = response.json()
                    if 'webSocketDebuggerUrl' in data:
                        print(f"✅ CDP endpoint ready: {data.get('Browser', 'Unknown')}")
                        print(f"WebSocket URL: {data['webSocketDebuggerUrl']}")
                        break
                    else:
                        print(f"Attempt {i+1}/{max_retries}: Response missing webSocketDebuggerUrl")
                        print(f"Response data: {data}")
                else:
                    print(f"Attempt {i+1}/{max_retries}: HTTP {response.status_code}")
            except Exception as e:
                print(f"Attempt {i+1}/{max_retries}: {type(e).__name__}: {e}")

            if i == max_retries - 1:
                print("❌ Failed to connect to CDP endpoint after all retries")
                print("Checking startup script logs...")
                logs = sandbox.commands.run("tail -50 /tmp/*.log 2>/dev/null || echo 'No logs found'")
                print(f"Logs: {logs}")
                raise RuntimeError("CDP endpoint not ready")

            time.sleep(2)
        # 创建 BrowserUse 会话
        browser_session = BrowserSession(cdp_url=cdp_url)
        await browser_session.start()
        print("BrowserUse session created successfully")
        # 创建 AI Agent
        agent = Agent(
            task="Go to ppio.com and tell me what you see",
            llm=ChatOpenAI(
                api_key=os.getenv("LLM_API_KEY"),
                base_url=os.getenv("LLM_BASE_URL"),
                model=os.getenv("LLM_MODEL"),
                temperature=1
            ),
            browser_session=browser_session,
        )
        # 运行 Agent 任务
        print("Starting Agent task execution...")
        await agent.run(
            on_step_end=screenshot,  # Take screenshot after each step
        )
        # 关闭浏览器会话
        await browser_session.kill()
        print("Task execution completed")
    finally:
        # 清理 Sandbox 资源
        sandbox.kill()
        print("Sandbox resources cleaned up")

if __name__ == "__main__":
    asyncio.run(main())
```

### 4. 运行 Agent

安装完依赖并设置好环境变量后,即可运行示例代码。如果一切顺利,你会在终端中看到如下输出。Browser-use 正在你的 Sandbox 内的远程浏览器中执行任务。

<img src="https://mintcdn.com/ppinfra/nkBi88UBzmHnrrPQ/images/sandbox/browser-use-screenshot-1.png?fit=max&auto=format&n=nkBi88UBzmHnrrPQ&q=85&s=8c2fe10794dfeb8ed72069c0ddce1b49" alt="运行输出" width="4336" height="1236" data-path="images/sandbox/browser-use-screenshot-1.png" />

它会生成如下的截图:

<img src="https://mintcdn.com/ppinfra/nkBi88UBzmHnrrPQ/images/sandbox/browser-use-screenshot-2.png?fit=max&auto=format&n=nkBi88UBzmHnrrPQ&q=85&s=28299d64385a2b58d629d1106e93a9b7" alt="生成的截图" width="780" height="441" data-path="images/sandbox/browser-use-screenshot-2.png" />


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.