Observe

View as Markdown

Ask Stagehand what actions are available on an active browser page. Use the result to inspect the page before choosing an action, or pass the instruction to your own decision-making code.

Observe a page

1const result = await bctrl.tools.call("stagehand.observe", {
2 pageId,
3 instruction: "Find the primary button that continues checkout.",
4}, { runtimeId: runtime.id });
5
6for (const action of result.actions) {
7 console.log(action.description, action.selector);
8}

pageId is optional. If omitted, Stagehand observes the Runtime’s active page. instruction must be between 1 and 20,000 characters, and timeoutMs can be up to 120000 milliseconds.

Request parameters

ParameterTypeRequiredDescription
instructionstringYesWhat to find on the page, from 1 to 20,000 characters.
pageIdPageIdNoPage to inspect. Uses the active page when omitted.
timeoutMsintegerNoMaximum observation time in milliseconds. Maximum: 120000.

Response

FieldTypeAlways presentDescription
actionsStagehandAction[]YesActionable elements and possible browser actions.
cacheStatus"HIT" | "MISS"NoWhether a cached result was used.

Each action contains:

FieldTypeAlways presentDescription
selectorstringYesElement selector suggested for the action.
descriptionstringYesHuman-readable action description.
methodstringNoBrowser method associated with the action.
argumentsstring[]NoArguments associated with the method.

Run asynchronously

1const call = await bctrl.tools.start("stagehand.observe", {
2 pageId,
3 instruction: "Find every field required to complete this form.",
4}, { runtimeId: runtime.id });
5
6const result = await bctrl.toolCalls.result(call.id, {
7 waitSeconds: 120,
8});

Use bctrl.toolCalls.get(call.id) to inspect progress or bctrl.toolCalls.cancel(call.id) to cancel an active call.

Next