Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix(dom): preserve structural state when AX tree fails

MagMuellerce0fc1b99b47
diff --git a/browser_use/dom/service.py b/browser_use/dom/service.py
index 609063d08..9e2f70f83 100644
--- a/browser_use/dom/service.py
+++ b/browser_use/dom/service.py
@@ -620,7 +620,10 @@ class DomService:
for task in pending2:
task.cancel()
- # Extract results, tracking which ones failed
+ # Extract results, tracking which required requests failed. The AX tree
+ # enriches DOM nodes with accessibility names and roles, but the snapshot
+ # and DOM tree still contain a usable page structure without it. Do not
+ # discard that structure when accessibility collection stalls or fails.
results = {}
failed = []
for key, task in tasks.items():
@@ -629,10 +632,16 @@ class DomService:
results[key] = task.result()
except Exception as e:
self.logger.warning(f'CDP request {key} failed with exception: {e}')
- failed.append(key)
+ if key == 'ax_tree':
+ results[key] = {'nodes': []}
+ else:
+ failed.append(key)
else:
self.logger.warning(f'CDP request {key} timed out')
- failed.append(key)
+ if key == 'ax_tree':
+ results[key] = {'nodes': []}
+ else:
+ failed.append(key)
# If any required tasks failed, raise an exception
if failed:
diff --git a/tests/ci/browser/test_dom_listener_detection.py b/tests/ci/browser/test_dom_listener_detection.py
index dbd4c50ce..ab9446271 100644
--- a/tests/ci/browser/test_dom_listener_detection.py
+++ b/tests/ci/browser/test_dom_listener_detection.py
@@ -1,12 +1,13 @@
"""Regression tests for bounded JavaScript click-listener detection."""
+import asyncio
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
+from browser_use.dom.service import DomService, _MAX_JS_CLICK_LISTENER_ELEMENTS
@pytest.fixture
@@ -68,3 +69,22 @@ async def test_listener_detection_preserves_small_pages_and_skips_cdp_fanout(htt
assert overflow_state.dom_state is not None
assert cdp_calls['DOM.describeNode'] == 0
+
+
+async def test_ax_tree_failure_preserves_structural_dom(httpserver, browser_session: BrowserSession, monkeypatch):
+ """An unavailable accessibility tree must not erase the usable structural DOM."""
+ httpserver.expect_request('/ax-unavailable').respond_with_data(
+ '<html><body><button id="continue">Continue</button></body></html>',
+ content_type='text/html',
+ )
+
+ async def fail_ax_tree(_service: DomService, _target_id):
+ raise asyncio.CancelledError
+
+ monkeypatch.setattr(DomService, '_get_ax_tree_for_all_frames', fail_ax_tree)
+ await browser_session.navigate_to(httpserver.url_for('/ax-unavailable'))
+
+ state = await browser_session.get_browser_state_summary(include_screenshot=False)
+
+ assert state.dom_state is not None
+ assert any(node.attributes.get('id') == 'continue' for node in state.dom_state.selector_map.values())