Repository navigation
[java] Escape frame ids that are not valid CSS identifiers - #18274
Conversation
switchTo().frame(String) built frame#<id>, so an id such as 1 was rejected as an invalid selector and the frame was never entered.
PR Summary by Qodo[java] Escape frame IDs in CSS selectors
AI Description
Diagram
High-Level Assessment
Files changed (3)
|
diemol
left a comment
There was a problem hiding this comment.
Thanks for this. The bug is real, and keeping the # selector so ids still match case-insensitively on pages without a doctype is a good call.
My concern is that this adds a third CSS escaper. W3CHttpCommandCodec.cssEscape() already handles everything here: the leading-digit hex escape, whitespace, and the newline hex escapes (from #18151). By.PreW3CLocator.cssEscape() is a near copy of it. With this PR, the same rules would live in three places, and the copies already differ: the By one doesn't have the newline fix.
Two separate patterns also aren't needed. As we saw in #18151, the identifier escaper is safe inside a quoted [name='...'] value, so one function covers both the name and the id lookups.
Could you move cssEscape into a single shared helper (for example a small class in org.openqa.selenium.internal) and use it from By.PreW3CLocator, W3CHttpCommandCodec, and RemoteWebDriver.frame(String)? That fixes the frame bug, removes duplicate code, and gives By the newline fix too. Your unit tests should still pass as they are, because the selectors sent to the driver would be the same.
The .NET Frame(string) has the same problem. Keeping it out of this PR is fine; we can track it separately.
frame(String), By, and the W3C codec each escaped selectors separately, so the frame lookup could drift from the codec rules. One helper keeps the identifier escaping, including newline hex escapes, in a single place.
|
@diemol Moved cssEscape to org.openqa.selenium.internal.Css. By.PreW3CLocator, W3CHttpCommandCodec, and RemoteWebDriver.frame(String) all use it, and the name and id lookups share that one result. The unit tests needed two expectation updates. The name lookup goes through the same escaper now, so "1" is sent as frame[name='\31 '] and "frame 1" as frame[name='frame\ 1']. The id selectors are unchanged, and those name selectors still match the same element. By.name still builds its own selector string, so it does not use the helper. A remote find goes through the codec, which does escape the value. I left that path as it is. .NET Frame(string) stays out of this PR. |
|
/agentic_review |
Code Review by Qodo
1.
|
|
Please run |
google-java-format keeps these calls on one line.
|
@diemol Ran google-java-format on the Java changes. The calls that fit on one line are on one line now. |
|
Code review by qodo was updated up to the latest commit 8ef0a28 |
🔗 Related Issues
No issue.
💥 What does this PR do?
RemoteWebDriver.switchTo().frame(String)looks up a frame by name, then by id. The id lookup buildsframe#<id>. An id that starts with a digit, such as1, is not a valid CSS identifier, so the driver rejectsframe#1withInvalidSelectorExceptionand never switches.The id is now escaped as a CSS identifier. A leading digit becomes a hex escape (
1becomes\31), and a space or a newline is escaped too. A plain id such asframeNameis still sent asframe#frameName. The name lookup is unchanged, apart from a newline, which CSS would otherwise drop when it follows a backslash.The id selector stays a
#selector. On a page with no doctype,#Foomatchesid="foo", and[id='Foo']does not. Frameset pages are often in that mode.🔧 Implementation Notes
I ran
RemoteWebDriverUnitTest. The new examples check the selector for a leading digit, a space, a hyphen, an apostrophe, and a newline. A plain id is stillframe#frameName. All 68 tests pass.On Chrome,
testShouldBeAbleToSwitchToAnIframeWhoseIdStartsWithADigitfails on trunk withInvalidSelectorException. With this change it passes. The existing frame-id tests pass, andframe("QUIRKY")still findsid="quirky"on a page with no doctype.🤖 AI assistance
💡 Additional Considerations
.NET
TargetLocator.Frame(string)buildsframe#the same way. I am not changing that here.🔄 Types of changes