stagehand icon indicating copy to clipboard operation
stagehand copied to clipboard

deepLocator deep-XPath only treats positionally-indexed iframe steps as frame hops — iframe[@name='…'] paths resolve in the wrong frame

Open brennanbutler01 opened this issue 3 weeks ago • 0 comments

Before submitting an issue, please:

  • [x] Check the documentation for relevant information
  • [x] Search existing issues to avoid duplicates

Environment Information

Stagehand:

  • Language/SDK: TypeScript
  • Stagehand version: 4.4.0 (main d9cb8cc as of 2026-11-08)

AI Provider:

  • Provider: N/A — pure selector resolution, no LLM involved

Issue Description

planDeepXPathTarget decides which XPath steps are iframe "hops" using:

// understudy/a11y/snapshot/focusSelectors.ts:54
export const IFRAME_STEP_RE = /^i?frame(?:\[\d+])?$/i;

…applied to the step's name, where parseXPath strips only a trailing positional predicate:

const name = raw.replace(/\[\d+\]\s*$/u, "").toLowerCase();

So iframe[2] is recognized as a hop, but any iframe step that selects by an attribute predicate — iframe[@name='main'], iframe[@id='content'], iframe[2][@title='x'] — fails the regex, is not planned as a hop, and the remaining path is resolved in the parent frame. The lookup then fails (or resolves to nothing) even though the target element exists inside that iframe.

Hand-written deep XPaths that select the iframe by attribute therefore can never cross the frame boundary, while the same selector written with a positional predicate works.

Steps to Reproduce

  1. Call planDeepXPathTarget("//iframe[@name='main']//button").
  2. Observe frameHopSelectors is empty and the whole path becomes the final in-frame selector.
  3. Compare with planDeepXPathTarget("//iframe[1]//button"), which correctly hops.

Minimal Reproduction Code

Pure-function repro, no browser needed, in the style of packages/extension/tests/deep-locator.test.ts (verified on main, 2026-11-08: the positional form hops, the attribute form does not):

import { expect, it } from "vitest";
import { planDeepXPathTarget } from "../understudy/deepLocator.js";

it("plans a hop for an iframe selected by attribute predicate", () => {
  expect(planDeepXPathTarget("//iframe[@name='main']//button")).toEqual({
    frameHopSelectors: ["xpath=//iframe[@name='main']"],
    finalSelector: "xpath=//button",
  });
  // Actual: frameHopSelectors: [] — the iframe step is kept in the final
  // selector and resolved as an element in the parent frame.
});

Suggested direction

Broaden the hop test so the step name is checked before predicates are considered, e.g. strip all bracketed predicates for the name test (raw.replace(/\[[^\]]*\]/g, "")) or test /^i?frame\b/i against the token before the first [. Positional predicates on the hop selector should stay in buildXPathFromSteps output either way, since the hop selector itself must still select the right iframe element.

Related Issues

Are there any related issues or PRs?

  • Related to: #3240 (stale frame ownership references — same iframe-crossing machinery)
  • Duplicate of: none found as of 2026-11-08
  • Blocks: N/A

brennanbutler01 avatar Nov 09 '26 01:11 brennanbutler01