AI agents can leverage browser sessions to complete tasks in the web. The ways to use the browser in CrewAI agent platform are:
As a Flexible Browser Tool: Enable the CrewAI agent to explore the web freely using a general-purpose browser tool.
As a Specific-Flow Tool Defined in CrewAI: Create custom tools by writing CrewAI code to define specific workflows tailored to your needs.
As a Specific-Flow Tool Defined in Anchor Browser: Define tools using the Anchor platform, which CrewAI agents can then use through the Official Anchor Tool.
Quick start - Use Anchor Browser as a flexible browser tool
You can connect your CrewAI agent directly to Anchor Browser, allowing it to use browser sessions for various tasks, leveraging the power of browser automation without the need for complex integration code. Below is a quick guide to setting up and using Anchor Browser as a tool for your CrewAI agent.
Code example - Use Anchor Browser as a flexible browser tool
Copy
Ask AI
import crewaifrom playwright.sync_api import sync_playwrightANCHOR_API_KEY = "YOUR_ANCHOR_API_KEY" # Replace with your actual API key# Register Anchor Browser as a tool in CrewAIclass AnchorBrowserTool(crewai.Tool): name = "AnchorBrowser" description = "Use Anchor Browser to perform web-based tasks." def __init__(self): super().__init__() def run(self, command): with sync_playwright() as p: browser = p.chromium.connect_over_cdp( f"wss://connect.anchorbrowser.io?apiKey={ANCHOR_API_KEY}" ) page = browser.new_page() page.goto(command['url']) # Perform specific actions as needed if command.get('action') == 'search': page.fill(command['search_box'], command['search_text']) page.click(command['search_button']) # Extract and return data if needed result = page.content() browser.close() return result# Add AnchorBrowserTool to CrewAI agentmy_agent = crewai.Agent(name="Web Agent")my_agent.add_tool(AnchorBrowserTool())# Use the agent to perform actions on the webresult = my_agent.act({ 'tool': 'AnchorBrowser', 'command': { 'url': 'https://example.com', 'action': 'search', 'search_box': 'input[name="q"]', 'search_text': 'Anchor Browser', 'search_button': 'button[type="submit"]' }})print(result)
Use Anchor Browser as a specific-flow tool defined in CrewAI
CrewAI can integrate closely with Anchor Browser to define tools for particular workflows. For example, if you need a customized process to interact with an authenticated application, you can create that flow as a reusable tool that CrewAI agents can use for automation.Here is an example of creating a specific workflow tool using Anchor Browser that can be utilized by CrewAI to automate targeted tasks.
Code example - Use Anchor Browser as a specific-flow tool defined in CrewAI
Copy
Ask AI
import crewaifrom playwright.sync_api import sync_playwrightANCHOR_API_KEY = "YOUR_ANCHOR_API_KEY" # Replace with your actual API key# Register Anchor Browser as a specific application tool in CrewAIclass AnchorSpecificTool(crewai.Tool): name = "SpecificAnchorTool" description = "Custom tool for interacting with a specific application." def __init__(self): super().__init__() def run(self, command): with sync_playwright() as p: # Connect to Anchor Browser session browser = p.chromium.connect_over_cdp( f"wss://connect.anchorbrowser.io?apiKey={ANCHOR_API_KEY}" ) page = browser.new_page() page.goto(command['url']) # Perform specific actions based on command if command.get('action') == 'extract': result = page.text_content(command['selector']) browser.close() return result browser.close()# Add custom AnchorBrowserTool to CrewAI agentmy_agent = crewai.Agent(name="Web Automation Agent")my_agent.add_tool(AnchorSpecificTool())# Use the agent to perform a specific taskresult = my_agent.act({ 'tool': 'SpecificAnchorTool', 'command': { 'url': 'https://example.com', 'action': 'extract', 'selector': '#data' }})print(result)