使用瀏覽器設定檔
瀏覽器設定檔可讓您在多個瀏覽器工作階段中保留和重複使用瀏覽器設定檔資料。瀏覽器設定檔存放工作階段資訊,包括 Cookie 和本機儲存。
例如,您可以在瀏覽器工作階段中對網站進行身分驗證一次,並將設定檔資料儲存至瀏覽器設定檔資源。當您使用儲存的設定檔啟動新的瀏覽器工作階段時,您的身分驗證狀態會保留,而且您仍保持登入狀態。這可讓客服人員在已驗證的網站上執行任務,而不需要手動介入。
概觀
Amazon Bedrock AgentCore 中的瀏覽器設定檔運作方式如下:
-
建立將在瀏覽器工作階段間重複使用的瀏覽器設定檔資源
-
啟動瀏覽器工作階段並執行填入 Cookie 和本機儲存的動作 (例如登入網站或將項目放入購物車)
-
將設定檔資料從目前瀏覽器工作階段儲存至瀏覽器設定檔資源
-
使用儲存的設定檔啟動新的瀏覽器工作階段,以還原先前的狀態
先決條件
使用瀏覽器設定檔之前,請確定您已:
-
完成一般瀏覽器先決條件
-
管理設定檔資源的 IAM 許可。將下列許可新增至您的 IAM 政策:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "BedrockAgentCoreBrowserProfileManagementAccess",
"Effect": "Allow",
"Action": [
"bedrock-agentcore:CreateBrowserProfile",
"bedrock-agentcore:ListBrowserProfiles",
"bedrock-agentcore:GetBrowserProfile",
"bedrock-agentcore:DeleteBrowserProfile"
],
"Resource": "arn:aws:bedrock-agentcore:<Region>:<account_id>:browser-profile/*"
}
]
}
-
儲存和載入設定檔的 IAM 許可。將下列許可新增至您的 IAM 政策:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "BedrockAgentCoreBrowserProfileUsageAccess",
"Effect": "Allow",
"Action": [
"bedrock-agentcore:StartBrowserSession",
"bedrock-agentcore:SaveBrowserSessionProfile"
],
"Resource": [
"arn:aws:bedrock-agentcore:<Region>:<account_id>:browser-profile/*",
"arn:aws:bedrock-agentcore:<Region>:<account_id>:browser-custom/*",
"arn:aws:bedrock-agentcore:<Region>:<account_id>:browser/*"
]
}
]
}
瀏覽器設定檔入門
本節會逐步引導您建立瀏覽器設定檔、將工作階段資料儲存至其中,以及在後續工作階段中重複使用設定檔。
步驟 1:建立瀏覽器設定檔
建立瀏覽器設定檔資源以存放工作階段資料。
範例
- AWS CLI
-
-
若要使用 CLI AWS 建立瀏覽器設定檔:
aws bedrock-agentcore-control create-browser-profile \
--region <Region> \
--name "profile_demo" \
--description "example profile"
- Boto3
-
-
若要使用適用於 Python 的 AWS SDK (Boto3) 建立瀏覽器設定檔:
import boto3
region = "us-west-2"
client = boto3.client('bedrock-agentcore-control', region_name=region)
response = client.create_browser_profile(
name="profile_demo",
description="example profile"
)
profile_id = response['profileId']
print(f"Created profile: {profile_id}")
- API
-
-
若要使用 API 建立瀏覽器設定檔:
awscurl -X PUT \
"https://bedrock-agentcore-control.<Region>.amazonaws.com/browser-profiles" \
-H "Content-Type: application/json" \
--service bedrock-agentcore-control \
--region <Region> \
-d '{
"name": "profile_demo",
"description": "example profile",
"clientToken": "FC21DD1F-FA7B-40EC-8D3A-12E029A1BFF3"
}'
步驟 2:啟動瀏覽器工作階段並執行動作
啟動瀏覽器工作階段並執行登入網站等動作。將擷取工作階段狀態,包括 Cookie 和本機儲存體。
範例
- AWS CLI
-
-
若要使用 CLI AWS 啟動瀏覽器工作階段:
aws bedrock-agentcore start-browser-session \
--region <Region> \
--browser-identifier "aws.browser.v1" \
--name "my-session" \
--session-timeout-seconds 3600
使用傳回的工作階段 ID 與瀏覽器互動,並執行所需的動作 (例如登入網站)。
- Boto3
-
-
若要使用 Boto3 啟動瀏覽器工作階段:
import boto3
region = "us-west-2"
client = boto3.client('bedrock-agentcore', region_name=region)
response = client.start_browser_session(
browserIdentifier="aws.browser.v1",
name="my-session",
sessionTimeoutSeconds=3600
)
session_id = response['sessionId']
print(f"Session ID: {session_id}")
# Use the session to perform actions (e.g., login to a website)
# ... your browser automation code here ...
- API
-
-
若要使用 API 啟動瀏覽器工作階段:
awscurl -X PUT \
"https://bedrock-agentcore.<Region>.amazonaws.com/browsers/aws.browser.v1/sessions/start" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
--service bedrock-agentcore \
--region <Region> \
-d '{
"name": "my-session",
"sessionTimeoutSeconds": 3600
}'
步驟 3:將工作階段儲存至設定檔
在瀏覽器工作階段中執行動作後,請將目前狀態儲存到瀏覽器設定檔。這會擷取 Cookie、本機儲存和其他工作階段資料。
範例
- AWS CLI
-
-
若要使用 CLI AWS 將瀏覽器工作階段儲存至設定檔:
aws bedrock-agentcore save-browser-session-profile \
--region <Region> \
--profile-identifier "<ProfileId>" \
--browser-identifier "aws.browser.v1" \
--session-id "<SessionId>"
- Boto3
-
-
若要使用 Boto3 將瀏覽器工作階段儲存至設定檔:
import boto3
region = "us-west-2"
client = boto3.client('bedrock-agentcore', region_name=region)
response = client.save_browser_session_profile(
profileIdentifier=profile_id, # From Step 1
browserIdentifier="aws.browser.v1",
sessionId=session_id # From Step 2
)
print(f"Profile saved successfully")
- API
-
-
若要使用 API 將瀏覽器工作階段儲存至設定檔:
awscurl -X PUT \
"https://bedrock-agentcore.<Region>.amazonaws.com/browser-profiles/<ProfileId>/save" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
--service bedrock-agentcore \
--region <Region> \
-d '{
"browserIdentifier": "aws.browser.v1",
"sessionId": "<SessionId>",
"clientToken": "52866F95-4468-4B6C-920C-09A00D36F04E"
}'
步驟 4:使用設定檔啟動新的工作階段
使用儲存的設定檔啟動新的瀏覽器工作階段。工作階段會以儲存的狀態初始化,包括身分驗證 Cookie 和本機儲存。
範例
- AWS CLI
-
-
若要使用 CLI AWS 以設定檔啟動瀏覽器工作階段:
aws bedrock-agentcore start-browser-session \
--region <Region> \
--browser-identifier "aws.browser.v1" \
--name "session-with-profile" \
--session-timeout-seconds 3600 \
--profile-configuration '{
"profileIdentifier":"<ProfileId>"
}'
- Boto3
-
-
若要使用 Boto3 以設定檔啟動瀏覽器工作階段:
import boto3
region = "us-west-2"
client = boto3.client('bedrock-agentcore', region_name=region)
response = client.start_browser_session(
browserIdentifier="aws.browser.v1",
name="session-with-profile",
sessionTimeoutSeconds=3600,
profileConfiguration={
"profileIdentifier": profile_id # From Step 1
}
)
new_session_id = response['sessionId']
print(f"New session started with profile: {new_session_id}")
print("Session is now authenticated with saved cookies and state")
- API
-
-
若要使用 API 以設定檔啟動瀏覽器工作階段:
awscurl -X PUT \
"https://bedrock-agentcore.<Region>.amazonaws.com/browsers/aws.browser.v1/sessions/start" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
--service bedrock-agentcore \
--region <Region> \
-d '{
"name": "session-with-profile",
"sessionTimeoutSeconds": 3600,
"profileConfiguration": {
"profileIdentifier": "<ProfileId>"
},
"clientToken": "8FAAFF7F-7803-4C40-A7BD-139C6D008646"
}'
瀏覽器設定檔操作
本節說明管理瀏覽器設定檔的其他操作。
列出瀏覽器設定檔
您可以列出帳戶中的所有瀏覽器設定檔。
範例
- AWS CLI
-
-
若要使用 CLI AWS 列出瀏覽器設定檔:
aws bedrock-agentcore-control list-browser-profiles \
--region <Region>
- Boto3
-
-
若要使用 Boto3 列出瀏覽器設定檔:
import boto3
region = "us-west-2"
client = boto3.client('bedrock-agentcore-control', region_name=region)
response = client.list_browser_profiles()
for profile in response.get('items', []):
print(f"Profile ID: {profile['profileId']}")
print(f"Name: {profile['name']}")
print(f"Status: {profile['status']}")
print("---")
- API
-
-
使用 API 列出瀏覽器設定檔:
awscurl -X POST \
"https://bedrock-agentcore-control.<Region>.amazonaws.com/browser-profiles/list" \
-H "Content-Type: application/json" \
--service bedrock-agentcore-control \
--region <Region>
取得瀏覽器設定檔詳細資訊
您可以擷取特定瀏覽器設定檔的詳細資訊。
範例
- AWS CLI
-
-
若要使用 CLI AWS 取得瀏覽器設定檔詳細資訊:
aws bedrock-agentcore-control get-browser-profile \
--region <Region> \
--profile-id "<ProfileId>"
- Boto3
-
-
若要使用 Boto3 取得瀏覽器設定檔詳細資訊:
import boto3
region = "us-west-2"
client = boto3.client('bedrock-agentcore-control', region_name=region)
response = client.get_browser_profile(
profileId=profile_id
)
print(f"Profile ID: {response['profileId']}")
print(f"Name: {response['name']}")
print(f"Description: {response.get('description', 'N/A')}")
print(f"Status: {response['status']}")
print(f"Created: {response['createdAt']}")
- API
-
-
若要使用 API 取得瀏覽器設定檔詳細資訊:
awscurl -X GET \
"https://bedrock-agentcore-control.<Region>.amazonaws.com/browser-profiles/<ProfileId>" \
-H "Content-Type: application/json" \
--service bedrock-agentcore-control \
--region <Region>
刪除瀏覽器設定檔
當您不再需要瀏覽器設定檔時,您可以將其刪除以釋放資源。
範例
- AWS CLI
-
-
若要使用 CLI AWS 刪除瀏覽器設定檔:
aws bedrock-agentcore-control delete-browser-profile \
--region <Region> \
--profile-id "<ProfileId>"
- Boto3
-
-
若要使用 Boto3 刪除瀏覽器設定檔:
import boto3
region = "us-west-2"
client = boto3.client('bedrock-agentcore-control', region_name=region)
response = client.delete_browser_profile(
profileId=profile_id
)
print(f"Profile deleted successfully")
- API
-
-
若要使用 API 刪除瀏覽器設定檔:
awscurl -X DELETE \
"https://bedrock-agentcore-control.<Region>.amazonaws.com/browser-profiles/<ProfileId>" \
-H "Content-Type: application/json" \
--service bedrock-agentcore-control \
--region <Region>
使用案例
瀏覽器設定檔適用於:
-
持久性身分驗證 :維護跨多個瀏覽器調用的登入工作階段,無需每次重新驗證
-
多步驟工作流程:執行跨越多個工作階段的複雜工作流程,同時保留狀態和內容 (在 Cookie 或本機儲存中)
-
自動化任務 :讓 AI 代理器在已驗證的網站上執行任務,無需手動介入
考量事項
-
設定檔持續性 :當您明確呼叫儲存操作時,會儲存設定檔資料。請務必先儲存設定檔,再終止工作階段以保留狀態。
-
工作階段隔離 :每個使用設定檔的瀏覽器工作階段都會獨立運作。一個工作階段所做的變更在其他並行工作階段中不可見。
-
設定檔載入 :已儲存的設定檔變更僅適用於新的工作階段。使用中的工作階段會從啟動時繼續使用設定檔狀態,而且不會反映工作階段啟動後對設定檔所做的更新。
-
安全性 :瀏覽器設定檔可能包含敏感資料,例如身分驗證 Cookie。確保有適當的 IAM 政策來限制對設定檔資源的存取。
-
設定檔更新 :當您將工作階段儲存到設定檔時,它會覆寫先前的設定檔資料。請仔細考慮您的工作流程,以避免遺失重要的狀態資訊。
-
Cookie 過期 :Cookie 由網站設定自己的過期時間。瀏覽器設定檔會保留 Cookie,但瀏覽器會根據過期日期自動移除過期的 Cookie。