Repository navigation
Description
Hey Stagehand team, hope you're doing well!
Calling first() or nth() on a locator that is already narrowed replaces the earlier index instead of narrowing further, so it can act on a different element than Playwright would.
Before submitting an issue, please:
- Check the documentation for relevant information
- Search existing issues to avoid duplicates
Environment Information
Stagehand:
- Language/SDK: TypeScript (the Python SDK sends the same single
nth) - Stagehand version: 4.4.0 (main at
9bb60e2)
AI Provider:
- Provider: n/a, no model calls involved
- Model: n/a
Issue Description
What happens today:
- A locator keeps a single
nthnumber in its descriptor (locator.ts#L160-L175, locator.py#L291-L300, wire schema schemas.ts#L456). first()isnth(0), andnth(k)spreads the old descriptor and writesnth: kover it.- So
page.locator("li").nth(2).first()sends{ selector: "li", nth: 0 }, and the click lands on the firstliinstead of the third one the code already picked.
The runtime then resolves that single index against the full match list (runtime.ts#L1174, understudy/locator.ts#L899-L915, deepLocator.ts#L201-L213).
Where this bites in practice: a helper that receives an already narrowed locator and calls .first() on it silently acts on a different element, with no error.
Expected behavior
In Playwright, locator.nth() and locator.first() narrow the elements the current locator already matches, so the calls compose. A locator narrowed to one element has exactly one match, so first() / nth(0) keep it and any other index matches nothing.
Steps to Reproduce
- Load a page with 5
lielements. - Read text through the three chained locators below.
- Compare with the same chains in Playwright.
Minimal Reproduction Code
import { localBrowser, Stagehand } from "@browserbasehq/stagehand";
const browser = await localBrowser.launch({ headless: true });
const stagehand = await Stagehand.create({ browser });
const page = await browser.context.newPage(
"data:text/html,<ul><li>li0</li><li>li1</li><li>li2</li><li>li3</li><li>li4</li></ul>",
);
const items = page.locator("li");
console.log(await items.nth(2).first().innerText());
console.log(await items.nth(1).nth(1).innerText());
console.log(await items.first().nth(2).innerText());
await stagehand.close();
await browser.close();What I see in real Chrome, next to Playwright:
chain Stagehand Playwright
nth(2).first() li0 li2
nth(1).nth(1) li1 no match (count 0)
first().nth(2) li2 no match (count 0)
Error Messages / Log trace
None, the call succeeds on the other element.
Related Issues
I noticed there's a test that locks in the current replacing behaviour: objectWrapper.test.ts#L2192-L2204 asserts that page.locator("button").first().nth(2) sends nth: 2. So this might be on purpose, and I didn't want to assume it's a bug.
The docs read a bit differently though: locator.mdx#L7 says first() and nth() "refine the local locator descriptor", and the Playwright migration guide says "first() and nth() work" (playwright.mdx#L212) and maps them as "Same names" (playwright.mdx#L617).
There's some really nice locator work in flight nearby, which I read through before filing: #3415 / #3417 (negative nth), the locator builder PRs #3188 and #3191 to #3193 (keeping an earlier nth when a descendant search follows), and #3308 (nth performance). As far as I can tell none of them change how nth chains.
I found this while fixing count() ignoring nth() in #3443, which is a separate change.
Which behaviour would you prefer here?
- (a) compose like Playwright: if the locator is already narrowed,
first()/nth(0)keep it and any other index matches nothing - (b) throw a clear error when
nthis chained, so nobody gets the wrong element silently - (c) keep it as is and spell out the replacing behaviour in the docs
Happy to send a PR once you pick one, and I'd really appreciate any feedback or tips on how you'd like it done. Thanks so much!
Activity