Loopfour
Blocks

Stagehand Block

AI-driven browser automation for portal navigation, downloads, and form filling

The Stagehand block provides AI-driven browser automation powered by Stagehand and Browserbase. Use it to navigate web portals, download reports, fill forms, and extract data from websites that do not have APIs.

Stagehand uses computer vision and AI to understand web pages, making it resilient to UI changes. Actions are described in natural language rather than brittle CSS selectors.

Available Actions

Open a URL in the browser session.

OptionTypeDescription
urlstringURL to navigate to
contextIdstringBrowserbase context ID for auth persistence (optional)

Act

Execute a browser action described in natural language.

OptionTypeDescription
instructionstringWhat to do (e.g., "Click the login button")
timeoutMsnumberAction timeout in milliseconds (default: 30000)
agentFallbackbooleanFall back to agent mode if act fails (default: false)
observeBeforeActbooleanRequire exactly one observed action, then execute it without another model call
retryAttemptsnumberAdditional retries for invalid AI structured output (default: 2, maximum: 4)

Extract

Extract structured data from the current page.

OptionTypeDescription
instructionstringWhat to extract (e.g., "Get the invoice total and due date")

Download

Download a file triggered by a browser action.

OptionTypeDescription
instructionstringAction that triggers the download (e.g., "Click the PDF download link")
downloadWaitMsnumberWait time before polling for the file (default: 3000)
observeBeforeActbooleanRequire exactly one observed click action before triggering the download
retryAttemptsnumberAdditional retries for invalid AI output, missing downloads, canceled downloads, or MIME mismatches (default: 2, maximum: 4)
persistToFolderstringWrite the file straight to this workspace folder and return { fileId, filePath } instead of inline content. Keeps large downloads out of the step result.
expectedMimeTypestringOptional. Leave empty to accept any content type — the stored MIME is then derived from the file's bytes.

Set expectedMimeType only when the site always serves the same type. For a sniffable type (application/pdf, image/png, image/jpeg, application/zip) it is a hard assertion against the downloaded bytes: if they disagree the step fails even though the file downloaded correctly, and a site that sometimes serves a .zip instead of a PDF will fail every time. Prefer leaving it unset and validating the artifact in a later step with the workspace_files extract archive operation.

Screenshot

Capture the current page state.

OptionTypeDescription
fullPagebooleanCapture entire page vs viewport only (default: false)

Agent

Run an autonomous multi-step browser task.

OptionTypeDescription
instructionstringHigh-level goal (e.g., "Log in and download the monthly report")
maxStepsnumberMaximum actions the agent can take (default: 10)

Example Patterns

Download Accounting Reports

Schedule (monthly) -> Stagehand (navigate to portal)
    -> Stagehand (act: log in) -> Stagehand (act: navigate to reports)
    -> Stagehand (download: click PDF download)
    -> Agent (extract data from PDF) -> QuickBooks (record entries)

Fill Tax Forms

Trigger -> Stagehand (navigate to tax portal)
    -> Stagehand (act: fill in company name) -> Stagehand (act: fill in EIN)
    -> Stagehand (act: submit form) -> Stagehand (screenshot: confirmation)
    -> Slack (share confirmation)

Session Management

Browser sessions are cached per workflow run. One Browserbase session is shared across all Stagehand steps in a single execution, with a 15-minute TTL.

Auth persistence: Use Browserbase Contexts to persist cookies and login state across workflow runs. Create a context once, log in, and reuse the contextId in subsequent runs to skip login steps.

Best Practices

  • Use contextId for authenticated portals. This avoids re-authenticating on every run.
  • Set agentFallback: true for unreliable UI elements. If an act instruction fails (e.g., button not found), the agent mode can recover by exploring the page.
  • Use observeBeforeAct for high-value controls. The runtime requires exactly one selector-backed action and fails without clicking when the result is missing or ambiguous.
  • Keep download retries bounded. Each retry takes a fresh Browserbase download baseline, so a bad first file cannot be returned as the next attempt's result.
  • Use screenshot for debugging. Take screenshots before and after critical actions to verify the browser state.
  • Keep instructions specific. "Click the blue Download PDF button in the Reports section" is better than "download the report."

Frequently Asked Questions

Stagehand can automate any website accessible via a standard Chrome browser. This includes vendor portals, accounting platforms, government websites, and any web application. It uses AI vision to understand page layout, so it works even with dynamic or JavaScript-heavy pages.

Browserbase Contexts save browser cookies and session state. When you provide a contextId, the browser session starts with the saved cookies, maintaining login state from a previous session. Create a context once, authenticate, and reuse it across runs.

Stagehand uses Claude Sonnet 4.6 by default for act, extract, and observe actions. Agent mode also uses Claude Sonnet 4.6. Models can be overridden via environment variables (STAGEHAND_MODEL_NAME, STAGEHAND_AGENT_MODEL).

The download action triggers a browser action (like clicking a download link), waits for the download to complete via CDP events, then retrieves the file from Browserbase's cloud storage. The returned shape depends on persistToFolder: with it set, the bytes are written straight to workspace storage and the step returns a { fileId, filePath } reference; without it, the file is returned as base64-encoded content. Either way the MIME type is detected from the file's bytes, not from the provider's metadata field.

Leave expectedMimeType unset so the download accepts any content type, then add a workspace_files step with the extract archive operation after it. That step passes any non-archive through untouched and opens an archive to save what is inside; set its Keep Only These Types field to application/pdf when the downstream expects a PDF. Setting expectedMimeType to application/pdf instead makes the download fail outright on a .zip, and because the error is retryable-but-terminal when retryAttempts is 0, retrying will not recover it.

Browser sessions have a 15-minute TTL. Sessions are cached per workflow run, so all Stagehand steps in a single execution share the same session. The TTL sweeper runs every 5 minutes.

On this page