Skip to main content

Overview

The StagehandTool 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
The StagehandTool wraps the Stagehand Python SDK to provide CrewAI agents with browser control capabilities through three core primitives:
  1. Act: Perform actions like clicking, typing, or navigating
  2. Extract: Extract structured data from web pages
  3. Observe: Identify and analyze elements on the page

Prerequisites

Before using this tool, ensure you have:
  1. A Browserbase account with API key and project ID
  2. An API key for an LLM (OpenAI or Anthropic Claude)
  3. The Stagehand Python SDK installed
Install the required dependency:

Usage

Basic Implementation

The StagehandTool can be implemented in two ways:
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

The act command type (default) enables webpage interactions like clicking buttons, filling forms, and navigation.

2. Extract Command

The extract command type retrieves structured data from webpages.

3. Observe Command

The observe command type identifies and analyzes webpage elements.

Configuration Options

Customize the StagehandTool behavior with these parameters:

Best Practices

  1. Be Specific: Provide detailed instructions for better results
  2. Choose Appropriate Command Type: Select the right command type for your task
  3. Use Selectors: Leverage CSS selectors to improve accuracy
  4. Break Down Complex Tasks: Split complex workflows into multiple tool calls
  5. 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: