Repository navigation
Description
Before submitting an issue, please:
- Check the documentation for relevant information
- Search existing issues to avoid duplicates
Environment Information
Stagehand:
- Language/SDK: TypeScript
- Stagehand version: 4.4.0 (main
d9cb8ccas 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
- Create any locator against a page with multiple matches, e.g.
page.deepLocator("//li"). - Call
.nth(-1)(the idiomatic Playwright way to target the last item). - Observe
RangeErrorinstead 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:
- Match Playwright: resolve matches and index from the end for negative values (e.g., resolve all, then index
matches.length + index), and treatnthIndex >= 0as "explicit narrowing". Keep-1as the internal "no narrowing" sentinel only when it comes from the default, not from an explicitnth()call — e.g., trackhasExplicitNthseparately from the index value. - 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: Text locator count and nth lookups take seconds on large repeated lists #3238 (locator
nth/count performance — same resolution path) - Duplicate of: none found as of 2026-11-08
- Blocks: N/A
Activity