Browser

Browser Sessions provide direct access to Chrome browsers via DevTools Protocol (CDP), enabling advanced automation and custom browser control.

What is a Browser Session?

A raw browser instance with:

  • CDP URL: Direct Chrome DevTools Protocol access
  • Live URL: Real-time browser viewing
  • Timeout-based lifecycle: Automatic cleanup after inactivity
  • Profile support: Inherit saved browser state

Unlike AI agent sessions, browser sessions give you direct programmatic control.

Key Properties

  • id: Unique browser session identifier
  • status: "active" or "stopped"
  • cdpUrl: Chrome DevTools Protocol URL for automation
  • liveUrl: Real-time browser viewing URL
  • timeoutAt: When the session will automatically stop

Creating Browser Sessions

1import { BrowserUseClient } from "browser-use-sdk";
2
3const client = new BrowserUseClient({ apiKey: "bu_..." });
4
5// Basic browser session
6const browserSession = await client.browsers.createBrowserSession({
7 timeout: 30 // minutes
8});
9
10// With profile (inherit login state)
11const profileBrowser = await client.browsers.createBrowserSession({
12 profileId: "profile_123",
13 proxyCountryCode: "US",
14 timeout: 60
15});
16
17console.log(`CDP URL: ${browserSession.cdpUrl}`);
18console.log(`Watch live: ${browserSession.liveUrl}`);

Using CDP for Automation

1import { chromium } from 'playwright';
2
3// Connect to the browser session
4const browser = await chromium.connectOverCDP(browserSession.cdpUrl);
5const context = browser.contexts()[0];
6const page = context.pages()[0];
7
8// Direct browser automation
9await page.goto('https://example.com');
10await page.fill('#username', 'admin');
11await page.fill('#password', 'secret');
12await page.click('#login');
13
14// Take screenshot
15const screenshot = await page.screenshot();
16
17// Close when done
18await browser.close();

Managing Browser Sessions

1// List all browser sessions
2const sessions = await client.browsers.listBrowserSessions();
3
4// Get specific session
5const session = await client.browsers.getBrowserSession(sessionId);
6
7// Stop session
8await client.browsers.updateBrowserSession(sessionId, { action: "stop" });
9
10// Filter by status
11const activeSessions = await client.browsers.listBrowserSessions({
12 filterBy: "active"
13});

Automation Libraries

Browser sessions work with any CDP-compatible automation library:

Playwright:

1import { chromium } from 'playwright';
2const browser = await chromium.connectOverCDP(cdpUrl);

Puppeteer:

1import puppeteer from 'puppeteer';
2const browser = await puppeteer.connect({ browserWSEndpoint: cdpUrl });

Selenium:

1from selenium import webdriver
2from urllib.parse import urlparse
3
4options = webdriver.ChromeOptions()
5parsed_url = urlparse(cdp_url)
6debugger_address = f"{parsed_url.hostname}:{parsed_url.port}"
7options.add_experimental_option("debuggerAddress", debugger_address)
8driver = webdriver.Chrome(options=options)

Browser vs Agent Sessions

Browser Sessions (Direct Control):

  • Raw CDP access for custom automation
  • Use your own automation scripts
  • Full control over browser behavior
  • Requires programming automation logic

Agent Sessions (AI-Powered):

  • AI agent executes natural language instructions
  • Separate AI-powered automation system
  • Automatic task completion
  • No programming required

Common Use Cases

Custom Automation Workflows:

1const browser = await client.browsers.createBrowserSession({ timeout: 60 });
2const pwBrowser = await chromium.connectOverCDP(browser.cdpUrl);
3
4// Multi-step custom workflow
5const page = pwBrowser.contexts()[0].pages()[0];
6await page.goto('https://admin.example.com');
7await page.waitForSelector('#dashboard');
8
9// Custom data extraction
10const data = await page.evaluate(() => {
11 return Array.from(document.querySelectorAll('.metric')).map(el => ({
12 label: el.querySelector('.label').textContent,
13 value: el.querySelector('.value').textContent
14 }));
15});
16
17console.log('Extracted metrics:', data);

Integration Testing:

1// Create browser for testing
2const browser = await client.browsers.createBrowserSession({
3 profileId: testProfileId,
4 timeout: 30
5});
6
7// Run test suite
8const playwright = await chromium.connectOverCDP(browser.cdpUrl);
9await runTestSuite(playwright);
10
11// Cleanup
12await client.browsers.updateBrowserSession(browser.id, { action: "stop" });

Web Scraping with State:

1// Browser with login profile
2const browser = await client.browsers.createBrowserSession({
3 profileId: "logged_in_profile",
4 proxyCountryCode: "US"
5});
6
7// Already logged in from profile
8const page = (await chromium.connectOverCDP(browser.cdpUrl)).contexts()[0].pages()[0];
9await page.goto('https://protected-data.example.com');
10
11// Scrape authenticated content
12const protectedData = await page.locator('.protected-content').textContent();

Best Practices

Session Management:

  • Set appropriate timeouts based on task duration
  • Always stop sessions when done to free resources
  • Use profiles to preserve login state across sessions

Automation:

  • Handle network errors and timeouts gracefully
  • Use explicit waits for dynamic content
  • Take screenshots for debugging

Performance:

  • Reuse profiles to avoid repeated login flows
  • Close sessions promptly to avoid timeout charges
  • Use appropriate proxy locations for target websites

Security:

  • Don’t expose CDP URLs publicly
  • Use profiles to isolate different users
  • Monitor session usage for suspicious activity

Next Steps