crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

fix: scan_full_page captures all content on virtual-scroll pages (#731)

Open hafezparast opened this issue 8 months ago • 1 comments

Summary

  • _handle_full_page_scan: installs a MutationObserver before scrolling to capture DOM elements that are removed during scroll (virtual scroll recycling). After scrolling, deduplicates against still-visible elements and re-injects accumulated content into a hidden <div> so page.content() includes everything.
  • _handle_virtual_scroll: falls back to window.scrollBy() when container.scrollTop has no effect (window-level scrolling, e.g. Twitter/X). Updates end-of-scroll detection accordingly.
  • Non-virtual-scroll pages are unaffected: if no elements are removed during scrolling, no injection occurs.

Before fix: scan_full_page=True on a 50-item virtual-scroll page → only 10 items (last batch)
After fix: all 50 items captured

Fixes #731, related to #1087

Test plan

  • [x] Reproduction test (tests/test_repro_731.py) — local virtual-scroll page with 50 items, verifies all 50 captured
  • [x] 13 adversarial tests covering: virtual scroll (50/100 items), static pages (no false positives), empty pages, append-based infinite scroll, nested virtual scroll with header/footer preservation, low max_steps, observer cleanup, element ordering, hidden container verification, deduplication
  • [x] Full regression suite: 303 passed, 0 regressions (1 pre-existing HuggingFace failure unrelated)

🤖 Generated with Claude Code

hafezparast avatar Mar 23 '26 06:03 hafezparast

Reworked based on your review. Force-pushed with the scroll-and-snapshot approach:

  • Replaced MutationObserver with a Map<fingerprint, outerHTML> loop
  • Fingerprinting uses id/data-* attributes first, content hash fallback
  • Accumulator capped at 10K elements
  • Kept the window.scrollBy fallback
  • Real pytest tests with @pytest.mark.asyncio — 9 adversarial cases (1000 items, similar text, no data-id, append-only, memory stress)

Sorry about the detour through #1877 — consolidated everything back here.

hafezparast avatar Mar 31 '26 13:03 hafezparast