Skip to main content

Python SDK for Stagehand

Project description

Stagehand SDK Client (Python)

Python SDK for controlling the Stagehand extension through ModCDP.

Run

From the repository root:

pnpm run build:artifacts
pnpm --filter @browserbasehq/stagehand-docs test:examples:python

For the CI smoke flow that exercises raw CDP, ModCDP, and GitHub summary output:

uv run python tests/integration/test_basic.py

Usage

import asyncio
import os

from stagehand import StagehandClient


async def main() -> None:
    client = StagehandClient(
        # Pass at most one launch source:
        # cdp_url="http://127.0.0.1:9222",
        # cdp_url="ws://127.0.0.1:9222/devtools/browser/<id>",
        # local_browser_launch_options={
        #     "executable_path": "/Applications/Chromium.app/Contents/MacOS/Chromium",
        # },
        # browserbase_session_create_params={
        #     "browserbase_api_key": os.environ["BROWSERBASE_API_KEY"],
        # },
    )

    await client.connect()

    client.on("Target.targetInfoChanged", print)

    print(await client.Browser.getVersion())
    print(await client.Target.getTargets())
    print(await client.Mod.evaluate(expression="chrome.runtime.id"))

    browser = client.browser
    page = await browser.new_page(url="https://example.com")
    page2 = await page.goto(url="https://browserbase.com")
    print(page2.url)
    print(await browser.pages(url=page2.url))

    body = await page.locate(css="body")
    print(await body.info())

    await client.close()


asyncio.run(main())

StagehandClient.connect() and StagehandClient.close() are async. Stagehand browser/page/locator methods are async. The compact client.* CDP command surface is synchronous because the Python ModCDP client owns a background websocket reader thread.

Launch Sources

connect() accepts exactly one effective launch source:

  • cdp_url: an existing browser CDP endpoint. http://... URLs are resolved through /json/version; ws://... and wss://... URLs are used directly.
  • local_browser_launch_options: launches local Chromium or Chrome Canary with CDP enabled. CHROME_PATH can provide the executable path. STAGEHAND_SDK_CDP_PORT fixes the debugging port for editor attach workflows.
  • browserbase_session_create_params["browserbase_api_key"]: asks ModCDP to create a Browserbase session. The SDK uploads or reuses the packaged versioned extension artifact automatically; no extension ID configuration is required.

If no launch source is passed, connect() reads os.environ; it uses BROWSERBASE_API_KEY when present, otherwise it launches local Chromium/Canary.

Extension Paths

The default unpacked extension directory is the WXT output:

stagehand-extension/.output/chrome-mv3

The same directory contains the single reusable extension zip:

stagehand-extension/.output/chrome-mv3/stagehand-extension.zip

The prepared extension manifest uses the canonical stagehand-modcdp/background.js service worker path. The client trusts and waits for that service worker with globalThis.__stagehand_modcdp_ready === true.

Routing

Default client routes:

{
    "Mod.*": "service_worker",
    "Stagehand.*": "service_worker",
    "*.*": "service_worker",
}

Default extension service-worker routes:

{
    "Mod.*": "service_worker",
    "Stagehand.*": "service_worker",
    "*.*": "loopback_cdp",
}

Stagehand SDK routes are fixed: Stagehand and ModCDP commands go through the extension service worker, and native CDP commands go from the service worker to loopback CDP.

Debugging

The checked-in VS Code tasks set:

STAGEHAND_DEBUG_WAIT_FOR_ENTER=1
STAGEHAND_SDK_CDP_PORT=9231

The smoke test at tests/integration/test_basic.py prints [debug] Browser ready. Press Enter to continue. after the extension is connected, so DevTools can inspect chrome-extension://*/stagehand-modcdp/background.js before the flow continues.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shalpha-4.0.0rc54.tar.gz (3.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shalpha-4.0.0rc54-py3-none-any.whl (2.9 MB view details)

Uploaded Python 3

File details

Details for the file shalpha-4.0.0rc54.tar.gz.

File metadata

  • Download URL: shalpha-4.0.0rc54.tar.gz
  • Upload date:
  • Size: 3.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for shalpha-4.0.0rc54.tar.gz
Algorithm Hash digest
SHA256 5af1bcece3fdc320c4851e4393c42c30672086e1e770b65ce3e20a71e79a4d49
MD5 7455258e5a64df7e6961c1fcdea64e93
BLAKE2b-256 965f52b23f950ee8e551968b4b9fddbc24fcc881fa72f0e26f6149a906e2b51b

See more details on using hashes here.

File details

Details for the file shalpha-4.0.0rc54-py3-none-any.whl.

File metadata

  • Download URL: shalpha-4.0.0rc54-py3-none-any.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for shalpha-4.0.0rc54-py3-none-any.whl
Algorithm Hash digest
SHA256 b950a5ebd8b2539dd828dcdd7de13e428c61beda054fca3402b3569492f0caf9
MD5 fefcfd3aa87928a2a4d9e85e1fcc81f4
BLAKE2b-256 a1d5779b4ca844eb1ae3fd751746d0c328c1d1ff1ef33425f66383ad11cd8ca0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page