Use this file to discover all available pages before exploring further.
This guide walks you through setting up CrewAI with Browserbase to create agents that can perform web automation tasks using natural language instructions.
import osfrom crewai import Agent, Task, Crewfrom crewai_tools import StagehandToolfrom stagehand.schemas import AvailableModel# Get API keys from environmentbrowserbase_api_key = os.environ.get("BROWSERBASE_API_KEY")browserbase_project_id = os.environ.get("BROWSERBASE_PROJECT_ID")model_api_key = os.environ.get("OPENAI_API_KEY") # or ANTHROPIC_API_KEY# Initialize the StagehandToolstagehand_tool = StagehandTool( api_key=browserbase_api_key, project_id=browserbase_project_id, model_api_key=model_api_key, model_name=AvailableModel.GPT_4O, # or AvailableModel.CLAUDE_3_7_SONNET_LATEST)# Create an agent with the toolresearcher = Agent( role="Web Researcher", goal="Find and summarize information from websites", backstory="I'm an expert at finding information online.", verbose=True, tools=[stagehand_tool],)
# Create a task that uses the toolresearch_task = Task( description="Go to https://www.example.com and tell me what you see on the homepage.", agent=researcher,)# Run the crewcrew = Crew( agents=[researcher], tasks=[research_task], verbose=True,)try: result = crew.kickoff() print(result)finally: # Clean up resources stagehand_tool.close()
Customize the StagehandTool behavior with additional parameters:
stagehand_tool = StagehandTool( api_key=browserbase_api_key, project_id=browserbase_project_id, model_api_key=model_api_key, model_name=AvailableModel.CLAUDE_3_7_SONNET_LATEST, dom_settle_timeout_ms=5000, # Wait longer for DOM to settle headless=True, # Run browser in headless mode self_heal=True, # Attempt to recover from errors wait_for_captcha_solves=True, # Wait for CAPTCHA solving verbose=1, # Control logging verbosity (0-3))
form_task = Task( description=""" Submit a contact form: 1. Go to https://example.com/contact 2. Fill out the form with name 'John Doe', email 'john@example.com' 3. Submit and confirm success """, agent=researcher,)
extraction_task = Task( description=""" Extract product information: 1. Go to the products page 2. Extract all product names, prices, and descriptions 3. Format as structured data """, agent=researcher,)
navigation_task = Task( description=""" Navigate and analyze: 1. Start at homepage 2. Navigate to products section 3. Filter by 'Electronics' category 4. Find and extract details of highest-rated product """, agent=researcher,)
CrewAI Documentation
Dive into the CrewAI documentation to learn more about its capabilities and integrations.
Browserbase Documentation
Access the Browserbase documentation for comprehensive guides and resources.