Skip to content

Add link mapping #648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/petite-worms-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

add mapping of node id -> url
19 changes: 19 additions & 0 deletions lib/a11y/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ export async function buildHierarchicalTree(
page?: StagehandPage,
logger?: (logLine: LogLine) => void,
): Promise<TreeResult> {
// Map to store nodeId -> URL for only those nodes that do have a URL.
const idToUrl: Record<string, string> = {};

// Map to store processed nodes for quick lookup
const nodeMap = new Map<string, AccessibilityNode>();
const iframe_list: AccessibilityNode[] = [];
Expand All @@ -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 =
Expand Down Expand Up @@ -250,6 +258,7 @@ export async function buildHierarchicalTree(
tree: finalTree,
simplified: simplifiedFormat,
iframes: iframe_list,
idToUrl: idToUrl,
};
}

Expand Down Expand Up @@ -294,6 +303,7 @@ export async function getAccessibilityTree(
backendDOMNodeId: node.backendDOMNodeId,
parentId: node.parentId,
childIds: node.childIds,
properties: node.properties,
};
}),
page,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ export interface AXNode {
backendDOMNodeId?: number;
parentId?: string;
childIds?: string[];
properties?: {
name: string;
value: {
type: string;
value?: string;
};
}[];
}

export type AccessibilityNode = {
Expand All @@ -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<string, string>;
}

export interface EnhancedContext
Expand Down