Hyperbrowser Python SDK
Hyperbrowser Python SDK is a client library for interacting with the Hyperbrowser platform, offering cloud-based headless browser automation. It enables developers to perform advanced web scraping, create AI-driven browser agents, and manage scalable browser sessions with built-in features like CAPTCHA solving, proxy management, and anti-bot detection. The library provides both synchronous and asynchronous clients for flexible integration, with frequent updates to its `0.90.x` series.
Common errors
-
ModuleNotFoundError: No module named 'hyperbrowser'
cause The `hyperbrowser` package is not installed in the current Python environment.fixInstall the package using pip: `pip install hyperbrowser`. -
AttributeError: 'Hyperbrowser' object has no attribute 'sessions'
cause This usually happens if the `Hyperbrowser` or `AsyncHyperbrowser` client is not correctly initialized or if you're trying to access `sessions` without creating an instance.fixEnsure you have correctly initialized the client, e.g., `client = Hyperbrowser(api_key="...")` or `async with AsyncHyperbrowser(api_key="...") as client:`. -
hyper.common.exceptions.HTTPUpgrade: ('h2c', <socket.socket fd=...)cause This error comes from the unrelated `hyper` library, which handles HTTP/2 connections. It indicates a confusion between the `hyper` library and the `hyperbrowser` SDK. You might have mistakenly imported `hyper` components or installed the wrong library.fixEnsure you have `pip install hyperbrowser` and are importing `from hyperbrowser import ...` not `from hyper import ...`. If `hyper` is installed and causing conflicts, consider uninstalling it if not needed for other parts of your project: `pip uninstall hyper`. -
KeyError: 'ws_endpoint'
cause This error occurs when attempting to access `session.ws_endpoint` (or other session attributes) on a `session` object that is `None` or an unexpected structure, typically because the `client.sessions.create()` call failed.fixAdd error handling around `client.sessions.create()` to check if the session object is valid before attempting to access its attributes. The most common cause is an invalid `HYPERBROWSER_API_KEY`.
Warnings
- gotcha It is crucial to explicitly stop Hyperbrowser sessions using `client.sessions.stop(session.id)` in a `finally` block or similar cleanup logic. Failing to do so can lead to sessions running longer than intended, consuming resources, and potentially incurring unexpected charges, even with automatic timeouts.
- gotcha The Hyperbrowser SDK for Python uses `snake_case` for all session parameters (e.g., `use_proxy`, `solve_captchas`), while the Node.js/TypeScript SDK uses `camelCase` (e.g., `useProxy`, `solveCaptchas`). Using the incorrect casing in Python will result in `TypeError` or ignored parameters.
- gotcha The core `hyperbrowser` library does not bundle browser automation libraries like Playwright or Puppeteer. To interact with the remote browser session's WebSocket endpoint (e.g., `session.ws_endpoint`), you must separately install and use `playwright-core` or `puppeteer-core`.
- breaking The `0.90.0` release included a fix for an `incorrect type for FetchBrowserOptions.solve_captchas`. If your code used `FetchBrowserOptions` and explicitly set `solve_captchas` with a type that was previously accepted but is now corrected, it might lead to type validation errors or runtime issues.
Install
-
pip install hyperbrowser -
pip install "hyperbrowser[playwright]" # or "hyperbrowser[puppeteer]" or "hyperbrowser[langchain]"
Imports
- Hyperbrowser
from hyperbrowser import Hyperbrowser
- AsyncHyperbrowser
from hyperbrowser import AsyncHyperbrowser
- CreateSessionParams
from hyperbrowser.models import CreateSessionParams
- Hyperbrowser
from hyper import HTTP20Connection
from hyperbrowser import Hyperbrowser
Quickstart
import asyncio
import os
from hyperbrowser import AsyncHyperbrowser
from puppeteer_core import connect # Or from playwright_core import connect_over_cdp
async def main():
hyperbrowser_api_key = os.environ.get('HYPERBROWSER_API_KEY', '')
if not hyperbrowser_api_key:
print("Error: HYPERBROWSER_API_KEY environment variable not set.")
print("Please get your API key from https://app.hyperbrowser.ai/dashboard and set it.")
return
async with AsyncHyperbrowser(api_key=hyperbrowser_api_key) as client:
print("Creating Hyperbrowser session...")
session = await client.sessions.create()
print(f"Session created: {session.id}, WS Endpoint: {session.ws_endpoint}")
try:
# Connect to the remote browser using puppeteer_core (or playwright_core)
browser = await connect(browserWSEndpoint=session.ws_endpoint, defaultViewport=None)
page = (await browser.pages())[0]
print("Navigating to example.com...")
await page.goto("https://example.com")
page_title = await page.title()
print(f"Page title: {page_title}")
await page.close()
await browser.disconnect()
except Exception as e:
print(f"Error during browser interaction: {e}")
finally:
print(f"Stopping session {session.id}...")
await client.sessions.stop(session.id)
print("Session stopped.")
if __name__ == "__main__":
asyncio.run(main())