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 Failed to build DOM tree: cannot reuse already awaited coroutine
Nick Sweeting76512fbfdd43
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex dedc5bfaf..ffe426ac6 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -183,7 +183,9 @@ class MessageManager:read_state_idx = 0for idx, action_result in enumerate(result):if action_result.include_extracted_content_only_once and action_result.extracted_content:- self.state.read_state_description += f'<read_state_{read_state_idx}>\n{action_result.extracted_content}\n</read_state_{read_state_idx}>\n'+ self.state.read_state_description += (+ f'<read_state_{read_state_idx}>\n{action_result.extracted_content}\n</read_state_{read_state_idx}>\n'+ )read_state_idx += 1logger.debug(f'Added extracted_content to read_state_description: {action_result.extracted_content}')diff --git a/browser_use/browser/events.py b/browser_use/browser/events.pyindex 702721efc..4dccdb24d 100644--- a/browser_use/browser/events.py+++ b/browser_use/browser/events.py@@ -308,7 +308,7 @@ class BrowserKillEvent(BaseEvent):# event_timeout: float | None = 15.0 # seconds-# Moved to strorage state+# Moved to storage state# class SetCookiesEvent(BaseEvent):# """Set browser cookies."""diff --git a/browser_use/browser/popups_watchdog.py b/browser_use/browser/popups_watchdog.pyindex c55abb3d8..fd89c5a77 100644--- a/browser_use/browser/popups_watchdog.py+++ b/browser_use/browser/popups_watchdog.py@@ -36,7 +36,9 @@ class PopupsWatchdog(BaseWatchdog):self.logger.info(f'📌 Starting dialog handler setup for target {target_id}')try:- cdp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False) # don't auto-focus new tabs! sometimes we need to open tabs in background+ cdp_session = await self.browser_session.get_or_create_cdp_session(+ target_id, focus=False+ ) # don't auto-focus new tabs! sometimes we need to open tabs in background# Set up async handler for JavaScript dialogs - now we can handle them immediately!async def handle_dialog(event_data, session_id: str | None = None):diff --git a/browser_use/browser/views.py b/browser_use/browser/views.pyindex ceb09630a..3469b3343 100644--- a/browser_use/browser/views.py+++ b/browser_use/browser/views.py@@ -28,16 +28,19 @@ class TabInfo(BaseModel):url: strtitle: strtarget_id: TargetID = Field(serialization_alias='tab_id', validation_alias=AliasChoices('tab_id', 'target_id'))- parent_target_id: TargetID | None = Field(default=None, serialization_alias='parent_tab_id', validation_alias=AliasChoices('parent_tab_id', 'parent_target_id')) # parent page that contains this popup or cross-origin iframe+ parent_target_id: TargetID | None = Field(+ default=None, serialization_alias='parent_tab_id', validation_alias=AliasChoices('parent_tab_id', 'parent_target_id')+ ) # parent page that contains this popup or cross-origin iframe@field_serializer('target_id')def serialize_target_id(self, target_id: TargetID, _info: Any) -> str:return target_id[-4:]-+@field_serializer('parent_target_id')def serialize_parent_target_id(self, parent_target_id: TargetID | None, _info: Any) -> str | None:return parent_target_id[-4:] if parent_target_id else None+class PageInfo(BaseModel):"""Comprehensive page size and scroll information"""diff --git a/browser_use/controller/views.py b/browser_use/controller/views.pyindex cca08361f..a4d7a678b 100644--- a/browser_use/controller/views.py+++ b/browser_use/controller/views.py@@ -15,7 +15,9 @@ class GoToUrlAction(BaseModel):class ClickElementAction(BaseModel):index: int = Field(ge=1, description='index of the element to click')- while_holding_ctrl: bool = Field(default=False, description='set True to open any resulting navigation in a new background tab, False otherwise')+ while_holding_ctrl: bool = Field(+ default=False, description='set True to open any resulting navigation in a new background tab, False otherwise'+ )# expect_download: bool = Field(default=False, description='set True if expecting a download, False otherwise') # moved to downloads_watchdog.py# click_count: int = 1 # TODOdiff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex c07b8a492..30e3e2cab 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -345,37 +345,84 @@ class DomService:except Exception as e:self.logger.debug(f'Failed to get iframe scroll positions: {e}')- snapshot_request = cdp_session.cdp_client.send.DOMSnapshot.captureSnapshot(- params={- 'computedStyles': REQUIRED_COMPUTED_STYLES,- 'includePaintOrder': True,- 'includeDOMRects': True,- 'includeBlendedBackgroundColors': False,- 'includeTextColorOpacities': False,- },- session_id=cdp_session.session_id,- )+ # Define CDP request factories to avoid duplication+ def create_snapshot_request():+ return cdp_session.cdp_client.send.DOMSnapshot.captureSnapshot(+ params={+ 'computedStyles': REQUIRED_COMPUTED_STYLES,+ 'includePaintOrder': True,+ 'includeDOMRects': True,+ 'includeBlendedBackgroundColors': False,+ 'includeTextColorOpacities': False,+ },+ session_id=cdp_session.session_id,+ )- dom_tree_request = cdp_session.cdp_client.send.DOM.getDocument(- params={'depth': -1, 'pierce': True}, session_id=cdp_session.session_id- )+ def create_dom_tree_request():+ return cdp_session.cdp_client.send.DOM.getDocument(+ params={'depth': -1, 'pierce': True}, session_id=cdp_session.session_id+ )++ start = time.time()++ # Create initial tasks+ tasks = {+ 'snapshot': asyncio.create_task(create_snapshot_request()),+ 'dom_tree': asyncio.create_task(create_dom_tree_request()),+ 'ax_tree': asyncio.create_task(self._get_ax_tree_for_all_frames(target_id)),+ 'device_pixel_ratio': asyncio.create_task(self._get_viewport_ratio(target_id)),+ }- ax_tree_request = self._get_ax_tree_for_all_frames(target_id)+ # Wait for all tasks with timeout+ done, pending = await asyncio.wait(tasks.values(), timeout=10.0)++ # Retry any failed or timed out tasks+ if pending:+ for task in pending:+ task.cancel()++ # Retry mapping for pending tasks+ retry_map = {+ tasks['snapshot']: lambda: asyncio.create_task(create_snapshot_request()),+ tasks['dom_tree']: lambda: asyncio.create_task(create_dom_tree_request()),+ tasks['ax_tree']: lambda: asyncio.create_task(self._get_ax_tree_for_all_frames(target_id)),+ tasks['device_pixel_ratio']: lambda: asyncio.create_task(self._get_viewport_ratio(target_id)),+ }++ # Create new tasks only for the ones that didn't complete+ for key, task in tasks.items():+ if task in pending and task in retry_map:+ tasks[key] = retry_map[task]()++ # Wait again with shorter timeout+ done2, pending2 = await asyncio.wait([t for t in tasks.values() if not t.done()], timeout=2.0)++ if pending2:+ for task in pending2:+ task.cancel()++ # Extract results, tracking which ones failed+ results = {}+ failed = []+ for key, task in tasks.items():+ if task.done() and not task.cancelled():+ try:+ results[key] = task.result()+ except Exception as e:+ self.logger.warning(f'CDP request {key} failed with exception: {e}')+ failed.append(key)+ else:+ self.logger.warning(f'CDP request {key} timed out')+ failed.append(key)- device_pixel_ratio_request = self._get_viewport_ratio(target_id)+ # If any required tasks failed, raise an exception+ if failed:+ raise TimeoutError(f'CDP requests failed or timed out: {", ".join(failed)}')- start = time.time()- # Gather all CDP requests with timeout- try:- snapshot, dom_tree, ax_tree, device_pixel_ratio = await asyncio.wait_for(- asyncio.gather(snapshot_request, dom_tree_request, ax_tree_request, device_pixel_ratio_request), timeout=10.0- )- except TimeoutError:- # Try to get them individually to see which one hangs- snapshot = await asyncio.wait_for(snapshot_request, timeout=2.0)- dom_tree = await asyncio.wait_for(dom_tree_request, timeout=2.0)- ax_tree = await asyncio.wait_for(ax_tree_request, timeout=2.0)- device_pixel_ratio = await asyncio.wait_for(device_pixel_ratio_request, timeout=2.0)+ snapshot = results['snapshot']+ dom_tree = results['dom_tree']+ ax_tree = results['ax_tree']+ device_pixel_ratio = results['device_pixel_ratio']end = time.time()cdp_timing = {'cdp_calls_total': end - start}diff --git a/browser_use/logging_config.py b/browser_use/logging_config.pyindex 8e2620cc2..e4c41225f 100644--- a/browser_use/logging_config.py+++ b/browser_use/logging_config.py@@ -129,7 +129,7 @@ def setup_logging(stream=None, log_level=None, force_setup=False):# Configure CDP logging using separate CDP_LOGGING_LEVELcdp_log_type = CONFIG.CDP_LOGGING_LEVEL.lower()-+# Determine the CDP log level to useif cdp_log_type == 'result':cdp_log_level = 35 # RESULT level valuediff --git a/tests/ci/test_browser_session_tab_management.py b/tests/ci/test_browser_session_tab_management.pyindex 108b90754..984acaae6 100644--- a/tests/ci/test_browser_session_tab_management.py+++ b/tests/ci/test_browser_session_tab_management.py@@ -210,6 +210,7 @@ class TestTabManagement:# Switch agent back to the first tab using correct indexfrom browser_use.browser.events import SwitchTabEvent+await browser_session.event_bus.dispatch(SwitchTabEvent(target_id=first_tab_id))await asyncio.sleep(0.5)