The context object manages the browser context, which is a container for multiple pages (tabs). It provides methods for creating new pages, accessing existing pages, and managing which page is currently active.Access the context through your Stagehand instance:
Provide the script to inject. Pass raw source code, reference a file on disk,
or supply a function that Stagehand serializes before sending to the browser.
import { Stagehand } from "@browserbasehq/stagehand";// Initialize with Browserbase (API key and project ID from environment variables)// Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID in your environmentconst stagehand = new Stagehand({ env: "BROWSERBASE" });await stagehand.init();const context = stagehand.context;// Create a new pageconst page1 = await context.newPage("https://example.com");console.log("Created page 1");// Create another pageconst page2 = await context.newPage("https://another-site.com");console.log("Created page 2");// Get all pagesconst allPages = context.pages();console.log(`Total pages: ${allPages.length}`);await stagehand.close();
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL" });await stagehand.init();const context = stagehand.context;// Start with main pageconst mainPage = context.pages()[0];await mainPage.goto("https://example.com");// Open additional pagesconst dashboardPage = await context.newPage("https://example.com/dashboard");const settingsPage = await context.newPage("https://example.com/settings");// Work with specific pagecontext.setActivePage(dashboardPage);await stagehand.act("click the export button");// Switch to another pagecontext.setActivePage(settingsPage);await stagehand.act("enable notifications");// Back to main pagecontext.setActivePage(mainPage);await stagehand.act("click the logout button");await stagehand.close();
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL" });await stagehand.init();const context = stagehand.context;// Create multiple pagesconst pages = await Promise.all([ context.newPage("https://site1.com"), context.newPage("https://site2.com"), context.newPage("https://site3.com"),]);console.log(`Opened ${pages.length} pages`);// Get the active pageconst active = context.activePage();console.log(`Active page URL: ${active?.url()}`);// Iterate through all pagesfor (const page of context.pages()) { console.log(`Page URL: ${page.url()}`); console.log(`Page title: ${await page.title()}`);}await stagehand.close();
import { Stagehand } from "@browserbasehq/stagehand";import { z } from "zod/v3";const stagehand = new Stagehand({ env: "LOCAL" });await stagehand.init();const context = stagehand.context;// Create pages for different sitesconst page1 = await context.newPage("https://site1.com");const page2 = await context.newPage("https://site2.com");const page3 = await context.newPage("https://site3.com");const schema = z.object({ title: z.string(), description: z.string()});// Extract data from all pages in parallelconst results = await Promise.all([ stagehand.extract("get page info", schema, { page: page1 }), stagehand.extract("get page info", schema, { page: page2 }), stagehand.extract("get page info", schema, { page: page3 })]);console.log("Extracted data:", results);await stagehand.close();
import { Stagehand } from "@browserbasehq/stagehand";const stagehand = new Stagehand({ env: "LOCAL" });await stagehand.init();const context = stagehand.context;// Create pagesconst homePage = await context.newPage("https://example.com");const aboutPage = await context.newPage("https://example.com/about");const contactPage = await context.newPage("https://example.com/contact");// The last created page (contactPage) is now activeconsole.log("Active:", context.activePage()?.url());// Output: "https://example.com/contact"// Switch to home pagecontext.setActivePage(homePage);console.log("Active:", context.activePage()?.url());// Output: "https://example.com"// Now act on the active page (homePage)await stagehand.act("click the header link");await stagehand.close();
The context tracks which page is currently active:
const stagehand = new Stagehand({ env: "LOCAL" });await stagehand.init();// Get the current active pageconst activePage = stagehand.context.activePage();// Create a new page - it becomes activeconst newPage = await stagehand.context.newPage();// Now context.activePage() returns newPageawait newPage.goto("https://example.com");
Context manages the browser-level state and multiple pages
Page represents a single tab/window with content
Creating a new page via context.newPage() automatically sets it as active
You can explicitly control the active page with context.setActivePage()
Use context.activePage() to get the currently active page
// Get the active pageconst activePage = stagehand.context.activePage();// Or get the first page directlyconst firstPage = stagehand.context.pages()[0];