# why
`locator.locator()` chaining/resolution needs to preserve the order of
queries and positions: `.card -> nth(1) -> button` searches a different
scope from `.card -> button -> nth(1)`. this PR defines that internal
representation before exposing a public API
# what changed
- added canonical schemas and inferred types for CSS, XPath, and legacy
text queries, explicit frame hops, positional steps, and ordered chains
- every chain must start by finding elements before selecting a match by
index
- derived the existing selector engine's query types from the canonical
types, preserving its `kind` discriminator and parsing behavior
### note:
this PR defines the format only; it does not execute chains or add
public methods. RPC inputs, result shapes, and generated protocol/Python
artifacts are unchanged
# test plan
added `locator-chain.test-d.ts`, `locator-chain.test.ts`, & shared chain
fixtures covering:
- Type-level requirements for a leading query and subsequent query,
frame, and positional steps, including discriminated-union narrowing.
- Preservation of step order, repeated indices, XPath predicates,
whitespace, Unicode, and literal delimiters in selector text.
- Distinct representations for selecting an iframe, querying after it,
and explicit legacy frame hops. These validate shapes, not browser
traversal.
- Rejection of empty chains, leading positions/frame hops, missing
selectors, unknown engines/steps, extra fields, and invalid indices.
- Legacy CSS, XPath, text, and `>>` requests round-trip through every
locator RPC; internal chains remain rejected as executable requests.
- Scopes, exclusions, screenshot masks, and action results retain their
existing shapes, and generated protocol definitions do not advertise
chains.
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Defines the internal schema for `locator.locator()` chaining so chained
queries and positions preserve their order before exposing a public API.
This PR only defines the format; it does not execute chains or add
public methods. Existing RPC inputs, results, and generated protocol
artifacts are unchanged.
- Adds canonical schemas and inferred types for CSS, XPath, and legacy
text queries, explicit frame hops, positional steps, and ordered chains.
Every chain must start with a query before any `nth` or frame step.
- Derives the existing selector engine's query types from the new
canonical types, keeping its `kind` discriminator and parsing behavior.
- Tests validate type-level requirements, order preservation (including
repeated indices, whitespace, and Unicode), rejection of malformed
chains, and that legacy locator RPCs and schemas reject internal chains
while keeping their existing shapes.
<sup>Written for commit 78f1d1688586e9caf8ed91a279ae0705f3ee720f.
Summary will update on new commits.</sup>
<a
href="https://cubic.dev/pr/browserbase/stagehand/pull/3535?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<a
href="https://www.cubic.dev/action/auto-fix/pr/browserbase/stagehand/3535?returnTo=https%3A%2F%2Fgithub.com%2Fbrowserbase%2Fstagehand%2Fpull%2F3535&source=description"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img
alt="Turn on auto-fix"
src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->