Skip to content

Fix(v3): use XPath as fallback when the page has a shadow root - #3287

Merged
akeimach merged 1 commit into
v3from
fix/v3-xpath-native-children
Oct 21, 2026
Merged

akeimach merged 1 commit into
v3from
fix/v3-xpath-native-children

Conversation

@akeimach

@akeimach akeimach commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

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 override children on 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

  • resolveXPathAtIndex and countXPathMatches updates: 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.
  • Added an integration test in locator-count.spec.ts. The page has a shadow root and a host that overrides children, and locator(xpath).count() must return 1.

test plan

  • pnpm --filter @browserbasehq/stagehand test:core to run the new integration test

Summary 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 children property.

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.

View guided diff Turn on auto-fix

@changeset-bot

changeset-bot Bot commented Oct 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ef28c59

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@browserbasehq/stagehand Patch
@browserbasehq/stagehand-evals Patch
@browserbasehq/stagehand-server-v3 Patch

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

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading

View guided diff | Turn on auto-fix | Re-trigger cubic

@akeimach
akeimach merged commit 27ee07c into v3 Oct 21, 2026
254 checks passed
@akeimach
akeimach deleted the fix/v3-xpath-native-children branch October 21, 2026 21:45
@github-actions github-actions Bot mentioned this pull request Oct 21, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants