diff --git a/.changeset/petite-worms-punch.md b/.changeset/petite-worms-punch.md new file mode 100644 index 000000000..be113e8f7 --- /dev/null +++ b/.changeset/petite-worms-punch.md @@ -0,0 +1,5 @@ +--- +"@browserbasehq/stagehand": patch +--- + +add mapping of node id -> url diff --git a/lib/a11y/utils.ts b/lib/a11y/utils.ts index 15443cee8..f19ba765a 100644 --- a/lib/a11y/utils.ts +++ b/lib/a11y/utils.ts @@ -165,6 +165,9 @@ export async function buildHierarchicalTree( page?: StagehandPage, logger?: (logLine: LogLine) => void, ): Promise { + // Map to store nodeId -> URL for only those nodes that do have a URL. + const idToUrl: Record = {}; + // Map to store processed nodes for quick lookup const nodeMap = new Map(); const iframe_list: AccessibilityNode[] = []; @@ -178,6 +181,11 @@ export async function buildHierarchicalTree( return; } + const url = extractUrlFromAXNode(node); + if (url) { + idToUrl[node.nodeId] = url; + } + const hasChildren = node.childIds && node.childIds.length > 0; const hasValidName = node.name && node.name.trim() !== ""; const isInteractive = @@ -250,6 +258,7 @@ export async function buildHierarchicalTree( tree: finalTree, simplified: simplifiedFormat, iframes: iframe_list, + idToUrl: idToUrl, }; } @@ -294,6 +303,7 @@ export async function getAccessibilityTree( backendDOMNodeId: node.backendDOMNodeId, parentId: node.parentId, childIds: node.childIds, + properties: node.properties, }; }), page, @@ -493,6 +503,15 @@ function removeRedundantStaticTextChildren( return children; } +function extractUrlFromAXNode(axNode: AccessibilityNode): string | undefined { + if (!axNode.properties) return undefined; + const urlProp = axNode.properties.find((prop) => prop.name === "url"); + if (urlProp && urlProp.value && typeof urlProp.value.value === "string") { + return urlProp.value.value.trim(); + } + return undefined; +} + export async function performPlaywrightMethod( stagehandPage: Page, logger: (logLine: LogLine) => void, diff --git a/types/context.ts b/types/context.ts index d89068c5c..2c8976554 100644 --- a/types/context.ts +++ b/types/context.ts @@ -10,6 +10,13 @@ export interface AXNode { backendDOMNodeId?: number; parentId?: string; childIds?: string[]; + properties?: { + name: string; + value: { + type: string; + value?: string; + }; + }[]; } export type AccessibilityNode = { @@ -22,12 +29,20 @@ export type AccessibilityNode = { parentId?: string; nodeId?: string; backendDOMNodeId?: number; + properties?: { + name: string; + value: { + type: string; + value?: string; + }; + }[]; }; export interface TreeResult { tree: AccessibilityNode[]; simplified: string; iframes?: AccessibilityNode[]; + idToUrl: Record; } export interface EnhancedContext