fix: keep element text in simplifyHtmlElement - #5691
Open
DavertMik wants to merge 1 commit into
Open
Conversation
removeNonInteractiveElements dropped non-interactive descendants, so an interactive element whose label lives in nested spans was serialized as an empty shell. Add an opt-in keepText option, off by default, and use it from simplifyHtmlElement which describes a single element. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JbhzmY1M51pyRysUx32MdP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
simplifyHtmlElement()callsremoveNonInteractiveElements(), which strips non-interactive descendants. When an interactive element's visible label lives in nested<span>s (icon + badge + text button markup), everything inside is removed and the element serializes as an empty shell.Before:
After:
Why it matters
WebElement.toSimplifiedHTML()is the only caller, and it feedsMultipleElementsFound, whose job is to describe candidate elements so a human or an agent can tell them apart. With the labels stripped, two different menu items —New testandNew tests from requirement— rendered byte-identically, making the error message useless and any automated choice between them a guess.The change
removeNonInteractiveElements()gets an opt-inkeepTextoption,falseby default. When enabled, a non-interactive subtree is dropped only if it contains no visible text.simplifyHtmlElement()passeskeepText: true, since it describes a single element and that element's own text is exactly what the caller needs.removeNonInteractiveElements()'s existing behavior is unchanged: withkeepTextfalse the new condition short-circuits, and output on the full-page fixtures (test/data/github.html,test/data/testomat.html) is byte-identical to before.maxLengthtruncation still applies after simplification, as before.Tests
New
#simplifyHtmlElementblock intest/unit/html_test.jscovers the nested label, distinguishability of two similar labels, dropping of text-free nested markup,maxLengthtruncation, and thatremoveNonInteractiveElements()still strips the nested label by default.npx mocha test/unit --recursive --timeout 10000— 786 passing, 11 pending, 0 failing.