Skip to content

fixed deepLocator() pathing bug #880

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
73 changes: 44 additions & 29 deletions lib/handlers/handlerUtils/actHandlerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,52 @@ import { StagehandClickError } from "@/types/stagehandErrors";
const IFRAME_STEP_RE = /^iframe(\[[^\]]+])?$/i;

export function deepLocator(
root: Page | FrameLocator,
rawXPath: string,
): Locator {
// 1 ─ strip optional 'xpath=' prefix and whitespace
let xpath = rawXPath.replace(/^xpath=/i, "").trim();
if (!xpath.startsWith("/")) xpath = "/" + xpath;

// 2 ─ split into steps, accumulate until we hit an iframe step
const steps = xpath.split("/").filter(Boolean); // tokens
let ctx: Page | FrameLocator = root;
let buffer: string[] = [];

const flushIntoFrame = () => {
if (buffer.length === 0) return;
const selector = "xpath=/" + buffer.join("/");
ctx = (ctx as Page | FrameLocator).frameLocator(selector);
buffer = [];
};

for (const step of steps) {
buffer.push(step);
if (IFRAME_STEP_RE.test(step)) {
// we've included the <iframe> element in buffer ⇒ descend
flushIntoFrame();
root: Page | FrameLocator,
rawXPath: string,
): Locator {
// 1 ─ strip optional 'xpath=' prefix and whitespace
let xpath = rawXPath.replace(/^xpath=/i, "").trim();

// Split the path by sequences of slashes, but keep the slashes as
// separate elements in the array. This preserves separators like '//'.
// e.g., '//a/b' becomes ['//', 'a', '/', 'b']
const parts = xpath.split(/(\/+)/).filter(Boolean);

let ctx: Page | FrameLocator = root;
let buffer: string[] = [];

const flushIntoFrame = () => {
if (buffer.length === 0) return;

// Join the buffered parts to form the selector for the iframe.
// .join('') is used because the separators are already in the buffer.
const selector = "xpath=" + buffer.join("");
ctx = (ctx as Page | FrameLocator).frameLocator(selector);
buffer = []; // Reset buffer for the next path segment.
};

// Iterate through all parts (which include both steps and their separators).
for (const part of parts) {
buffer.push(part);

// A "step" is a part of the path that is NOT a separator.
// We test if the current step (a non-slash part) is an iframe locator.
const isStep = !/^\/+$/.test(part);
if (isStep && IFRAME_STEP_RE.test(part)) {
flushIntoFrame();
}
}
}

// 3 ─ whatever is left in buffer addresses the target *inside* the last ctx
const finalSelector = "xpath=/" + buffer.join("/");
return (ctx as Page | FrameLocator).locator(finalSelector);
}
// If the XPath ended with an iframe, the buffer will be empty. In this case,
// we return a locator for the root element ('*') within the final frame.
if (buffer.length === 0) {
return (ctx as Page | FrameLocator).locator("xpath=/*");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Potentially incorrect fallback for empty buffer case - returning '/*' will only match root level elements, not all elements in the frame


// Join the remaining parts for the final locator.
const finalSelector = "xpath=" + buffer.join("");
return (ctx as Page | FrameLocator).locator(finalSelector);
}

/**
* A mapping of playwright methods that may be chosen by the LLM to their
Expand Down