Repository navigation
[py] Fix Select escaping of trailing double quotes - #18292
Conversation
When text contained both quote types, _escape_string split on '"' and then also appended a closing quote if the text ended with one. Python's str.split keeps the trailing empty part, so the quote was emitted twice and the generated XPath matched the text plus an extra '"'. Options like it's "quoted" could never be selected by visible text. Build the concat() from the actual pieces instead, so every quote in the text appears exactly once.
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
Thank you, @Mohsintl for this code suggestion. The support packages contain example code that many users find helpful, but they do not necessarily represent After reviewing the change, unless it is a critical fix or a feature that is needed for Selenium We actively encourage people to add the wrapper and helper code that makes sense for them to their own frameworks. |
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can open the rule or relationship behind a finding in one click |
None, I found this while reading the Select code.
💥 What does this PR do?
select_by_visible_text can't select an option like it's "quoted": text that has both quote types and ends with a double quote.
_escape_string builds an XPath concat() for that case. It splits on " and then adds one more " if the text ends with one. But Python's split keeps the empty string at the end, so the last quote gets added twice. For it's "quoted" the XPath comes out as concat("it's ", '"', "quoted", '"', "", '"'), which is it's "quoted"", so nothing matches.
Java's Quotes.escape doesn't hit this because Java's split drops trailing empty strings.
🔧 Implementation Notes
I changed it to build concat() from the pieces directly: a '"' between pieces, and empty pieces skipped. So every quote in the text shows up exactly once. Text with only one kind of quote (or none) is handled exactly as before.
Added py/test/unit/selenium/webdriver/support/select_tests.py. It's a unit test, no browser needed. 4 of the cases fail on trunk and pass with this change.
I also checked the output against lxml's XPath engine on 20k random strings made of letters, spaces and both quotes, and every one matched. I ran the Python unit tests with pytest because I couldn't get Bazel in my environment, so CI will be the real Bazel run. ruff 0.17.4 check and format are clean.
🤖 AI assistance
💡 Additional Considerations
Java's Quotes.escape looks like it could lose a quote when the text ends with two double quotes (like x'""), because Java's split drops all trailing empty strings. I haven't run the Java side, so I left it out of this PR.
select_by_value has a separate problem (it uses this XPath escaper inside a CSS selector). I'll open a separate PR for that.
🔄 Types of changes