*** title: Stagehand Context description: Complete Context method reference for the Stagehand driver. ------------------------------------------------------------------------ All `Context` methods available when using the **Stagehand** driver. Access via `runtime.browserContext` after launching a Stagehand runtime. Every method below is a remote call. The SDK translates it into a structured step sent to a single endpoint: ``` POST /v1/workspaces/{workspaceId}/execute ``` ```json { "runtime": "my-browser", "steps": [ { "call": "context.goto", "args": ["https://example.com"] } ] } ``` 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. ```ts const page = await context.newPage(); ``` | Parameter | Type | Required | Description | | --------- | -------- | -------- | --------------------------------------- | | `url` | `string` | No | The URL to navigate to in the new page. | **Returns** `RemoteStagehandPage` [Upstream docs](https://docs.stagehand.dev/references/context#newpage) *** ### pages() Get all open pages in the browser context. Returns an array of all open pages, ordered from oldest to newest. ```ts const allPages = await context.pages(); ``` **Returns** `RemoteStagehandPage[]` [Upstream docs](https://docs.stagehand.dev/references/context#pages) *** ### 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. ```ts await runtime.browserContext.close(); ``` [Upstream docs](https://docs.stagehand.dev/references/context#close) *** ## Other ### activePage() Get the currently active page. The active page is determined by: 1. Most recently interacted with page 2. Most recently created page if no interaction history 3. undefined if all pages have been closed ```ts const page = await context.activePage(); ``` **Returns** `RemoteStagehandPage \| undefined` [Upstream docs](https://docs.stagehand.dev/references/context#activepage) *** ### 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. ```ts context.setActivePage(page.id); ``` | Parameter | Type | Required | Description | | --------- | -------- | -------- | ------------------------------------ | | `pageId` | `string` | Yes | The ID of the page to set as active. | [Upstream docs](https://docs.stagehand.dev/references/context#setactivepage) *** ### 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() ```ts await context.addInitScript(() => { window.alert = () => {}; window.confirm = () => true; }); ``` | Parameter | Type | Required | Description | | --------- | ----------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------ | | `script` | `string \| { path?: string; content?: string } \| ((arg: Arg) => unknown)` | Yes | The script to inject. Can be raw source code, a file reference, or a function. | | `arg` | `Arg` | No | Extra data that is JSON-serialized and passed to your function. | [Upstream docs](https://docs.stagehand.dev/references/context#addinitscript) ***