Skip to content

[java] Escape frame ids that are not valid CSS identifiers - #18274

Merged
diemol merged 4 commits into
SeleniumHQ:trunkfrom
Mochxd:fix-frame-id-selector
Nov 6, 2026
Merged

diemol merged 4 commits into
SeleniumHQ:trunkfrom
Mochxd:fix-frame-id-selector

Conversation

@Mochxd

@Mochxd Mochxd commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

🔗 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 builds frame#<id>. An id that starts with a digit, such as 1, is not a valid CSS identifier, so the driver rejects frame#1 with InvalidSelectorException and never switches.

The id is now escaped as a CSS identifier. A leading digit becomes a hex escape (1 becomes \31 ), and a space or a newline is escaped too. A plain id such as frameName is still sent as frame#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, #Foo matches id="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 still frame#frameName. All 68 tests pass.

On Chrome, testShouldBeAbleToSwitchToAnIframeWhoseIdStartsWithADigit fails on trunk with InvalidSelectorException. With this change it passes. The existing frame-id tests pass, and frame("QUIRKY") still finds id="quirky" on a page with no doctype.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s):
    • What was generated:
    • I reviewed all AI output and can explain the change

💡 Additional Considerations

.NET TargetLocator.Frame(string) builds frame# the same way. I am not changing that here.

🔄 Types of changes

  • Bug fix (backwards compatible)

switchTo().frame(String) built frame#<id>, so an id such as 1 was rejected as an invalid selector and the frame was never entered.
@selenium-ci selenium-ci added the C-java Java Bindings label Nov 2, 2026
@qodo-code-review

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

Copy link
Copy Markdown
Contributor

Code Review by Qodo

Grey Divider

Sorry, something went wrong

We weren't able to complete the code review on our side. Please try again manually by commenting /agentic_review on this PR.

Grey Divider

Qodo Logo

@qodo-code-review

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

[java] Escape frame IDs in CSS selectors

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Escape frame IDs that would otherwise produce invalid CSS selectors during frame switching.
• Preserve name-first lookup and ID-selector behavior on quirks-mode pages.
• Add selector-level and browser tests for escaped IDs and case-insensitive matching.
Diagram

graph TD
  A["frame(String)"] --> B["Name lookup"] --> C{"Name found?"}
  C -->|Yes| F["Switch frame"]
  C -->|No| D["Escape ID"] --> E["ID lookup"] --> G{"ID found?"}
  G -->|Yes| F
  G -->|No| H["NoSuchFrameException"]
Loading
High-Level Assessment

Keep the name-first lookup and escape the fallback ID within its existing # selector. Replacing it with an attribute selector would change case-matching behavior on quirks-mode pages.

Files changed (3) +114 / -3

Bug fix (1) +24 / -3
RemoteWebDriver.javaEscape frame IDs before CSS ID lookup +24/-3

Escape frame IDs before CSS ID lookup

• Separates escaping for the name string and ID identifier selectors. Escapes leading digits and special characters in IDs, and hex-escapes newlines and related characters in both lookup paths while retaining the '#' selector.

java/src/org/openqa/selenium/remote/RemoteWebDriver.java

Tests (2) +90 / -0
FrameSwitchingTest.javaCover digit-leading IDs and quirks-mode matching +26/-0

Cover digit-leading IDs and quirks-mode matching

• Adds browser-level tests that switch into an iframe with a digit-leading ID and confirm that ID lookup remains case-insensitive on a page without a doctype.

java/test/org/openqa/selenium/FrameSwitchingTest.java

RemoteWebDriverUnitTest.javaVerify emitted frame-lookup selectors +64/-0

Verify emitted frame-lookup selectors

• Checks the selectors sent for leading digits, spaces, leading hyphens, apostrophes, and newlines. The leading-digit case also verifies that a matched element is used to switch frames.

java/test/org/openqa/selenium/remote/RemoteWebDriverUnitTest.java

@diemol diemol left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Mochxd
Mochxd requested a review from diemol November 3, 2026 22:25
@Mochxd

Mochxd commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

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

@diemol

diemol commented Nov 6, 2026

Copy link
Copy Markdown
Member

/agentic_review

@qodo-code-review

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

Copy link
Copy Markdown
Contributor

Code Review by Qodo

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

Grey Divider


Action required

1. Test helper call fails formatter check ✓ Resolved

Resolving commit: 8ef0a28

Resolution reason
The wrapped two-line assertFrameLookups call in escapesALeadingHyphenInAFrameId was joined into a
single line of 100-column-compliant length (line 106), exactly as the suggested fix prompt
recommends.
Description
The escapesALeadingHyphenInAFrameId test wraps its assertFrameLookups(...) call across two
lines, but joined it is about 97 characters, under the 100-column limit. The repository formatter
(google-java-format via scripts/format.sh) would collapse it to one line, so the formatting check
on this changed file reports a diff.

Grey Divider

Context sources
Review mode: Auto: 🚀 Fast: Focused changes are test-only assertions for frame id selector escaping; localized, low risk.
✅ Fix-loop round: fix loop

Grey Divider

Tip of the day
💡 Did you know, you can switch off images and animations for a plain-text comment

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit 8ef0a28

Results up to commit c3c25f0 ⚖️ Balanced


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)


Action required
1. Test helper call fails formatter check ✓ Resolved

Resolving commit: 8ef0a28

Resolution reason
The wrapped two-line assertFrameLookups call in escapesALeadingHyphenInAFrameId was joined into a
single line of 100-column-compliant length (line 106), exactly as the suggested fix prompt
recommends.
Description
The escapesALeadingHyphenInAFrameId test wraps its assertFrameLookups(...) call across two
lines, but joined it is about 97 characters, under the 100-column limit. The repository formatter
(google-java-format via scripts/format.sh) would collapse it to one line, so the formatting check
on this changed file reports a diff.

Grey Divider

Qodo Logo

@diemol

diemol commented Nov 6, 2026

Copy link
Copy Markdown
Member

Please run ./scripts/format.sh

google-java-format keeps these calls on one line.
@Mochxd

Mochxd commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@diemol Ran google-java-format on the Java changes. The calls that fit on one line are on one line now.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 8ef0a28

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

C-java Java Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants