本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
开始使用 AgentCore 浏览器
AgentCore 浏览器使您的代理能够通过托管的 Chrome 浏览器与网页进行交互。在本指南中,你将在不到 5 分钟的时间内创建一个 AI 代理来浏览网站并提取信息。
主题
先决条件
在开始之前,请确保您有:
-
Python 3.10 或更新版本(python.org/downloads
) -
Boto3 已安装(Boto3 文档)
-
AWS 已配置凭据 — 使用以下方式进行验证:
aws sts get-caller-identity如果此命令失败,请参阅 C AWS LI 文档中的配置和凭据文件设置。
-
亚马逊 Bedrock 控制台中启用 Anthropic Claude Sonnet 4.0 模型访问权限
-
AWS 亚马逊 Bedrock 可用 AgentCore 的地区(参见支持的 AWS 区域)
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>替换为您的实际 AWS 地区<account_id>和您的 AWS 账户 ID。
第 1 步:安装依赖项
pip install bedrock-agentcore strands-agents strands-agents-tools playwright nest-asyncio
第 2 步:创建您的代理
创建一个名为的文件browser_agent.py:
注意
<Region>替换为您的 AWS 地区(例如,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 步:运行代理
python browser_agent.py
您应该看到代理浏览网站并返回有关 AgentCore 服务的详细信息。如果您遇到错误,请验证您的 IAM 权限、模型访问权限和 AWS 证书。
第 4 步:实时查看浏览器会话
当您的代理运行时,您可以通过 AWS 控制台实时观看:
-
导航到左侧导航栏中的Built-in 工具
-
选择浏览器工具(例如
AgentCore Browser Tool,或您的自定义浏览器) -
在浏览器会话部分中,找到状态为 “就绪” 的活动会话
-
在 “实时预览/录制” 列中,单击提供的 “查看实时会话” 网址
实时视图提供带有交互式控件的实时视频流,以接管自动化或释放控制权。
查找资源
使用 AgentCore 浏览器后,在 AWS 控制台中查看您的资源:
| # | 资源 | 位置 |
|---|---|---|
|
1 |
实时预览 |
浏览器控制台 > 工具名称 > 查看实时会话 |
|
2 |
会话录制和重播 |
浏览器控制台 > 工具名称 > 查看录像 |
|
3 |
浏览器日志 |
CloudWatch> 日志组 > |
|
4 |
录制文件 |
S3 > 您的存储桶 > |
|
5 |
自定义浏览器 |
AgentCore 控制台 > Built-in 工具 > 您的自定义浏览器 |
|
6 |
IAM 角色 |
IAM > 角色 > 搜索您的执行角色 |
后续步骤
现在您的 AgentCore 浏览器可以正常工作了,请探索以下高级功能:
-
会话录制和重播 -录制和重放会话以进行调试
-
在 Nova Act AgentCore 中使用浏览器以及在 Playwright AgentCore 中使用浏览器 -使用 Nova Act 或 Playwright 等其他框架
-
基础知识 -了解 API 操作和自定义浏览器