Repository navigation
Fix(v3): use XPath as fallback when the page has a shadow root - #3287
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: ef28c59 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
There was a problem hiding this comment.
No issues found across 3 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant Locator as Locator API
participant Resolver as XPath Resolver
participant Composed as Composed Traversal
participant ShadowHop as Shadow-Hop Traversal
participant NativeXPath as Native XPath Engine
participant DOM as Browser DOM
Note over Locator,DOM: XPath Resolution Flow
Locator->>Resolver: resolveXPathAtIndex(xpath, index)
Resolver->>Composed: Get composed matches
Composed->>DOM: Traverse using composed tree
DOM-->>Composed: Match results
Composed-->>Resolver: composedMatches[]
alt composedMatches.length > 0
Resolver-->>Locator: Return match at index or null
else CHANGED: composedMatches empty
Resolver->>ShadowHop: Get shadow-hop matches
ShadowHop->>DOM: Traverse reading element.children
DOM-->>ShadowHop: Match results (may be 0 if children overridden)
ShadowHop-->>Resolver: shadowHopMatches[]
alt NEW: shadowHopMatches empty (children override detected)
Resolver->>NativeXPath: resolveNativeAtIndexWithError(xpath, index)
Note over NativeXPath: Native XPath reads actual DOM,<br/>ignores children override
NativeXPath->>DOM: Query with native XPath
DOM-->>NativeXPath: Match result
NativeXPath-->>Resolver: Value at index
Resolver-->>Locator: Return native match
else shadowHopMatches.length > 0
Resolver-->>Locator: Return match at index or null
end
end
Note over Locator,NativeXPath: Count Flow (parallel path)
Locator->>Resolver: countXPathMatches(xpath)
Resolver->>Composed: Get composed matches
Composed->>DOM: Traverse composed tree
DOM-->>Composed: Match results
Composed-->>Resolver: composedCount
alt composedCount > 0
Resolver-->>Locator: Return composedCount
else CHANGED: composedCount is 0
Resolver->>ShadowHop: Get shadow-hop matches
ShadowHop->>DOM: Traverse reading element.children
DOM-->>ShadowHop: Match results
ShadowHop-->>Resolver: shadowHopCount
alt NEW: shadowHopCount is 0
Resolver->>NativeXPath: resolveNativeCountWithError(xpath)
NativeXPath->>DOM: Native XPath count query
DOM-->>NativeXPath: Match count
NativeXPath-->>Resolver: Native count (ignores children override)
Resolver-->>Locator: Return native count
else shadowHopCount > 0
Resolver-->>Locator: Return shadowHopCount
end
end
seanmcguire12
approved these changes
Oct 21, 2026
Merged
seanmcguire12
pushed a commit
that referenced
this pull request
Nov 18, 2026
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to v3, this PR will be updated. # Releases ## @browserbasehq/[email protected] ### Patch Changes - [#3150](#3150) [`69c2440`](69c2440) Thanks [@miguelg719](https://github.com/miguelg719)! - Fix OpenAI strict-schema rejection of the nullable `act` action while still stripping unknown keys when parsing. - [#3114](#3114) [`f6b3d85`](f6b3d85) Thanks [@miguelg719](https://github.com/miguelg719)! - Fix CAPTCHA awareness options for local Browserbase attachments and forward them through the API. - [#3491](#3491) [`57b905e`](57b905e) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Fix clicks on partially occluded elements - [#3132](#3132) [`09063f6`](09063f6) Thanks [@shubh24](https://github.com/shubh24)! - Use automatic tool choice for the final agent `done` call with Claude Sonnet 5.5, Opus 5.5, and Fable 5.1. - [#3287](#3287) [`27ee07c`](27ee07c) Thanks [@akeimach](https://github.com/akeimach)! - Fix XPath resolution for light DOM elements when the page has a shadow root and a host element overrides `children`. ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies \[[`69c2440`](69c2440), [`f6b3d85`](f6b3d85), [`57b905e`](57b905e), [`09063f6`](09063f6), [`27ee07c`](27ee07c)]: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - [#3312](#3312) [`e11bbe5`](e11bbe5) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Rebuild the standalone server binaries with Node.js 24.22.1 and Undici 7.30.1 to include the October 2026 security fixes. Binary builds now reject outdated Node versions and rebuild instead of reusing cached executables. - Updated dependencies \[[`69c2440`](69c2440), [`f6b3d85`](f6b3d85), [`57b905e`](57b905e), [`09063f6`](09063f6), [`27ee07c`](27ee07c)]: - @browserbasehq/[email protected] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
why
When the page has a shadow root, the resolver walks the tree with its own composed and shadow-hop traversal, and that walk reads
element.children. However, some component libraries overridechildrenon a host element, leading to the walk stopping at the host and finding 0 matches.XPath is able to find the element in these cases since it sees the actual DOM, so including XPath as a fallback option solves this issue.
what changed
resolveXPathAtIndexandcountXPathMatchesupdates: if composed and shadow-hop traversal both find 0 matches, use native XPath. The fallback is used only when there are no matches at all, not when the index is out of range. Native XPath cannot cross a shadow boundary, so it cannot change results for shadow DOM paths that already resolve.locator-count.spec.ts. The page has a shadow root and a host that overrideschildren, andlocator(xpath).count()must return 1.test plan
pnpm --filter @browserbasehq/stagehand test:coreto run the new integration testSummary by cubic
Fixes XPath resolution so locators still find light DOM elements when a page has a shadow root and a host element overrides its
childrenproperty.Because the composed and shadow-hop traversal reads
element.children, component libraries that override that property could cause locators to find 0 matches. When both traversals return no matches, the resolver now falls back to native XPath, which ignores the override. The fallback only fires when there are no matches at all, so it doesn't affect shadow DOM paths that already resolve.Written for commit ef28c59. Summary will update on new commits.