Skip to content

[py] Fix Select escaping of trailing double quotes - #18292

Merged
diemol merged 2 commits into
SeleniumHQ:trunkfrom
Mohsintl:py-select-escape-trailing-quote
Nov 12, 2026
Merged

diemol merged 2 commits into
SeleniumHQ:trunkfrom
Mohsintl:py-select-escape-trailing-quote

Conversation

@Mohsintl

@Mohsintl Mohsintl commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

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

☐︎	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

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

•	Bug fix (backwards compatible)

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-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 9, 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 9, 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 changed the title [py] Fix Select XPath escaping for text ending with a double quote [py] Fix Select escaping of trailing double quotes Nov 9, 2026
@qodo-code-review

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can open the rule or relationship behind a finding in one click

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@diemol
diemol merged commit f3ed8fb into SeleniumHQ:trunk Nov 12, 2026
27 checks passed
This was referenced Nov 21, 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.

4 participants