Skip to content

[fix] Compose chained nth locator selections - #3461

Draft
adity982 wants to merge 3 commits into
browserbase:mainfrom
adity982:fix/3444-nth-locator-composition
Draft

adity982 wants to merge 3 commits into
browserbase:mainfrom
adity982:fix/3444-nth-locator-composition

Conversation

@adity982

@adity982 adity982 commented Nov 15, 2026 •

Copy link
Copy Markdown

Summary

  • Preserve repeated nth() and first() calls as an index path through the TypeScript and Python SDKs.
  • Carry the path across the protocol and apply it in browser, frame, accessibility snapshot, and focus resolution.
  • Keep the existing single-index nth wire field for compatibility.

Fixes #3444

Validation

  • Protocol unit suite: 591 passed.
  • Focused TypeScript locator tests: 79 passed.
  • Focused Python locator and Stagehand tests: 82 passed, 1 skipped.
  • TypeScript package type checks and Python source type check passed.
  • Browser integration could not run in this Windows environment: Vitest reported File path cannot be resolved for all 10 integration tests.
  • The broader SDK TypeScript suite has unrelated platform/runtime failures under Node 26 on Windows; see local run report for details.

Summary by cubic

Fixes chained nth() and first() locator calls so earlier index selections are no longer lost, resolving #3444. Previously .nth(2).first() collapsed to a single index and selected the wrong element; the chained indices are now preserved as a path across both SDKs, the protocol, and extension resolution. count() on a chained locator now reflects the full path, returning 1 when the path resolves and 0 otherwise.

Protocol

  • Adds an optional nth_path wire field to all locator requests while keeping the existing nth field for backwards compatibility, and bumps the protocol to 3.2.0.
  • The extension applies the full index path in browser, frame, accessibility snapshot, and focus resolution instead of only the last index.

Written for commit f8d57f1. Summary will update on new commits.

View guided diff

@changeset-bot

changeset-bot Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f8d57f1

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run.
Approving the latest commit mirrors it into an internal PR owned by the approver.
If new commits are pushed later, the internal PR stays open but is marked stale until someone approves the latest external commit and refreshes it.

@github-actions github-actions Bot added external-contributor Tracks PRs mirrored from external contributor forks. external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. labels Nov 15, 2026
@iyuzhang

Copy link
Copy Markdown

One merge-order edge with #3443: this PR keeps nthIndex as the last entry of nthPath, while that count() fix checks only nthIndex < total, so if both land unchanged, nth(2).nth(1) on three matches would count as 1 despite matching nothing. Could the combined regression cover nth(2).first() → 1, nth(2).nth(1) → 0, and nth(99).first() → 0, so count() respects the whole path whichever PR lands second?

@adity982
adity982 force-pushed the fix/3444-nth-locator-composition branch from 5d4c2fd to 62cf7cb Compare November 17, 2026 16:25
@abhiramshaji-cyber

Copy link
Copy Markdown

Hey @iyuzhang and @adity982, hope you're both doing well! Thanks for catching this, iyuzhang. And adity982, the nthPath approach plus the counting chained locator paths table already covers exactly those three cases, really nice work.

I opened #3443, so I wanted to check how the two actually combine instead of guessing. I merged both branches locally:

  • Git stops with a conflict in count() and in locatorActions.test.ts in either order, so they can't land together silently.
  • Keeping this PR's nthPath block in count() plus both test tables, everything passes against a real headless Chrome: nth(2).first() is 1, nth(2).nth(1) is 0, nth(99).first() is 0, and [fix]: make count() respect nth() and first() #3443's nth(1) is 1 and nth(3) is 0 (locatorCount, 8 of 8).
  • Keeping [fix]: make count() respect nth() and first() #3443's nthIndex < total check instead makes your three chained cases fail, so your table already guards against the wrong resolution.

One thing on my side: #3443's unit table mocks selectorResolver.count, which your count() skips once nth() is set, so three of its cases fail after the merge. If yours lands first, I'll update #3443 to mock resolveAtIndexPath there and keep only the docs and the integration test. If #3443 lands first, would it help if I sent that conflict resolution as a small PR to your branch? Happy to go whichever way is easiest for you both and the maintainers.

This branch has not been deployed

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

Labels

external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. external-contributor Tracks PRs mirrored from external contributor forks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Locator first() and nth() replace an earlier nth instead of narrowing further (differs from Playwright)

3 participants