Skip to content

Commit ca7ba6b

Browse files
DavertMikclaude
andcommitted
refactor: extract simplifyHtmlElement to lib/html.js and add toSimplifiedHTML to WebElement
Move HTML simplification logic (removeNonInteractiveElements + unwrap + truncate) into a reusable simplifyHtmlElement() function in lib/html.js. Add toSimplifiedHTML() method to WebElement that combines toOuterHTML() with simplifyHtmlElement(). MultipleElementsFound.fetchDetails() now uses webEl.toSimplifiedHTML() directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c670e49 commit ca7ba6b

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

lib/element/WebElement.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from 'assert'
2+
import { simplifyHtmlElement } from '../html.js'
23

34
/**
45
* Unified WebElement class that wraps native element instances from different helpers
@@ -352,6 +353,11 @@ class WebElement {
352353
}
353354
}
354355

356+
async toSimplifiedHTML(maxLength = 300) {
357+
const outerHTML = await this.toOuterHTML()
358+
return simplifyHtmlElement(outerHTML, maxLength)
359+
}
360+
355361
_normalizeLocator(locator) {
356362
if (typeof locator === 'string') {
357363
return locator

lib/helper/errors/MultipleElementsFound.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Locator from '../../locator.js'
2-
import { removeNonInteractiveElements } from '../../html.js'
32

43
class MultipleElementsFound extends Error {
54
constructor(locator, webElements) {
@@ -25,17 +24,8 @@ class MultipleElementsFound extends Error {
2524
const webEl = this.webElements[i]
2625
try {
2726
const xpath = await webEl.toAbsoluteXPath()
28-
let outerHTML = await webEl.toOuterHTML()
29-
try {
30-
outerHTML = removeNonInteractiveElements(outerHTML)
31-
outerHTML = outerHTML.replace(/<html><head><\/head><body>(.*)<\/body><\/html>/s, '$1').trim()
32-
} catch (e) {
33-
// keep raw outerHTML if minification fails
34-
}
35-
if (outerHTML.length > 300) {
36-
outerHTML = outerHTML.slice(0, 300) + '...'
37-
}
38-
items.push(` ${i + 1}. > ${xpath}\n ${outerHTML}`)
27+
const html = await webEl.toSimplifiedHTML()
28+
items.push(` ${i + 1}. > ${xpath}\n ${html}`)
3929
} catch (err) {
4030
items.push(` ${i + 1}. [Unable to get element info: ${err.message}]`)
4131
}

lib/html.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,17 @@ function splitByChunks(text, chunkSize) {
245245
return chunks.map(chunk => chunk.trim())
246246
}
247247

248-
export { scanForErrorMessages, removeNonInteractiveElements, splitByChunks, minifyHtml }
248+
function simplifyHtmlElement(html, maxLength = 300) {
249+
try {
250+
html = removeNonInteractiveElements(html)
251+
html = html.replace(/<html><head><\/head><body>(.*)<\/body><\/html>/s, '$1').trim()
252+
} catch (e) {
253+
// keep raw html if minification fails
254+
}
255+
if (html.length > maxLength) {
256+
html = html.slice(0, maxLength) + '...'
257+
}
258+
return html
259+
}
260+
261+
export { scanForErrorMessages, removeNonInteractiveElements, splitByChunks, minifyHtml, simplifyHtmlElement }

0 commit comments

Comments
 (0)