Skip to main content

Overview

The Page class represents a single browser tab. It provides methods for navigation, element interaction, screenshots, and script evaluation.

Accessing Pages

Get pages from the context:
const page = await stagehand.context.newPage();
const activePage = stagehand.context.activePage();

goto()

Navigate to a URL.
const response = await page.goto(url, options?);
url
string
required
URL to navigate to
options
object
returns
Promise<Response | null>
Navigation response

reload()

Reload the current page.
await page.reload(options?);
options
object

goBack()

Navigate backward in history.
await page.goBack(options?);

goForward()

Navigate forward in history.
await page.goForward(options?);

url()

Get the current page URL.
const currentUrl = page.url();
returns
string
Current page URL

title()

Get the current page title.
const pageTitle = await page.title();
returns
Promise<string>
Current page title

Interaction Methods

click()

Click at specific coordinates.
await page.click(x, y, options?);
x
number
required
X coordinate in CSS pixels
y
number
required
Y coordinate in CSS pixels
options
object
returns
Promise<string>
XPath of clicked element if returnXpath is true

hover()

Hover at specific coordinates.
await page.hover(x, y, options?);

type()

Type text (focus must be on an input element).
await page.type(text, options?);
text
string
required
Text to type
options
object

scroll()

Scroll at a specific location.
await page.scroll(x, y, deltaX, deltaY, options?);
x
number
required
X coordinate
y
number
required
Y coordinate
deltaX
number
required
Horizontal scroll amount
deltaY
number
required
Vertical scroll amount

dragAndDrop()

Drag from one point to another.
await page.dragAndDrop(fromX, fromY, toX, toY, options?);
options
object

Capture Methods

screenshot()

Capture a screenshot of the page.
const buffer = await page.screenshot(options?);
options
ScreenshotOptions
returns
Promise<Buffer>
Screenshot image buffer

Evaluation Methods

evaluate()

Execute JavaScript in the page context.
const result = await page.evaluate(pageFunctionOrExpression, arg?);
pageFunctionOrExpression
string | Function
required
JavaScript expression or function to execute
arg
any
Argument passed to the function
returns
Promise<any>
Result of evaluation (must be JSON-serializable)

addInitScript()

Add script that runs on every document load.
await page.addInitScript(script, arg?);

Locator Methods

locator()

Create a locator for element queries.
const locator = page.locator(selector);
selector
string
required
CSS selector
See Locator utilities reference for more details.

deepLocator()

Create a locator with iframe traversal support.
const locator = page.deepLocator(selector);
selector
string
required
Selector with optional >> iframe hops

frameLocator()

Create a frame locator for targeting iframes.
const frameLocator = page.frameLocator(selector);

Wait Methods

waitForLoadState()

Wait for a lifecycle state.
await page.waitForLoadState(state, timeoutMs?);
state
'load' | 'domcontentloaded' | 'networkidle'
required
Lifecycle state to wait for
timeoutMs
number
Timeout in millisecondsDefault: 15000

waitForSelector()

Wait for an element to appear.
await page.waitForSelector(selector, options?);
selector
string
required
CSS selector (supports >> for iframe hops)
options
object

waitForTimeout()

Wait for a specific duration.
await page.waitForTimeout(ms);
ms
number
required
Milliseconds to wait

Other Methods

close()

Close this page (tab).
await page.close();

setViewportSize()

Set the viewport dimensions.
await page.setViewportSize(width, height, options?);
width
number
required
Width in CSS pixels
height
number
required
Height in CSS pixels
options.deviceScaleFactor
number
Device pixel ratioDefault: 1

sendCDP()

Send a raw CDP command.
const result = await page.sendCDP<T>(method, params?);
method
string
required
CDP method name (e.g., “Page.enable”)
params
object
Optional parameters
returns
Promise<T>
CDP response

Example

import { Stagehand } from "@browserbasehq/stagehand";

const stagehand = new Stagehand({ env: "LOCAL" });
await stagehand.init();

const page = await stagehand.context.newPage();

// Navigate
await page.goto("https://example.com", { waitUntil: "load" });

// Check URL and title
console.log(page.url());
console.log(await page.title());

// Take screenshot
const screenshot = await page.screenshot({ fullPage: true });

// Wait for element
await page.waitForSelector(".submit-button", { state: "visible" });

// Evaluate JavaScript
const result = await page.evaluate(() => {
  return document.querySelectorAll("a").length;
});

await stagehand.close();

Build docs developers (and LLMs) love