Skip to content

Scrape Obfuscated Text with Playwright: Recover the Price Visitors Actually See

A page can show a clean price while its markup contains a different sequence, extra invisible digits, or no currency symbol at all. This guide explains how to diagnose that gap and rebuild the visible string with browser evidence instead of trusting raw DOM text.

Diagnose the mismatch before writing a workaround

Start by separating three possible failures. First, the page may simply be incomplete because JavaScript has not finished. Second, the DOM may contain characters that CSS hides from people. Third, CSS or JavaScript may change the order, generation, or timing of the visible characters. Each failure needs a different check, so compare the rendered price with textContent and innerText before choosing a fix.

A useful diagnostic sequence is: confirm the correct element, let the page render, compare both DOM readings with the screen, inspect the markup for split or shuffled fragments, check computed visibility and direction styles, look for generated content, and examine the active font. Temporarily disabling a suspicious rule in developer tools can confirm which mechanism is responsible.

Five CSS patterns that make DOM text misleading

Flexbox can place fragments in DOM order while displaying them in another order. In the captured example, the cents precede the dollars in markup, yet visual ordering produces a normal price. Neither DOM text property follows that visual order.

Pseudo-elements create the opposite problem: the browser can display a complete price that is absent from the DOM. Hiding the original value and generating a replacement through ::before is the captured pattern. Because generated content is not a DOM text node, ordinary extraction returns the hidden placeholder.

Decoy characters are inserted between real characters and concealed with display, visibility, opacity, or a zero font size. The hiding method matters: innerText may omit some hidden forms while still including others, so switching properties is not a complete solution.

Bidirectional properties can reverse the displayed sequence while leaving the stored string unchanged. Reversing every extracted string is unsafe on real pages because nested elements may mix directions and combine this trick with others.

A custom font can also remap glyphs, so the stored characters are already in the right order but look like different symbols. In the captured example, the HTML contains 123456 and visitors see $79.50. The exact mapping is: 1 displays as $, 2 displays as 7, 3 displays as 9, 4 displays as ., 5 displays as 5, and 6 displays as 0. That mapping is loaded through the @font-face ObfuscatedPrices definition in protected module code0011. Position sorting cannot solve that case; the font mapping itself must be investigated.

These methods can be layered. The captured combined example displays $79.50 while raw extraction produces .560978: Flexbox changes fragment order, bidirectional styling reverses digits, invisible characters add digits, and a nested pseudo-element supplies the dollar sign. That example shows why a single-property fix often fails.

Choose the least complex method that matches the evidence

Use this decision order:

  • Try innerText on a fully rendered page for simple hidden elements.
  • Inspect computed styles when extra characters appear.
  • Inspect ::before and ::after computed content when visible text is missing from the DOM.
  • Reconstruct from character geometry when order or direction is wrong.
  • Check structured metadata or attributes when an original value may already be present.
  • Use OCR only when DOM and layout evidence remain unreliable.
  • Use AI to interpret recovered or visual evidence, not as a substitute for that evidence.

The captured comparison table qualifies browser need for each approach: innerText, computed styles, pseudo-element inspection, and geometry generally need a browser; alternative data may not; OCR usually needs a browser to capture the rendered page; AI depends on the chosen method. Combining approaches is often necessary. For example, geometry can restore order, computed styles can remove decoys, and pseudo-element inspection can restore generated symbols.

Rebuild visible text from geometry plus generated content

The reusable approach has two extraction layers. The first walks text nodes, segments them into grapheme clusters, measures each visible character with a Range, filters hidden fragments, and sorts by screen position. This handles many reordering, reversal, and invisible-character cases without naming the CSS property responsible. Its captured implementation assumes horizontal left-to-right text and uses a small vertical tolerance, so complex multilingual or vertical layouts need more work.

The second layer inspects computed content for ::before and ::after on the target and descendants, parses simple CSS strings, estimates generated fragments from host-element bounds, and merges them with DOM fragments by position. In the combined captured test, raw text was .560978, visible DOM reconstruction was 79.50, the generated fragment supplied $, and the final rendered result was $79.50. The implementation remains simplified: it approximates pseudo-element position and does not decode remapped custom fonts or fully handle clipping, transforms, and complex layouts.

Protected implementation modules code0023, code0024, code0025, code0026, code0027, and code0028 accompany this page and should be used exactly as supplied.

Handle JavaScript wrappers and changing DOM content separately

JavaScript can create one wrapper per character, add random nesting or class names, shuffle wrappers while CSS restores their order, or rebuild the price repeatedly. Random wrappers alone may not break text extraction, but shuffled wrappers do because DOM order no longer matches visual order. Asynchronous construction creates a separate timing problem: an early read may return a partial value even though the extraction method is otherwise correct.

The geometry-based extractor is insensitive to wrapper names and nesting because it walks text nodes and uses screen coordinates. Timing still needs a separate control. The captured waitForStableContent approach combines a MutationObserver quiet period with a validation callback. A quiet DOM alone is insufficient because a placeholder can remain stable; the extracted value must also pass a format check. When raw text is obfuscated, the read callback should return reconstructed visible text and validation should test that result.

In the captured dynamic examples, extraction returned $79.50 after markup regeneration. The remaining limitation is important: a quiet period and a valid format do not prove that a site will not update the value later, and CSS-only visual changes may not trigger the same mutation signal.

Protected implementation modules code0036, code0037, and code0038 accompany this page and should be used exactly as supplied.

Use OCR and AI only after browser evidence is exhausted

OCR reads rendered pixels and can help when the DOM is unusable, but it adds processing cost and can misread small text, unusual fonts, or poor screenshots. AI extraction has a related evidence problem: without layout or visual information, it may copy hidden placeholder text or include decoy digits. The captured AI test returned $9999 for generated content and values such as $7498.560 for hidden characters, even after an explicit instruction to ignore invisible text.

Treat OCR and AI as fallbacks or interpretation layers. First recover browser evidence where possible, validate the result against the expected format, and only then use visual recognition or AI processing. Also check metadata or structured data before building a more complex extractor, while remembering that alternate values may be absent, stale, or dynamically generated.

Frequently asked questions

What is CSS text obfuscation?

It is a method that leaves the on-screen wording unchanged for people while making the underlying markup harder to extract. Typical mechanisms include visual reordering, invisible decoy characters, and text supplied only through CSS.

Why does my web scraper extract incorrect text?

Common causes include reading before JavaScript has finished, including characters that CSS hides, and following DOM order when CSS has changed the visual order. Generated pseudo-element text is another source of mismatch.

What is dynamic character wrapping?

JavaScript divides a string into separate characters and places each one in its own element. On its own this may be a legitimate animation or typography technique; it becomes an extraction problem when wrappers are randomized, characters are shuffled, or the DOM keeps changing.

Can Playwright bypass CSS obfuscation?

Playwright can expose rendered layout, computed styles, and generated content, which helps reconstruct many common cases. It does not provide a universal guarantee, especially for remapped custom fonts or complex layouts.

Can AI extract obfuscated text?

An AI tool given only HTML may not know which characters are visible or how they are arranged. Pairing it with a screenshot, OCR, or browser-based reconstruction gives it the missing visual evidence, though accuracy still needs validation.

Custom-font glyph mapping
Character in HTMLDisplayed glyph
1$
27
39
4.
55
60

Captured custom-font example: HTML 123456 displays as $79.50 via the ObfuscatedPrices @font-face in code0011.