# why
- users need to be able to query by text, with typed options for exact
matching & scoping
- this PR adds `getByText()` in typescript & `get_by_text()` in python
eg:
```typescript
await page.getByText("Save", { exact: true }).click();
await page.locator(".card").nth(1).getByText("Details").click();
await page.locator("iframe#checkout").getByText("Place order").click();
```
# what changed
- added string-only text queries with case-insensitive substring
matching by default & case-sensitive equality with `exact`. both modes
normalize whitespace & remove zero-width spaces & soft hyphens from the
query & element text.
- combine text across inline descendants & use button, submit & reset
input values. skip script, style, head, template & noscript content.
- `getByText()` will prefer matching descendants over ancestors. a
matching shadow root's combined text also suppresses its host, even when
there is no child element to return. scoped text queries can match the
scope element itself.
- search both open & closed shadow roots, with no visibility
requirement. entering iframe documents requires chaining.
- return ordinary lazy locators supporting actions, further chaining,
`count()`, `first()` & `nth()`. typed queries use batched resolution;
existing `locator("text=...")` behavior stays unchanged
# test plan
added & extended tests covering:
- substring/exact matching, case, whitespace, invisible characters,
unicode, empty queries, inline text, input values, skipped content &
hidden elements.
- parent/child selection, matching the scope itself, overlapping scopes,
refreshed text after mutations, & unchanged legacy text matching.
- open/closed shadow roots, nested roots, slotted text without
double-counting, & hosts with direct or empty shadow text.
- lazy query construction, independent locator branches, query/index
order, action options, invalid inputs & shared typescript/python wire
fixtures.
- public reads, counts & actions, including explicitly chained iframe
queries in both sdks & text queries inside cross-origin frames & shadow
roots.
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds `getByText()` to the TypeScript SDK and `get_by_text()` to the
Python SDK, so callers can find elements by rendered text on pages,
locators, and matched iframes.
Matching normalizes whitespace and removes zero-width spaces and soft
hyphens from both the query and DOM text. Default is case-insensitive
substring; `exact: true` switches to case-sensitive whole-text equality.
Text is combined across inline descendants, button/submit/reset inputs
use their `value`, and script, style, head, template, and noscript
content is ignored. Both open and closed shadow roots are searched,
including shadow roots whose text suppresses their host; closed roots
require an `http:` or `https:` page. Matching prefers the smallest
descendant, scoped queries can match the scope element itself, and
iframe documents require an explicit chain step. Returned locators are
lazy and support chaining, `count()`, `first()`, and `nth()`. Existing
`locator("text=...")` behavior is unchanged.
<sup>Written for commit c51776e21a1b56b59b6f51b0d168950d6131761b.
Summary will update on new commits.</sup>
<a
href="https://cubic.dev/pr/browserbase/stagehand/pull/3541?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/3541?returnTo=https%3A%2F%2Fgithub.com%2Fbrowserbase%2Fstagehand%2Fpull%2F3541&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. -->