Repository navigation
[py] Find Select options by value with XPath - #18305
Conversation
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. |
00fa754 to
35c36e1
Compare
|
/agentic_review |
Code Review by Qodo
1.
|
|
@Mohsintl please check Qodo's comments. |
The case was written "a\b", which Python reads as a backspace, so the backslash path was never exercised. Use raw strings for the value and the expected XPath.
|
Thanks @diemol, Qodo is right: "a\b" was a backspace, so the backslash case wasn't really tested. Fixed with raw strings in the latest commit. |
|
Looks good, thanks. |
🔗 Related Issues
None. This is the
select_by_valuefollow-up I mentioned in #18292.💥 What does this PR do?
select_by_valueanddeselect_by_valuefind options with a CSS selector,option[value =...], but escape the value with_escape_string, which makes an XPath string literal. That breaks two kinds of values:a'b"c._escape_stringreturnsconcat(...), so the selector becomesoption[value =concat("a'b", '"', "c")], which is invalid CSS. The browser throws a SyntaxError instead of selecting the option.a\b. In XPath a backslash is a plain character, but in a CSS string it starts an escape, sooption[value ="a\b"]looks foraband finds nothing.This changes both methods to
.//option[@value = ...]withBy.XPATH. That's the locator Java, .NET, Ruby and JavaScript already use for select by value, and it's the language_escape_stringwas written for.🔧 Implementation Notes
The other option was a separate CSS string escaper for these two methods. Switching to XPath is the smaller change, reuses the existing escaper, and matches the other bindings.
Added unit tests to
py/test/unit/selenium/webdriver/support/select_tests.py, using a fake select element that records the locator it's asked for. No browser is needed. 8 of the new cases fail on trunk and pass with this change.I also checked:
querySelectorAllthrows a SyntaxError for the both-quotes selector and finds 0 options for the backslash one.document.evaluatewith the new XPath finds the option in both cases.As with #18292, I ran the unit tests with pytest because I couldn't get Bazel working here. ruff 0.17.4 check and format are clean.
🤖 AI assistance
💡 Additional Considerations
The Java
Quotes.escapeissue I mentioned in #18292 (text ending in two double quotes) is separate, and I'll look at it on its own.🔄 Types of changes