Overview
TheStagehandTool
integrates the Stagehand framework with CrewAI, enabling agents to interact with websites and automate browser tasks using natural language instructions.
Overview
Stagehand is a powerful browser automation framework built by Browserbase that allows AI agents to:- Navigate to websites
- Click buttons, links, and other elements
- Fill in forms
- Extract data from web pages
- Observe and identify elements
- Perform complex workflows
- Act: Perform actions like clicking, typing, or navigating
- Extract: Extract structured data from web pages
- Observe: Identify and analyze elements on the page
Prerequisites
Before using this tool, ensure you have:- A Browserbase account with API key and project ID
- An API key for an LLM (OpenAI or Anthropic Claude)
- The Stagehand Python SDK installed
Usage
Basic Implementation
The StagehandTool can be implemented in two ways:1. Using Context Manager (Recommended)
The context manager approach is recommended as it ensures proper cleanup of resources even if exceptions occur.
2. Manual Resource Management
Command Types
The StagehandTool supports three different command types for specific web automation tasks:1. Act Command
Theact
command type (default) enables webpage interactions like clicking buttons, filling forms, and navigation.
2. Extract Command
Theextract
command type retrieves structured data from webpages.
3. Observe Command
Theobserve
command type identifies and analyzes webpage elements.
Configuration Options
Customize the StagehandTool behavior with these parameters:Best Practices
- Be Specific: Provide detailed instructions for better results
- Choose Appropriate Command Type: Select the right command type for your task
- Use Selectors: Leverage CSS selectors to improve accuracy
- Break Down Complex Tasks: Split complex workflows into multiple tool calls
- Implement Error Handling: Add error handling for potential issues
Troubleshooting
Common issues and solutions:- Session Issues: Verify API keys for both Browserbase and LLM provider
- Element Not Found: Increase
dom_settle_timeout_ms
for slower pages - Action Failures: Use
observe
to identify correct elements first - Incomplete Data: Refine instructions or provide specific selectors
Additional Resources
For questions about the CrewAI integration:- Join Stagehand’s Slack community
- Open an issue in the Stagehand repository
- Visit Stagehand documentation