1

Get your API Key

Go over the Dashboard’s Settings tab:

Then copy and set the BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID environment variables in your .env file.

2

Install and create a virtual environment with UV

UV is a modern package manager for Python.

uv venv
3

Install Browserbase and Browser Use

uv venv
uv pip install browserbase browser-use
4

Create an Extended Browser Session and Context

We’ll create an extended version of the Browser Use context that better handles page management and session state.

main.py
from dotenv import load_dotenv
import os
import asyncio
from typing import Optional
from browserbase import Browserbase
from browser_use import Agent, Browser, BrowserConfig
from browser_use.browser.context import BrowserContext, BrowserContextConfig, BrowserSession
from langchain_anthropic import ChatAnthropic
from playwright.async_api import Page, BrowserContext as PlaywrightContext

class ExtendedBrowserSession(BrowserSession):
    """Extended version of BrowserSession that includes current_page"""
    def __init__(
        self,
        context: PlaywrightContext,
        cached_state: Optional[dict] = None,
        current_page: Optional[Page] = None
    ):
        super().__init__(context=context, cached_state=cached_state)
        self.current_page = current_page

class UseBrowserbaseContext(BrowserContext):
    async def _initialize_session(self) -> ExtendedBrowserSession:
        """Initialize a browser session using existing Browserbase page.

        Returns:
            ExtendedBrowserSession: The initialized browser session with current page.
        """
        playwright_browser = await self.browser.get_playwright_browser()
        context = await self._create_context(playwright_browser)
        self._add_new_page_listener(context)

        self.session = ExtendedBrowserSession(
            context=context,
            cached_state=None,
        )

        # Get existing page or create new one
        self.session.current_page = context.pages[0] if context.pages else await context.new_page()

        # Initialize session state
        self.session.cached_state = await self._update_state()

        return self.session
5

Set up Browser and Agent Configuration

Here’s how to configure the browser and create an automation agent:

main.py
async def setup_browser() -> tuple[Browser, UseBrowserbaseContext]:
    """Set up browser and context configurations.

    Returns:
        tuple[Browser, UseBrowserbaseContext]: Configured browser and context.
    """
    bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])
    bb_session = bb.sessions.create(
        project_id=os.environ["BROWSERBASE_PROJECT_ID"],
    )

    browser = Browser(config=BrowserConfig(cdp_url=bb_session.connect_url))
    context = UseBrowserbaseContext(
        browser,
        BrowserContextConfig(
            wait_for_network_idle_page_load_time=10.0,
            highlight_elements=True,
        )
    )

    return browser, context

async def setup_agent(browser: Browser, context: UseBrowserbaseContext) -> Agent:
    """Set up the browser automation agent.

    Args:
        browser: Configured browser instance
        context: Browser context for the agent

    Returns:
        Agent: Configured automation agent
    """
    llm = ChatAnthropic(
        model_name="claude-3-5-sonnet-20240620",
        temperature=0.0,
        timeout=100,
    )

    return Agent(
        task="go to https://www.macrumors.com/contact.php and fill in the form. Make sure to use the selectors and submit the form",
        llm=llm,
        browser=browser,
        browser_context=context,
    )
6

Create the Main Function

Here’s the main function that puts everything together:

main.py
async def main():
    load_dotenv()

    browser, context = await setup_browser()
    session = await context.get_session()

    try:
        agent = await setup_agent(browser, context)
        await agent.run()
    finally:
        # Simplified cleanup - just close the browser
        # This will automatically close all contexts and pages
        await browser.close()

if __name__ == "__main__":
    asyncio.run(main())
7

Run your script

Run your script:

uv run main.py

You should see your Browserbase session start in Browserbase.

Important Environment Variables

Make sure you have these environment variables in your .env file:

  • BROWSERBASE_API_KEY
  • BROWSERBASE_PROJECT_ID
  • ANTHROPIC_API_KEY

Common Issues & Fixes

Seeing blank or multiple tabs? You can:

Was this page helpful?