

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 开始使用 AgentCore 浏览器
<a name="browser-quickstart"></a>

AgentCore 浏览器使您的代理能够通过托管的 Chrome 浏览器与网页进行交互。在本指南中，你将在不到 5 分钟的时间内创建一个 AI 代理来浏览网站并提取信息。

**Topics**
+ [先决条件](#browser-prerequisites)
+ [第 1 步：安装依赖项](#browser-strands-install)
+ [第 2 步：创建您的代理](#browser-strands-create)
+ [第 3 步：运行代理](#browser-strands-run)
+ [第 4 步：实时查看浏览器会话](#browser-strands-live-view)
+ [查找资源](#browser-find-resources)
+ [后续步骤](#browser-next-steps)
+ [在 Nova Act AgentCore 中使用浏览器](browser-quickstart-nova-act.md)
+ [将 AgentCore 浏览器与 Playwright 一起](browser-quickstart-playwright.md)

## 先决条件
<a name="browser-prerequisites"></a>

在开始之前，请确保您有：
+ Python 3.10 或更新版本（[python.org/downloads](https://www.python.org/downloads/))
+ Boto3 已安装（[Boto3 文档）](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)
+  Amazon 已配置凭据 — 使用以下方式进行验证：

  ```
  aws sts get-caller-identity
  ```

  如果此命令失败，请参阅 C Amazon LI 文档[中的](https://docs.amazonaws.cn/cli/latest/userguide/cli-configure-files.html)配置和凭据文件设置。
+ 亚马逊 Bedrock 控制台[中](https://docs.amazonaws.cn/bedrock/latest/userguide/model-access-modify.html)启用 Anthropic Claude Sonnet 4.0 模型访问权限
+  Amazon 亚马逊 Bedrock 可用 AgentCore 的地区（参见[支持的 Amazon 区域](agentcore-regions.md)）

 **IAM 权限** 

将此政策附加到您的 IAM 身份。在 IAM 控制台中，找到您的用户或角色，选择**添加权限 ** > ** 创建内联策略**，切换到 JSON 视图，然后粘贴：

```
{
"Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "BedrockAgentCoreBrowserFullAccess",
            "Effect": "Allow",
            "Action": [
                "bedrock-agentcore:CreateBrowser",
                "bedrock-agentcore:ListBrowsers",
                "bedrock-agentcore:GetBrowser",
                "bedrock-agentcore:DeleteBrowser",
                "bedrock-agentcore:StartBrowserSession",
                "bedrock-agentcore:ListBrowserSessions",
                "bedrock-agentcore:GetBrowserSession",
                "bedrock-agentcore:StopBrowserSession",
                "bedrock-agentcore:UpdateBrowserStream",
                "bedrock-agentcore:ConnectBrowserAutomationStream",
                "bedrock-agentcore:ConnectBrowserLiveViewStream"
            ],
            "Resource": "arn:aws:bedrock-agentcore:<Region>:++<account_id>++:browser/*"
        },
        {
            "Sid": "BedrockModelAccess",
            "Effect": "Allow",
            "Action": [
                "bedrock:InvokeModel",
                "bedrock:InvokeModelWithResponseStream"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
```

**注意**  
`<Region>`替换为您的实际 Amazon 地区`<account_id>`和您的 Amazon 账户 ID。

## 第 1 步：安装依赖项
<a name="browser-strands-install"></a>

```
pip install bedrock-agentcore strands-agents strands-agents-tools playwright nest-asyncio
```

## 第 2 步：创建您的代理
<a name="browser-strands-create"></a>

创建一个名为的文件`browser_agent.py`：

**注意**  
`<Region>`替换为您的 Amazon 地区（例如，`us-west-2`）。

```
from strands import Agent
from strands_tools.browser import AgentCoreBrowser

# Initialize the Browser tool
browser_tool = AgentCoreBrowser(region="<Region>")

# Create an agent with the Browser tool
agent = Agent(tools=[browser_tool.browser])

# Test the agent with a web search prompt
prompt = "what are the services offered by Bedrock AgentCore? Use the documentation link if needed: https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/what-is-bedrock-agentcore.html"
print(f"\n\nPrompt: {prompt}\n\n")

response = agent(prompt)
print("\n\nAgent Response:")
print(response.message["content"][0]["text"])
```

## 第 3 步：运行代理
<a name="browser-strands-run"></a>

```
python browser_agent.py
```

您应该看到代理浏览网站并返回有关 AgentCore 服务的详细信息。如果您遇到错误，请验证您的 IAM 权限、模型访问权限和 Amazon 证书。

## 第 4 步：实时查看浏览器会话
<a name="browser-strands-live-view"></a>

当您的代理运行时，您可以通过 Amazon 控制台实时观看：

1. 打开[AgentCore 浏览器控制台 ](https://us-west-2.console.aws.amazon.com/bedrock-agentcore/builtInTools) 

1. 导航到左侧导航栏**中的**Built-in 工具

1. 选择浏览器工具（例如`AgentCore Browser Tool`，或您的自定义浏览器）

1. 在**浏览器会话**部分中，找到状态为 “**就绪” 的活动会话 ** 

1. 在 “**实时预览/录制” ** 列中，单击提供的 “查看实时会话” 网址

实时视图提供带有交互式控件的实时视频流，以接管自动化或释放控制权。

## 查找资源
<a name="browser-find-resources"></a>

使用 AgentCore 浏览器后，在 Amazon 控制台中查看您的资源：


| \# | 资源 | 位置 | 
| --- | --- | --- | 
| 1 | 实时预览 | 浏览器控制台 > 工具名称 > ** 查看实时会话 **  | 
| 2 | 会话录制和重播 | 浏览器控制台 > 工具名称 > ** 查看录像 **  | 
| 3 | 浏览器日志 |  **CloudWatch**> ** 日志组 ** > `/aws/bedrock-agentcore/browser/`  | 
| 4 | 录制文件 |  **S3 ** > 您的存储桶 > `browser-recordings/` 前缀 | 
| 5 | 自定义浏览器 |  **AgentCore 控制台 ** > ** Built-in 工具 ** > 您的自定义浏览器 | 
| 6 | IAM 角色 |  **IAM ** ** > ** 角色 > 搜索您的执行角色 | 

## 后续步骤
<a name="browser-next-steps"></a>

现在您的 AgentCore 浏览器可以正常工作了，请探索以下高级功能：
+  [会话录制和重播 ](browser-session-recording.md)-录制和重放会话以进行调试
+  [在 Nova Act AgentCore 中使用浏览器](browser-quickstart-nova-act.md)以及在 Playwright AgentCore 中[使用浏览器 ](browser-quickstart-playwright.md)-使用 Nova Act 或 Playwright 等其他框架
+  [基础知识 ](browser-resource-session-management.md)-了解 API 操作和自定义浏览器