View a markdown version of this page

Playwright での AgentCore ブラウザの使用 - Amazon Bedrock AgentCore

Playwright での AgentCore ブラウザの使用

ブラウザツールで Playwright 自動化フレームワークを使用できます。

ステップ 1: 依存関係をインストールする

プロジェクトフォルダを作成し (以前に作成していない場合)、必要なパッケージをインストールします。

mkdir agentcore-browser-quickstart cd agentcore-browser-quickstart python3 -m venv .venv source .venv/bin/activate
注記

Windows では、以下を使用します。 .venv\Scripts\activate

必要なパッケージをインストールします。

pip install bedrock-agentcore playwright boto3 nest-asyncio

これらのパッケージは以下を提供します。

  • bedrock-agentcore : AgentCore Browser を含む SDK for Amazon Bedrock AgentCore ツール

  • playwright : ブラウザ自動化用の Python ライブラリ

  • boto3 : AWS サービスを作成、設定、管理するための AWS SDK for Python (Boto3)

  • nest-asyncio : 既存のイベントループ内で非同期イベントループの実行を許可します

ステップ 2: Playwright でブラウザを制御する

ブラウザは、エージェントフレームワークまたは LLM なしで直接使用できます。これは、ブラウザの自動化をプログラムで制御する場合に便利です。Amazon Bedrock AgentCore は、ブラウザの自動化のために Playwright との統合を提供します。

非同期 Playwright の例

という名前のファイルを作成しdirect_browser_playwright.py、次のコードを追加します。

from playwright.async_api import async_playwright, Playwright, BrowserType from bedrock_agentcore.tools.browser_client import browser_session import asyncio async def run(playwright: Playwright): # Create and maintain a browser session with browser_session('us-west-2') as client: # Get WebSocket URL and authentication headers ws_url, headers = client.generate_ws_headers() # Connect to the remote browser chromium: BrowserType = playwright.chromium browser = await chromium.connect_over_cdp( ws_url, headers=headers ) # Get the browser context and page context = browser.contexts[0] page = context.pages[0] try: # Navigate to a website await page.goto("https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/what-is-bedrock-agentcore.html") # Print the page title title = await page.title() print(f"Page title: {title}") # Keep the session alive for 2 minutes to allow viewing print("\n\nBrowser session is active. Check the AWS Console for live view.") await asyncio.sleep(120) finally: # Clean up resources await page.close() await browser.close() async def main(): async with async_playwright() as playwright: await run(playwright) # Run the async function if __name__ == "__main__": asyncio.run(main())

Playwright をライブビューと同期する例

または、統合ライブビューサーバーで同期 API を使用することもできます。

from playwright.sync_api import sync_playwright, Playwright, BrowserType from bedrock_agentcore.tools.browser_client import browser_session from browser_viewer import BrowserViewerServer import time def run(playwright: Playwright): # Create the browser session and keep it alive with browser_session('us-west-2') as client: ws_url, headers = client.generate_ws_headers() # Start viewer server viewer = BrowserViewerServer(client, port=8005) viewer_url = viewer.start(open_browser=True) # Connect using headers chromium: BrowserType = playwright.chromium browser = chromium.connect_over_cdp( ws_url, headers=headers ) context = browser.contexts[0] page = context.pages[0] try: page.goto("https://amazon.com/") print(page.title()) time.sleep(120) finally: page.close() browser.close() with sync_playwright() as playwright: run(playwright)

スクリプトを実行する

次のいずれかのスクリプトを実行します。

python direct_browser_playwright.py

予想される出力

ページタイトルが出力されます (例: Page title: What is Amazon Bedrock AgentCore? - Amazon Bedrock AgentCore )。このスクリプトは、ブラウザセッションを 2 分間アクティブにしてから閉じます。

両方の例:

  • Amazon Bedrock AgentCore Browser を使用してマネージドブラウザセッションを作成する

  • Playwright の Chrome DevTools Protocol (CDP) を使用してリモート Chrome ブラウザに接続する

  • AgentCore ドキュメントに移動し、ページタイトルを出力する

  • セッションを 2 分間存続させ、 AWS コンソールで表示できるようにします。

  • ブラウザを適切に閉じ、リソースをクリーンアップする