Repository navigation
fix(extension): resolve XPath self steps against the document - #3142
Open
mikhail-koviazin wants to merge 1 commit into
Open
mikhail-koviazin wants to merge 1 commit into
mikhail-koviazin wants to merge 1 commit into
Conversation
parseXPathSteps took a `.` step for a tag name, so `.//div` and `//section/./div` matched nothing once a shadow root routed locators through the composed-tree parser, while document.evaluate() found them. Drop child-axis `.` steps: traversal already starts at the document, which is also the context node document.evaluate() uses.
🦋 Changeset detectedLatest commit: 2166665 The changes in this PR will be included in the next version bump. This PR includes changesets to release 24 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
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
This branch has not been deployed
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 document contains a shadow root, XPath locators go through the composed-tree parser instead of
document.evaluate().parseXPathStepsreads a.step as a tag name, and no element has the local name., so any path with a self step matches nothing:.//div,//section/./div,./html/body.document.evaluate()is called with the document as the context node, so natively.//divmeans//div. The composed-tree traversal starts at the document too, so the two should agree.Measured with the published
@browserbasehq/stagehand@4.0.0, fixture served over http. Without the shadow root every row agrees with native. With one unrelatedattachShadow()call on the page:document.evaluate().//div[@class='rel']//section/./div//section/.//div[@class='rel']./html/body/section/div//div[@class='rel']Fixture:
There is no error, only a count of 0, so a script that checks for the element concludes it is not on the page.
what changed
parseXPathStepsdrops a.step on the child axis. A leading.is the document, where traversal already starts, and/.selects the node it is applied to..inside a predicate ([.='Save']) is untouched, it never reaches this branch.If #3096 lands first, the only conflict is the neighbouring line in the group list in
scripts/test-integration.ts.test plan
packages/extension/tests/xpath-self-steps.test.ts(new, unit): a leading., a.step in the middle and at the end of a path, and a.predicate left as it was.packages/sdk-ts/tests/integration/locatorXPathSelfSteps.test.ts(new, integration): the first three rows of the table against real Chrome, plus the element the first match resolves to. Served over http, becausedata:URLs stay on the native engine and never reach this parser. Registered inscripts/test-integration.ts.On
main(7ad8cb6a) the integration test fails withexpected 0 to be 2in both cases. With the change it passes, and so dolocatorXPathTextPredicates,locatorCount,locatorNth,nestedDivandwaitForSelector, each run as its own file, and the extension unit suite.Summary by cubic
Fixes XPath self steps like
.//divand//section/./divreturning no matches when a shadow root in the document routes locators through the composed-tree parser. The parser treated.as a tag name, so these expressions matched nothing; now they resolve against the document, matchingdocument.evaluate()behavior..steps inparseXPathSteps; a leading.refers to the document where traversal starts, and/.selects nothing new.[.='Save']) untouched.Written for commit 2166665. Summary will update on new commits.