Skip to content

generate xpaths for namespaced elements #962

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 3 commits into from
Aug 13, 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/true-bees-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

handle namespaced elements in xpath build step
4 changes: 4 additions & 0 deletions evals/evals.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@
{
"name": "agent/sign_in",
"categories": ["agent"]
},
{
"name": "namespace_xpath",
"categories": ["act"]
}
]
}
39 changes: 39 additions & 0 deletions evals/tasks/namespace_xpath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { EvalFunction } from "@/types/evals";

export const namespace_xpath: EvalFunction = async ({
debugUrl,
sessionUrl,
stagehand,
logger,
}) => {
try {
await stagehand.page.goto(
"https://browserbase.github.io/stagehand-eval-sites/sites/namespaced-xpath/",
);

await stagehand.page.act({
action: "fill 'nunya' into the 'type here' form",
});

const inputValue = await stagehand.page.locator("#ns-text").inputValue();
// confirm that the form was filled
const formHasBeenFilled = inputValue === "nunya";

return {
_success: formHasBeenFilled,
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
} catch (error) {
return {
_success: false,
error: error,
debugUrl,
sessionUrl,
logs: logger.getLogs(),
};
} finally {
await stagehand.close();
}
};
17 changes: 11 additions & 6 deletions lib/a11y/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,18 @@ export async function buildBackendIdMaps(
const tag = lc(String(child.nodeName));
const key = `${child.nodeType}:${tag}`;
const idx = (ctr[key] = (ctr[key] ?? 0) + 1);
segs.push(
child.nodeType === 3
? `text()[${idx}]`
: child.nodeType === 8
? `comment()[${idx}]`
if (child.nodeType === 3) {
segs.push(`text()[${idx}]`);
} else if (child.nodeType === 8) {
segs.push(`comment()[${idx}]`);
} else {
// Element node: if qualified (e.g. "as:ajaxinclude"), switch to name()='as:ajaxinclude'
segs.push(
tag.includes(":")
? `*[name()='${tag}'][${idx}]`
: `${tag}[${idx}]`,
);
);
}
}
// push R→L so traversal remains L→R
for (let i = kids.length - 1; i >= 0; i--) {
Expand Down
Loading