Skip to content

fixed deepLocator() pathing bug #880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Mochael
Copy link

@Mochael Mochael commented Jul 14, 2025

why

The logic in deepLocator was consolidating all the double // -> single /s, meaning that if I tried to do any sort of XPATH without specifying direct descendants, the query would break. So for example when I set my selector to `selector: xpath=//[@id='myID'] , it turns the locator into 'xpath=/[@id='myID']' removing the 2 slashes. This breaks my search because it's only looking for the ID in the root element, not across the descendants of the whole document (which is what I wanted)

what changed

I changed the logic so that it preserves the number of slashes in the xpath instead of splitting on "/", dropping all the slashes and then re-adding them

test plan

I tested locally but the stagehand team should verify that this works on whatever suite of tests they typically try. They should also probably add a test to catch this error like a search on non-direct descendants.

Copy link

changeset-bot bot commented Jul 14, 2025

⚠️ No Changeset found

Latest commit: b733aeb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR fixes a critical bug in the deepLocator() function's XPath handling. The original implementation incorrectly consolidated multiple forward slashes into single ones, which broke XPath queries using the descendant-or-self axis (//). For example, a query like //*[@id='myID'] would incorrectly become /*[@id='myID'], changing the semantic meaning from "find anywhere in the document" to "only look at root level". The fix preserves the number of slashes in XPath expressions by using a more precise string splitting approach.

PR Description Notes:

  • The test plan could be more specific about which test cases should be added

Confidence score: 4/5

  1. Safe to merge after proper testing, as this fixes a bug rather than introducing new features
  2. High confidence due to the clear bug fix and preservation of XPath semantics, but lacks comprehensive test coverage
  3. Files needing attention:
    • lib/handlers/handlerUtils/actHandlerUtils.ts - Needs additional test cases for non-direct descendant XPath queries

1 file reviewed, 1 comment
Edit PR Review Bot Settings | Greptile

Comment on lines 50 to 52
if (buffer.length === 0) {
return (ctx as Page | FrameLocator).locator("xpath=/*");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Potentially incorrect fallback for empty buffer case - returning '/*' will only match root level elements, not all elements in the frame

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.

1 participant