Skip to content

[py] Find Select options by value with XPath - #18305

Merged
titusfortner merged 2 commits into
SeleniumHQ:trunkfrom
Mohsintl:py-select-by-value-quotes
Nov 15, 2026
Merged

titusfortner merged 2 commits into
SeleniumHQ:trunkfrom
Mohsintl:py-select-by-value-quotes

Conversation

@Mohsintl

Copy link
Copy Markdown
Contributor

🔗 Related Issues

None. This is the select_by_value follow-up I mentioned in #18292.

💥 What does this PR do?

select_by_value and deselect_by_value find 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 value with both quote types, like a'b"c. _escape_string returns concat(...), so the selector becomes option[value =concat("a'b", '"', "c")], which is invalid CSS. The browser throws a SyntaxError instead of selecting the option.
  • A value with a backslash, like a\b. In XPath a backslash is a plain character, but in a CSS string it starts an escape, so option[value ="a\b"] looks for ab and finds nothing.

This changes both methods to .//option[@value = ...] with By.XPATH. That's the locator Java, .NET, Ruby and JavaScript already use for select by value, and it's the language _escape_string was 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:

  • Old CSS vs new XPath in Chromium: querySelectorAll throws a SyntaxError for the both-quotes selector and finds 0 options for the backslash one. document.evaluate with the new XPath finds the option in both cases.
  • Random values: the new XPath, run through lxml, matches exactly the right option for 20k random values made of letters, spaces, both quotes and backslashes.

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

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s): Claude (Anthropic)
    • What was generated: it helped find the bug and wrote the fix and tests. I reviewed and checked all of it.
    • I reviewed all AI output and can explain the change

💡 Additional Considerations

The Java Quotes.escape issue I mentioned in #18292 (text ending in two double quotes) is separate, and I'll look at it on its own.

🔄 Types of changes

  • Bug fix (backwards compatible)

@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@CLAassistant

CLAassistant commented Nov 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@selenium-ci selenium-ci added C-py Python Bindings B-support Issue or PR related to support classes labels Nov 12, 2026
@selenium-ci

Copy link
Copy Markdown
Member

Thank you, @Mohsintl for this code suggestion.

The support packages contain example code that many users find helpful, but they do not necessarily represent
the best practices for using Selenium, and the Selenium team is not currently merging changes to them.

After reviewing the change, unless it is a critical fix or a feature that is needed for Selenium
to work, we will likely close the PR.

We actively encourage people to add the wrapper and helper code that makes sense for them to their own frameworks.
If you have any questions, please contact us

@Mohsintl
Mohsintl force-pushed the py-select-by-value-quotes branch from 00fa754 to 35c36e1 Compare November 12, 2026 11:07
@diemol

diemol commented Nov 13, 2026

Copy link
Copy Markdown
Member

/agentic_review

@qodo-code-review

qodo-code-review Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Backslash case tests a backspace instead ✓ Resolved

Resolving commit: 698c6bd

Resolution reason
Changed the entry to r"a\b" for the input value and r'.//option[@value = "a\b"]' for the expected
XPath, so both now contain a real backslash (\x5c) instead of a backspace (\x08). This matches the
suggested raw-string fix exactly.
Description
The VALUES entry meant to cover a literal backslash is written as the Python string "a\b", and
Python reads \b as a backspace character (U+0008). The same mistake appears in the expected XPath,
so both test functions compare backspace against backspace and pass without ever exercising the
backslash behaviour this PR is meant to fix. A regression that reintroduces CSS-style escape
handling for backslashes would not be caught by these tests.

Grey Divider

Context sources
✅ Linked PRs:
  🌐 PR #18292
Review mode: Auto: 🚀 Fast: Localized selector fix with focused tests and contained behavioral impact.

Grey Divider

Tip of the day
💡 Did you know, you can reply 'qodo' on any finding to push back, ask questions, or dig deeper

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@diemol

diemol commented Nov 13, 2026

Copy link
Copy Markdown
Member

@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.
@Mohsintl

Copy link
Copy Markdown
Contributor Author

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.

@titusfortner

Copy link
Copy Markdown
Member

Looks good, thanks.

This was referenced Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-support Issue or PR related to support classes C-py Python Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants