mastra/Core Concepts

Browser

Browser automation with AgentBrowser and Stagehand

browserautomationstagehandweb

Browser

Browser automation with AgentBrowser and Stagehand.

Topics

Browser Overview

Browser automation enables agents to interact with web pages and web-based applications.

Capabilities

  • Navigate to URLs
  • Fill forms and click buttons
  • Extract data from pages
  • Take screenshots
  • Execute JavaScript
  • Handle multi-tab workflows

Browser Providers

  • AgentBrowser - Simple browser automation
  • Stagehand - AI-powered visual understanding
  • Playwright - Low-level browser control

Getting Started

const browser = await AgentBrowser.launch({
  headless: true,
  viewport: { width: 1920, height: 1080 },
});

const page = await browser.newPage();
await page.goto('https://example.com');

AgentBrowser

Browser automation for agents.

Creating a Browser Agent

import { AgentBrowser } from '@mastra/core';

const browserAgent = new AgentBrowser({
  name: 'web-researcher',
  instructions: 'You browse the web to find information',
  browser: {
    headless: true,
  },
});

Basic Operations

// Navigate to a page
await browserAgent.goto('https://example.com');

// Click an element
await browserAgent.click('button.submit');

// Fill a form
await browserAgent.fill('input[name="email"]', 'user@example.com');

// Extract content
const title = await browserAgent.text('h1');
const links = await browserAgent.links('a');

Agent Integration

const agent = mastra.getAgent('myAgent');

browserAgent.useAgent(agent);

await browserAgent.start();

Screenshot

const screenshot = await browserAgent.screenshot();
console.log(screenshot.url);

Stagehand

AI-powered browser automation with visual understanding.

Overview

Stagehand uses AI to understand page structure and elements, making it easier to automate complex web pages.

Getting Started

import { StagehandBrowser } from '@mastra/stagehand';

const browser = await StagehandBrowser.launch({
  headless: true,
  model: 'gpt-4-vision',
});

Natural Language Commands

const page = await browser.newPage();
await page.goto('https://example.com');

// Use natural language to interact
await page.act('Click the "Sign Up" button');
await page.act('Fill in the registration form with email and password');
await page.act('Submit the form');

Visual Understanding

// Stagehand understands page visually
const elements = await page.findElements({
  type: 'button',
  visible: true,
  containsText: 'Submit',
});

Extract Data

const data = await page.extract({
  title: 'h1.product-title',
  price: 'span.price',
  description: 'div.description',
});

Limitations

Stagehand currently has beta support. For production use, consider AgentBrowser or Playwright.