fix: XPath resolution for shadow DOM with <slot> elements #1736
Draft
miguelg719 wants to merge 2 commits intomainfrom
Draft
fix: XPath resolution for shadow DOM with <slot> elements #1736miguelg719 wants to merge 2 commits intomainfrom
miguelg719 wants to merge 2 commits intomainfrom
Conversation
1. packages/core/lib/v3/dom/rerenderMissingShadows.runtime.ts
Replaced cloneNode(true) with document.createElement(tag) to properly trigger custom
element constructors, plus attribute transfer and child migration.
2. packages/core/lib/v3/dom/locatorScripts/xpathResolver.ts (additional fix)
Two changes that fixed the actual XPath resolution failure:
- composedChildren - slot handling: Added support for <slot> elements by using
slot.assignedElements({ flatten: true }) to return distributed/assigned content
instead of fallback content. This is critical for pages using native shadow DOM with
<slot> elements.
- resolveXPathAtIndex - native-first fallback: Changed the hasShadow = true branch to
always try native document.evaluate() first before falling back to composed
resolution. Previously, when any shadow root existed on the page, native XPath was
completely skipped, even for XPaths that don't cross shadow boundaries.
3. Regenerated build artifacts
Both build/rerender-index.js, build/reRenderScriptContent.ts, and
build/locatorScripts.generated.ts were regenerated via pnpm run build-dom-scripts.
4. packages/core/tests/unit/rerender-missing-shadows.test.ts (new)
Unit tests for the rerenderMissingShadowHosts function (6 test cases). The tests need
the build:esm pipeline to pass to run, which has pre-existing @types/jsdom issues.
|
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
what changed
Changes Made
In
packages/core/lib/v3/dom/locatorScripts/xpathResolver.tsTwo changes that fixed the actual XPath resolution failure:
test plan
Summary by cubic
Fixes XPath resolution across native shadow DOM with slot elements and correctly re-renders shadow hosts, restoring Stagehand’s ability to find and fill form fields. Addresses STG-1433 “Could not find an element for the given xpaths.”
Written for commit 5ac5b4a. Summary will update on new commits. Review in cubic