Skip to content

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

Open

Description

@brennanbutler01

Before submitting an issue, please:

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions