Repository navigation
fix: preserve string literals in XPath shorthand selectors - #1201
Merged
codeskyblue merged 1 commit intoNov 2, 2026
Merged
Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
Device-side compatibility remains unverified because validation covers only lxml/PageSource rather than the Android XPath service, particularly for concat() and literal handling.
No open issues 🎉
What changed in this PR
This pull request fixes XPath shorthand selectors by encoding values as valid XPath 1.0 string literals, preserving quotes, backslashes, newlines, Unicode, and regex escapes.
Changes:
- Added XPath-safe literal encoding with
concat(...)support for values containing both quote types. - Applied encoding to text, content-description, resource-ID, and regex shorthand selectors.
- Added 84 lxml-backed regression tests covering matching behavior and literal edge cases.
| File | Description |
|---|---|
uiautomator2/xpath.py |
Implements XPath 1.0-compatible string literal encoding for shorthand selectors. |
tests/test_xpath_literals.py |
Adds comprehensive coverage for exact, substring, prefix, suffix, resource-ID, regex, and literal edge cases. |
Review effort: Lite
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.
XPath shorthand currently formats values with Python
repr(). XPath 1.0 does not interpret those backslash escapes: text containing both quote types can raiseXPathError, while backslashes and newlines can cause matching nodes to be missed. Regex shorthand such as^A\d+$also fails to matchA123.Encode values as XPath string literals, using
concat(...)when both quote types are present. Apply the same encoding to@resource-idshorthand. Regex patterns retain their original characters and semantics.Add 84 cases using real lxml/PageSource evaluation, covering exact, substring, prefix, suffix, resource-ID and regex selectors, matching and nonmatching nodes, and literal edge cases.
Validation on macOS 26.5.2 arm64, CPython 3.12.15 and lxml 6.1.3, based on master
bad641e7:test_missing_command_shows_help. Unchanged upstream has the same failure with 106 passes; that CLI behavior is covered separately by fix: show successful help for an empty u2cli invocation #1198.uv build.Android device tests, Windows/Linux and other Python versions were not run.