Stagehand Context
All Context methods available when using the Stagehand driver.
Access via runtime.browserContext after launching a Stagehand runtime.
How these methods work over HTTP
Every method below is a remote call. The SDK translates it into a structured step sent to a single endpoint:
The call field maps directly to the method name. args is a JSON array of the method’s arguments. You can batch multiple steps in one request.
Lifecycle
newPage(url?)
Create a new page (tab) in the browser.
The new page is automatically set as the active page. Defaults to “about:blank” if no URL is provided.
Returns RemoteStagehandPage
pages()
Get all open pages in the browser context.
Returns an array of all open pages, ordered from oldest to newest.
Returns RemoteStagehandPage[]
close()
Close the browser context and all associated pages.
This method:
- Closes the CDP connection
- Cleans up all pages
- Clears all internal mappings
Note: This is typically called internally by stagehand.close(). You usually don’t need to call this directly.
Other
activePage()
Get the currently active page.
The active page is determined by:
- Most recently interacted with page
- Most recently created page if no interaction history
- undefined if all pages have been closed
Returns RemoteStagehandPage \| undefined
setActivePage(pageId)
Set a specific page as the active page.
This method:
- Marks the page as most recently used
- Brings the tab to the foreground (in headed mode)
- Makes it the default page for subsequent operations
Note: Pass page.id from a RemoteStagehandPage instance.
addInitScript(script, arg?)
Inject JavaScript that runs before any page scripts on every navigation.
This method:
- Runs at document start
- Installs the script on all currently open pages
- Replays it on every navigation of those pages
- Automatically applies to any pages created after calling addInitScript()

