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(browser): preserve DOM when state screenshots stall
MagMueller7f22c5a7b456
diff --git a/browser_use/browser/watchdogs/dom_watchdog.py b/browser_use/browser/watchdogs/dom_watchdog.pyindex ac22477b3..ebc5c79ce 100644--- a/browser_use/browser/watchdogs/dom_watchdog.py+++ b/browser_use/browser/watchdogs/dom_watchdog.py@@ -22,6 +22,8 @@ from browser_use.utils import create_task_with_error_handling, time_execution_asif TYPE_CHECKING:from browser_use.browser.views import BrowserStateSummary, NetworkRequest, PageInfo, PaginationButton+_BROWSER_STATE_PARALLEL_TASK_BUDGET_SECONDS = 20.0+class DOMWatchdog(BaseWatchdog):"""Handles DOM tree building, serialization, and element access via CDP.@@ -356,6 +358,7 @@ class DOMWatchdog(BaseWatchdog):# Execute DOM building and screenshot capture in paralleldom_task = Nonescreenshot_task = None+ parallel_tasks_started_at = time.monotonic()# Start DOM building task if requestedif event.include_dom:@@ -400,7 +403,15 @@ class DOMWatchdog(BaseWatchdog):if screenshot_task:try:- screenshot_b64 = await screenshot_task+ # BrowserStateRequestEvent has a 30-second budget. A nested+ # ScreenshotEvent can otherwise consume nearly all of it,+ # preventing this handler from returning the usable DOM-only+ # state when screenshot capture stalls.+ remaining_screenshot_budget = max(+ 0.001,+ _BROWSER_STATE_PARALLEL_TASK_BUDGET_SECONDS - (time.monotonic() - parallel_tasks_started_at),+ )+ screenshot_b64 = await asyncio.wait_for(screenshot_task, timeout=remaining_screenshot_budget)self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: ✅ Clean screenshot captured')except Exception as e:self.logger.warning(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Clean screenshot failed: {e}')diff --git a/tests/ci/browser/test_dom_listener_detection.py b/tests/ci/browser/test_dom_listener_detection.pyindex 8a4c4f1ed..92d7d8f5b 100644--- a/tests/ci/browser/test_dom_listener_detection.py+++ b/tests/ci/browser/test_dom_listener_detection.py@@ -7,6 +7,8 @@ import pytestfrom browser_use.browser.profile import BrowserProfilefrom browser_use.browser.session import BrowserSession+from browser_use.browser.watchdogs import dom_watchdog+from browser_use.browser.watchdogs.dom_watchdog import DOMWatchdogfrom browser_use.dom.service import _MAX_JS_CLICK_LISTENER_ELEMENTS, DomService@@ -88,3 +90,24 @@ async def test_ax_tree_failure_preserves_structural_dom(httpserver, browser_sessassert state.dom_state is not Noneassert any(node.attributes.get('id') == 'continue' for node in state.dom_state.selector_map.values())+++async def test_screenshot_timeout_preserves_structural_dom(httpserver, browser_session: BrowserSession, monkeypatch):+ """A stalled state screenshot must not consume the outer event's recovery budget."""+ httpserver.expect_request('/screenshot-unavailable').respond_with_data(+ '<html><body><button id="continue">Continue</button></body></html>',+ content_type='text/html',+ )++ async def hang_screenshot(_watchdog: DOMWatchdog):+ await asyncio.Future()++ monkeypatch.setattr(DOMWatchdog, '_capture_clean_screenshot', hang_screenshot)+ monkeypatch.setattr(dom_watchdog, '_BROWSER_STATE_PARALLEL_TASK_BUDGET_SECONDS', 0.1)+ await browser_session.navigate_to(httpserver.url_for('/screenshot-unavailable'))++ state = await browser_session.get_browser_state_summary(include_screenshot=True)++ assert state.screenshot is None+ assert state.dom_state is not None+ assert any(node.attributes.get('id') == 'continue' for node in state.dom_state.selector_map.values())