Skip to content

stagehand-python@4.5.0a0.dev1733

@seanmcguire12 seanmcguire12 tagged this 23 Nov 20:16
# why
resolving all matches repeated the selector query for each index and
released each remote handle separately. this is wasteful. we should
collect matches once in the browser, & then return only the results the
operation needs.

# what changed
- added CSS, text, and XPath collectors with document/element scopes,
preserving match order, open/closed shadow-root traversal, and existing
selector behavior
- added count, selected-range, and all-match resolution modes. Counts
return a number; first/nth exports only the selected element; batches
export their handles through one `Runtime.getProperties` response
- updated screenshot/PDF masks and snapshot exclusions to own a batch
until geometry or node IDs have been read, then release its array and
handles together.
- bound cleanup to the originating CDP session & added cleanup for
results arriving after timeout. separately selected action handles keep
their own lifetime

# test plan
added `selectorCollection.test.ts` and extended the resolver deadline
and consumer tests to cover:
- CSS, text, & XPath result order across document/element scopes,
overlapping scopes, and open/closed shadow roots, including empty and
malformed selectors
- Native CSS combinators, innermost text matching, document XPath shadow
hops, and element-relative XPath behavior.
- Real-Chrome CDP traffic at 500 and 1,000 matches for each engine:
count uses one call; first/nth uses three including handle release;
batch collection/export uses three including group release
- Complete all-match results and selected-range results, with no
per-element node requests for counts or batch export. First/nth
materializes only its target.
- Actual browser handle ownership: releasing a batch invalidates its
array and exported handles while another batch and a separately selected
target remain usable.
- Cleanup after navigation, evaluation/export exceptions, session
closure, and late success or rejection after timeout, including bounded
stalled cleanup and cleanup on the original session after frame
ownership changes.
- Screenshot masks and snapshot exclusions release batches after use or
expiry; an SDK screenshot-mask smoke confirms subsequent locator
operations still work.


<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Fixes selector resolution so all matches are collected once in the page
instead of rerunning the query for each index and releasing handles one
by one.

- Adds CSS, text, and XPath collectors that preserve match order and
traverse open and closed shadow roots across document or element scopes.
- Selector resolution now supports count, range, and all-match modes;
count returns a number, first/nth exports only the selected element, and
batches export their handles in one `Runtime.getProperties` response.
- Screenshot masks and snapshot exclusions own a batch until geometry or
node IDs are read, then release the array and handles together.
- Batch cleanup follows the originating CDP session and also covers
results that arrive after the deadline.

<sup>Written for commit 11a270d7c8a8fcf72924050c3ea006bef54dc8be.
Summary will update on new commits.</sup>

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