|
| 1 | +--- |
| 2 | +title: How to use the Browser Automation tool with the Azure AI Foundry Agent Service |
| 3 | +titleSuffix: Azure AI Foundry |
| 4 | +description: Learn how to automate browser use and interact with websites using AI Agents. |
| 5 | +services: azure-ai-agent-service |
| 6 | +manager: nitinme |
| 7 | +ms.service: azure-ai-agent-service |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 07/29/2025 |
| 10 | +author: aahill |
| 11 | +ms.author: aahi |
| 12 | +ms.custom: azure-ai-agents |
| 13 | +--- |
| 14 | + |
| 15 | +# How to use the Browser Automation tool (preview) |
| 16 | + |
| 17 | +Use this article to find step-by-step instructions and code samples for using the Browser Automation tool in the Azure AI Foundry Agent Service. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +* The requirements in the [Browser Automation overview](./deep-research.md). |
| 22 | +* Your Azure AI Foundry Project endpoint. |
| 23 | + |
| 24 | + [!INCLUDE [endpoint-string-portal](../../includes/endpoint-string-portal.md)] |
| 25 | + |
| 26 | + Save this endpoint to an environment variable named `PROJECT_ENDPOINT`. |
| 27 | + |
| 28 | +* Your playwright connection ID. You can find it in the Azure AI Foundry portal by selecting **Management center** from the left navigation menu. Then select **Connected resources**. |
| 29 | + |
| 30 | + <!-- |
| 31 | + :::image type="content" source="../../media/tools/deep-research/bing-resource-name.png" alt-text="A screenshot showing the Playwright connection. " lightbox="../../media/tools/deep-research/bing-resource-name.png"::: |
| 32 | + --> |
| 33 | + Save this name to an environment variable named `PLAYWRIGHT_CONNECTION_NAME`. |
| 34 | + |
| 35 | +* [!INCLUDE [model-name-portal](../../includes/model-name-portal.md)] |
| 36 | + |
| 37 | + Save this name to an environment variable named `MODEL_DEPLOYMENT_NAME`. |
| 38 | + |
| 39 | +## Example code |
| 40 | + |
| 41 | +```python |
| 42 | +import os |
| 43 | +from azure.identity import DefaultAzureCredential |
| 44 | +from azure.ai.agents import AgentsClient |
| 45 | +from azure.ai.agents.models import MessageRole |
| 46 | +from azure.ai.projects import AIProjectClient |
| 47 | + |
| 48 | +project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set |
| 49 | + |
| 50 | +project_client = AIProjectClient( |
| 51 | + endpoint=project_endpoint, |
| 52 | + credential=DefaultAzureCredential() |
| 53 | +) |
| 54 | + |
| 55 | +playwright_connection = project_client.connections.get( |
| 56 | + name=os.environ["PLAYWRIGHT_CONNECTION_NAME"] |
| 57 | +) |
| 58 | +print(playwright_connection.id) |
| 59 | + |
| 60 | +with project_client: |
| 61 | + agent = project_client.agents.create_agent( |
| 62 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], |
| 63 | + name="my-agent", |
| 64 | + instructions="use the tool to respond", |
| 65 | + tools=[{ |
| 66 | + "type": "browser_automation", |
| 67 | + "browser_automation": { |
| 68 | + "connection": { |
| 69 | + "id": playwright_connection.id, |
| 70 | + } |
| 71 | + } |
| 72 | + }], |
| 73 | + ) |
| 74 | + |
| 75 | + print(f"Created agent, ID: {agent.id}") |
| 76 | + |
| 77 | + thread = project_client.agents.threads.create() |
| 78 | + print(f"Created thread and run, ID: {thread.id}") |
| 79 | + |
| 80 | + # Create message to thread |
| 81 | + message = project_client.agents.messages.create( |
| 82 | + thread_id=thread.id, |
| 83 | + role="user", |
| 84 | + content="something you want the tool to perform") |
| 85 | + print(f"Created message: {message['id']}") |
| 86 | + |
| 87 | + # Create and process an Agent run in thread with tools |
| 88 | + run = project_client.agents.runs.create_and_process( |
| 89 | + thread_id=thread.id, |
| 90 | + agent_id=agent.id, |
| 91 | + ) |
| 92 | + print(f"Run created, ID: {run.id}") |
| 93 | + print(f"Run finished with status: {run.status}") |
| 94 | + |
| 95 | + if run.status == "failed": |
| 96 | + print(f"Run failed: {run.last_error}") |
| 97 | + |
| 98 | + run_steps = project_client.agents.run_steps.list(thread_id=thread.id, run_id=run.id) |
| 99 | + for step in run_steps: |
| 100 | + print(step) |
| 101 | + print(f"Step {step['id']} status: {step['status']}") |
| 102 | + |
| 103 | + # Check if there are tool calls in the step details |
| 104 | + step_details = step.get("step_details", {}) |
| 105 | + tool_calls = step_details.get("tool_calls", []) |
| 106 | + |
| 107 | + if tool_calls: |
| 108 | + print(" Tool calls:") |
| 109 | + for call in tool_calls: |
| 110 | + print(f" Tool Call ID: {call.get('id')}") |
| 111 | + print(f" Type: {call.get('type')}") |
| 112 | + |
| 113 | + function_details = call.get("function", {}) |
| 114 | + if function_details: |
| 115 | + print(f" Function name: {function_details.get('name')}") |
| 116 | + print() # add an extra newline between steps |
| 117 | + |
| 118 | + # Delete the Agent when done |
| 119 | + project_client.agents.delete_agent(agent.id) |
| 120 | + print("Deleted agent") |
| 121 | + |
| 122 | + # Fetch and log all messages |
| 123 | + response_message = project_client.agents.messages.get_last_message_by_role(thread_id=thread.id, role=MessageRole.AGENT) |
| 124 | + if response_message: |
| 125 | + for text_message in response_message.text_messages: |
| 126 | + print(f"Agent response: {text_message.text.value}") |
| 127 | + for annotation in response_message.url_citation_annotations: |
| 128 | + print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})") |
| 129 | +``` |
0 commit comments