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

On this page