Create intelligent agents that can interact with websites and automate browser tasks using natural language instructions
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:
Copy
Ask AI
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,)