Overview
Thecontext 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:
Methods
newPage()
Create a new page (tab) in the browser.The URL to navigate to in the new page.Default:
"about:blank"Promise<Page> - The newly created page object.
The new page is automatically set as the active page.
pages()
Get all open pages in the browser context.Page[] - Array of all open pages, ordered from oldest to newest.
activePage()
Get the currently active page.Page | undefined - The most recently used page, or undefined if no pages exist.
The active page is determined by:
- Most recently interacted with page
- Most recently created page if no interaction history
undefinedif all pages have been closed
setActivePage()
Set a specific page as the active page.The page to set as active. Must be a page that exists in this context.
- Marks the page as most recently used
- Brings the tab to the foreground (in headed mode)
- Makes it the default page for subsequent operations
addInitScript()
Inject JavaScript that runs before any page scripts on every navigation.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.
Extra data that is JSON-serialized and passed to your function. Only supported
when
script is a function.- Runs at document start, and installs the script on all currently open pages and replays it on every navigation of those pages
- Automatically applies the same script to any pages created after calling
context.addInitScript() - Allows referencing preload files via
{ path: "./preloads/dom-hooks.js" }, mirroring Playwright’ssourceURLbehavior for readable stack traces
close()
Close the browser context and all associated pages.- Closes the CDP connection
- Cleans up all pages
- Clears all internal mappings
stagehand.close(). You usually don’t need to call this directly.
Code Examples
- Basic Usage
- Multi-Page Workflow
- Page Management
- Parallel Operations
- Active Page Tracking
Working with Active Pages
The context tracks which page is currently active:Relationship Between Context and Page
- 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
Best Practices
- Create pages explicitly - Use
context.newPage()instead of relying on popups or window.open - Track page references - Store page objects in variables for easier management
- Set active page before operations - Ensure the correct page is active before calling Stagehand methods
- Clean up properly - Call
stagehand.close()to close all pages and the context - Handle page order - Remember that
context.pages()returns pages in creation order - Use parallel operations - Work with multiple pages simultaneously for better performance
Common Patterns
Tab Management
Bulk Data Collection
Conditional Page Management
Error Handling
Context methods may throw the following errors:- Timeout errors -
newPage()timeout waiting for page to attach - CDP errors - Connection errors with Chrome DevTools Protocol
- Invalid page errors - Attempting to set an active page that doesn’t exist in the context

