Skip to content

stagehand-python@4.5.0a0.dev1735

@seanmcguire12 seanmcguire12 tagged this 23 Nov 22:22
# why
locator chaining needs each query to search inside the previous matches.
for example, `.card -> nth(1) -> button` should find buttons inside the
second card, while `.card -> button -> nth(1)` should select the second
button across all cards.

this adds the internal resolver for that behavior. it keeps intermediate
matches inside the browser & returns only what the caller needs: a
count, one selected element, or every final match.

# what changed
- added `resolveElementChain()` on top of the existing batched selector
collector. queries run in order, duplicate elements are removed before
indexing, & each `nth` applies where it appears in the chain.
- kept queries within their element scopes, including open & closed
shadow roots, while preserving existing css, xpath, & text matching
behavior.
- added `FrameElementChainResolver` with count, selected, & all-target
modes. count returns a number without exporting elements; selected mode
obtains only the chosen element's backend node id. all-target mode
obtains every final id.
- release temporary browser handles after the required ids have been
captured. one deadline covers readiness through resolution, with cleanup
for failures, cancellation, & responses arriving after timeout

# test plan

added `elementChainResolution.test.ts` & `elementChainResolver.test.ts`
covering:
- nested scopes, indices before/after queries, repeated indices, & empty
results that stay empty rather than restarting the search at the
document.
- overlapping scopes preserve their order & return each element once.
distinct elements with identical ids or text remain separate matches.
- real-browser queries across open & closed shadow roots, native css
relationships, scoped xpath behavior, & exclusion of the scope itself,
siblings, & ancestors.
- counts & selected targets at 500 & 1,000 matches: count uses two
browser protocol calls including cleanup; selected uses three.
all-target mode batches collection & export but still describes each
final element to obtain its backend id.
- temporary arrays & handles become unusable after cleanup, while
returned ids still resolve to the exact selected element. later
resolution does not release a handle owned by a consumer.
- shared deadlines, cancellation, session changes, page exceptions, late
success or failure, & stalled cleanup. failures remain errors rather
than empty matches

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds element chain resolution so `nth` indices apply at their position
in a locator chain instead of only at the end.

- `.card -> nth(1) -> button` now finds buttons inside the second card;
`.card -> button -> nth(1)` selects the second button across all cards.
- Intermediate matches stay in the browser; the resolver returns only a
count, one selected element, or every final element.
- Queries run within their element scopes, including explicit document
scopes, open and closed shadow roots, and preserve existing CSS, XPath,
and text matching; non-element XPath nodes never enter a chain.
- Frame entry is rejected; iframes remain selectable as elements but
cross-frame transitions are left to the existing cross-frame resolver.
- Temporary browser handles are released after needed backend node IDs
are captured, with one shared deadline for readiness, resolution, and
cleanup, including late responses and cancellations.

**Tests**
- Adds unit tests for result ownership, cancellation, shared deadlines,
index validation, and cleanup edge cases.
- Adds real-browser tests covering nested scopes, overlapping scopes,
shadow roots, deduplication, `nth` positions, empty results, and
500/1,000-match counts and selections.

<sup>Written for commit 93809484088e30802ec022e7b1a428c6c78ad4f4.
Summary will update on new commits.</sup>

<a
href="https://cubic.dev/pr/browserbase/stagehand/pull/3536?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/3536?returnTo=https%3A%2F%2Fgithub.com%2Fbrowserbase%2Fstagehand%2Fpull%2F3536&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. -->
Assets 2
Loading