Skip to content

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

Refactor DefaultActionWatchdog to improve tab handling and metadata return

Magnus Müller138cc076fe03
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index 332857e1e..33bbb1756 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -77,7 +77,7 @@ class DefaultActionWatchdog(BaseWatchdog):
# Wait a bit for potential new tab to be created
# This is necessary because tab creation is async and might not be immediate
- await asyncio.sleep(0.5)
+ await asyncio.sleep(0.25)
# Note: We don't clear cached state here - let multi_act handle DOM change detection
# by explicitly rebuilding and comparing when needed
@@ -89,6 +89,8 @@ class DefaultActionWatchdog(BaseWatchdog):
# Check if a new tab was opened
after_target_ids = await self.browser_session._cdp_get_all_pages()
new_target_ids = {t['targetId'] for t in after_target_ids} - {t['targetId'] for t in initial_target_ids}
+ new_tab_opened = len(new_target_ids) > 0
+
if new_target_ids:
new_tab_msg = 'New tab opened - switching to it'
msg += f' - {new_tab_msg}'
@@ -106,8 +108,11 @@ class DefaultActionWatchdog(BaseWatchdog):
switch_event = await self.event_bus.dispatch(SwitchTabEvent(target_id=new_target_id))
await switch_event
- # Return click metadata (coordinates) if available
- return click_metadata
+ # Return click metadata including new tab information
+ result_metadata = click_metadata if isinstance(click_metadata, dict) else {}
+ result_metadata['new_tab_opened'] = new_tab_opened
+
+ return result_metadata
except Exception as e:
raise
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index a0ec8661c..d0f1c80d6 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -269,14 +269,20 @@ class Tools(Generic[Context]):
raise ValueError(f'Element index {params.index} not found in DOM')
event = browser_session.event_bus.dispatch(
- ClickElementEvent(node=node, while_holding_ctrl=params.while_holding_ctrl)
+ ClickElementEvent(node=node, while_holding_ctrl=params.while_holding_ctrl or False)
)
await event
# Wait for handler to complete and get any exception or metadata
click_metadata = await event.event_result(raise_if_any=True, raise_if_none=False)
memory = f'Clicked element with index {params.index}'
+
if params.while_holding_ctrl:
memory += ' and opened in new tab'
+
+ # Check if a new tab was opened (from watchdog metadata)
+ elif isinstance(click_metadata, dict) and click_metadata.get('new_tab_opened'):
+ memory += ' - which opened a new tab'
+
msg = f'🖱️ {memory}'
logger.info(msg)
diff --git a/browser_use/tools/views.py b/browser_use/tools/views.py
index c25c47c2a..0a1c00827 100644
--- a/browser_use/tools/views.py
+++ b/browser_use/tools/views.py
@@ -15,8 +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 | None = Field(
+ default=None,
+ description='Set to True to open the navigation in a new background tab (Ctrl+Click behavior). Optional.',
)
# expect_download: bool = Field(default=False, description='set True if expecting a download, False otherwise') # moved to downloads_watchdog.py
# click_count: int = 1 # TODO