Skip to content

stagehand-python@4.5.0a0.dev1737

@seanmcguire12 seanmcguire12 tagged this 24 Nov 18:15
# why
users need to narrow a locator to a particular container or enter an
iframe without combining everything into one selector string. this
exposes locator chaining in typescript & python through the existing
chain resolver

```typescript
await page.locator(".card").nth(1).locator("button.save").click();
await page.locator("iframe#checkout").locator("button.submit").click();
```

# what changed
- added `Locator.locator()` in both sdks. each call returns a new
locator, preserving the original & the positions of queries & selections
in the chain.
- connected chain requests to existing actions, reads & counts. actions
use the exact resolved element; counts avoid exporting every match.
- support chained locators in screenshot masks,
`act()`/`observe()`/`extract()` scopes, & exclusions. masks & exclusions
use all final matches; scopes use the first final match. an unresolved
chain scope fails instead of broadening to the full page.
- preserve existing string requests & `>>` iframe traversal. ordinary
queries search the current document & its open/closed shadow roots;
chaining enters matched iframe documents.
- document the public methods & update api parity checks for chain
descriptors.

# test plan
added & extended tests covering:
- shared typescript/python fixtures for valid chains, malformed inputs,
ambiguous chain/selector requests, & selector normalization.
- independent locator branches, query/index order, return types, action
options, timeouts, & serialization of masks, scopes & exclusions in both
sdks.
- public browser operations within elements, nested/cross-origin
iframes, open/closed shadow roots, relative xpath queries, & retained
`>>` selectors.
- actions that still target the resolved element when a duplicate
selector match appears, plus fill, typing, option selection & file
uploads through chains.
- screenshot pixels proving all matches are masked, `nth()` masks only
the selected match, & overlays across documents are removed afterward.
- scoped ai calls in both sdks: model inputs contain the requested
subtree & omit excluded elements. snapshot tests also cover all-match
exclusions & missing scopes

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds `locator.locator()` chaining to the TypeScript and Python SDKs,
letting callers search within matched elements or enter iframe documents
without composing one selector string.

- Chains preserve the original locator, support `nth()` at any step, and
work alongside existing selectors and `>>` iframe hops.
- Chained locators work with actions, counts, screenshot masks, and
`act()`/`observe()`/`extract()` scopes and exclusions. Masks and
exclusions cover all final matches; scopes use the first and fail if
unresolved.
- Shadow-root presence checks are cached within a query batch so chained
steps skip rescanning the tree.
- Updates docs and API parity checks, adds shared fixtures, and includes
chaining tests in the integration suite.

<sup>Written for commit 3c5259a6263138617ce481989b13a30c8432cde9.
Summary will update on new commits.</sup>

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