stagehand icon indicating copy to clipboard operation
stagehand copied to clipboard

locator().nth(-1) throws RangeError instead of selecting the last match (diverges from Playwright contract)

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 locator behavior, no LLM involved

Issue Description

Stagehand's locator API aims to mirror Playwright's, but nth() rejects negative indices. In Playwright, locator.nth(-1) selects the last match; negative indices count from the end of the match list. In Stagehand v4 the same call throws, and the fallback behavior for -1 is inconsistent across three entry points:

Entry point Behavior with -1
locator.nth(-1) throws RangeError("locator().nth() expects a non-negative index")
new Locator(frame, selector, options, -1) (constructor directly) silently treated as the "no explicit nth()" sentinel → resolves the first match (understudy/locator.ts: constructor normalizes normalized < 0 ? -1 : normalized; resolveNode() then uses index = this.nthIndex < 0 ? 0 : this.nthIndex)
page.deepLocator(sel).nth(-1) throws RangeError (DeepLocatorDelegate.nth), while the delegate's internal -1 default means "preserve every match" — a third, different meaning

So the same sentinel value means "not set" in one class, "preserve all" in another, and a hard error from the public nth() methods — while Playwright users migrating code will reasonably expect nth(-1) to click the last element.

Steps to Reproduce

  1. Create any locator against a page with multiple matches, e.g. page.deepLocator("//li").
  2. Call .nth(-1) (the idiomatic Playwright way to target the last item).
  3. Observe RangeError instead of the last match.

Minimal Reproduction Code

The assertion below runs without a browser against the real classes (verified on main, 2026-11-08 — all three entry points behave as described in the table above):

import { expect, it } from "vitest";
import { DeepLocatorDelegate } from "./understudy/deepLocator.js";
import { Locator } from "./understudy/locator.js";

it("nth(-1) should select the last match (Playwright contract)", () => {
  const d = new DeepLocatorDelegate(null as never, null as never, "//li");
  expect(() => d.nth(-1)).toThrow(RangeError); // ← currently throws; Playwright selects last match

  const loc = new Locator(null as never, "/html/body/div");
  expect(() => loc.nth(-1)).toThrow(RangeError); // ← same divergence
});

Error Messages / Log trace

RangeError: deepLocator().nth() expects a non-negative index
RangeError: locator().nth() expects a non-negative index

Suggested direction

Two options:

  1. Match Playwright: resolve matches and index from the end for negative values (e.g., resolve all, then index matches.length + index), and treat nthIndex >= 0 as "explicit narrowing". Keep -1 as the internal "no narrowing" sentinel only when it comes from the default, not from an explicit nth() call — e.g., track hasExplicitNth separately from the index value.
  2. Document the divergence in the locator docs if negative indexing is intentionally unsupported.

Option 1 removes a migration footgun for the Playwright audience and unifies the three -1 meanings.

Related Issues

Are there any related issues or PRs?

  • Related to: #3238 (locator nth/count performance — same resolution path)
  • Duplicate of: none found as of 2026-11-08
  • Blocks: N/A

brennanbutler01 avatar Nov 09 '26 01:11 brennanbutler01