Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix(dom): bound JS listener CDP fanout
MagMueller9bf7feffb694
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex 2e8dd33be..609063d08 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -31,6 +31,10 @@ if TYPE_CHECKING:# Note: iframe limits are now configurable via BrowserProfile.max_iframes and BrowserProfile.max_iframe_depth+_MAX_JS_CLICK_LISTENER_ELEMENTS = 100+_DESCRIBE_NODE_BATCH_SIZE = 20+_JS_CLICK_LISTENER_OVERFLOW = '__browser_use_too_many_click_listeners__'+class DomService:"""@@ -443,10 +447,10 @@ class DomService:self.logger.debug(f'Failed to get iframe scroll positions: {e}')iframe_scroll_ms = (time.time() - start_iframe_scroll) * 1000- # Detect elements with JavaScript click event listeners (without mutating DOM)- # On heavy pages (>10k elements) the querySelectorAll('*') + getEventListeners()- # loop plus per-element DOM.describeNode CDP calls can take 10s+.- # The JS expression below bails out early if the page is too heavy.+ # Detect elements with JavaScript click event listeners (without mutating DOM).+ # Bounding only the total DOM size is insufficient: framework-heavy pages can attach+ # hundreds of listeners to fewer than 10k elements. Resolving every listener with an+ # unbounded gather floods remote CDP connections and can make the whole session appear stale.# Elements are still detected via the accessibility tree and ClickableElementDetector.start_js_listener_detection = time.time()js_click_listener_backend_ids: set[int] = set()@@ -476,6 +480,9 @@ class DomService:// Check for click-related event listenersif (listeners.click || listeners.mousedown || listeners.mouseup || listeners.pointerdown || listeners.pointerup) {elementsWithListeners.push(el);+ if (elementsWithListeners.length > %d) {+ return %r;+ }}} catch (e) {// Ignore errors for individual elements (e.g., cross-origin)@@ -484,13 +491,19 @@ class DomService:return elementsWithListeners;})()- """,+ """+ % (_MAX_JS_CLICK_LISTENER_ELEMENTS, _JS_CLICK_LISTENER_OVERFLOW),'includeCommandLineAPI': True, # enables getEventListeners()'returnByValue': False, # Return object references, not values},session_id=cdp_session.session_id,)+ if js_listener_result.get('result', {}).get('value') == _JS_CLICK_LISTENER_OVERFLOW:+ self.logger.debug(+ f'Skipping JS listener resolution: more than {_MAX_JS_CLICK_LISTENER_ELEMENTS} elements have click listeners'+ )+result_object_id = js_listener_result.get('result', {}).get('objectId')if result_object_id:# Step 2: Get array properties to access each element@@ -514,7 +527,6 @@ class DomService:if object_id and isinstance(object_id, str):element_object_ids.append(object_id)- # Batch resolve backend node IDs (run in parallel)async def get_backend_node_id(object_id: str) -> int | None:try:node_info = await cdp_session.cdp_client.send.DOM.describeNode(@@ -525,8 +537,13 @@ class DomService:except Exception:return None- # Resolve all element object IDs to backend node IDs in parallel- backend_ids = await asyncio.gather(*[get_backend_node_id(oid) for oid in element_object_ids])+ # Keep concurrency bounded. Each describeNode call can trigger target/session+ # bookkeeping, so even a few dozen simultaneous calls can starve screenshots+ # and the other CDP requests needed to build browser state.+ backend_ids: list[int | None] = []+ for batch_start in range(0, len(element_object_ids), _DESCRIBE_NODE_BATCH_SIZE):+ batch = element_object_ids[batch_start : batch_start + _DESCRIBE_NODE_BATCH_SIZE]+ backend_ids.extend(await asyncio.gather(*[get_backend_node_id(object_id) for object_id in batch]))js_click_listener_backend_ids = {bid for bid in backend_ids if bid is not None}# Release the array object to avoid memory leaksdiff --git a/tests/ci/browser/test_dom_listener_detection.py b/tests/ci/browser/test_dom_listener_detection.pynew file mode 100644index 000000000..dbd4c50ce--- /dev/null+++ b/tests/ci/browser/test_dom_listener_detection.py@@ -0,0 +1,70 @@+"""Regression tests for bounded JavaScript click-listener detection."""++from collections import Counter++import pytest++from browser_use.browser.profile import BrowserProfile+from browser_use.browser.session import BrowserSession+from browser_use.dom.service import _MAX_JS_CLICK_LISTENER_ELEMENTS+++async def browser_session():+ session = BrowserSession(browser_profile=BrowserProfile(headless=True, user_data_dir=None, keep_alive=True))+ await session.start()+ yield session+ await session.kill()+++async def test_listener_detection_preserves_small_pages_and_skips_cdp_fanout(httpserver, browser_session: BrowserSession):+ """Direct listeners remain indexed normally, but listener-heavy pages do not flood CDP."""+ small_listener_count = 3+ overflow_listener_count = _MAX_JS_CLICK_LISTENER_ELEMENTS + 1++ def listener_page(element_count: int) -> str:+ elements = ''.join(f'<div id="custom-{index}">item {index}</div>' for index in range(element_count))+ return f"""+ <html>+ <body>+ {elements}+ <script>+ for (const element of document.querySelectorAll('[id^="custom-"]')) {{+ element.addEventListener('click', () => undefined);+ }}+ </script>+ </body>+ </html>+ """++ httpserver.expect_request('/few-listeners').respond_with_data(listener_page(small_listener_count), content_type='text/html')+ httpserver.expect_request('/many-listeners').respond_with_data(+ listener_page(overflow_listener_count), content_type='text/html'+ )++ await browser_session.navigate_to(httpserver.url_for('/few-listeners'))+ cdp_session = await browser_session.get_or_create_cdp_session()+ cdp_calls: Counter[str] = Counter()+ original_send_raw = cdp_session.cdp_client.send_raw++ async def counted_send_raw(method, params=None, session_id=None):+ cdp_calls[method] += 1+ return await original_send_raw(method=method, params=params, session_id=session_id)++ cdp_session.cdp_client.send_raw = counted_send_raw++ small_state = await browser_session.get_browser_state_summary(include_screenshot=False)+ small_ids = {+ node.attributes.get('id')+ for node in small_state.dom_state.selector_map.values()+ if node.attributes.get('id', '').startswith('custom-')+ }+ assert small_ids == {f'custom-{index}' for index in range(small_listener_count)}+ assert cdp_calls['DOM.describeNode'] == small_listener_count++ await browser_session.navigate_to(httpserver.url_for('/many-listeners'))+ cdp_calls.clear()+ overflow_state = await browser_session.get_browser_state_summary(include_screenshot=False)++ assert overflow_state.dom_state is not None+ assert cdp_calls['DOM.describeNode'] == 0