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
Remove activateTarget
Magnus Müller6568a4061f5f
diff --git a/browser_use/actor/mouse.py b/browser_use/actor/mouse.pyindex e22a19aa5..c4a05801d 100644--- a/browser_use/actor/mouse.py+++ b/browser_use/actor/mouse.py@@ -87,13 +87,6 @@ class Mouse:if not self._session_id:raise RuntimeError('Session ID is required for scroll operations')- # Activate the target first (critical for CDP calls to work)- if self._target_id:- try:- await self._client.send.Target.activateTarget(params={'targetId': self._target_id})- except Exception:- pass-# Method 1: Try mouse wheel event (most reliable)try:# Get viewport dimensionsdiff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 3a0d039fc..a3558143f 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -1062,7 +1062,6 @@ class BrowserSession(BaseModel):)self.agent_focus = sessionif focus:- await session.cdp_client.send.Target.activateTarget(params={'targetId': session.target_id})await session.cdp_client.send.Runtime.runIfWaitingForDebugger(session_id=session.session_id)# else:# self.logger.debug(f'[get_or_create_cdp_session] Reusing existing session for {target_id} (focus={focus})')@@ -1095,7 +1094,6 @@ class BrowserSession(BaseModel):f'[get_or_create_cdp_session] Switching agent focus from {self.agent_focus.target_id} to {target_id}')self.agent_focus = session- await session.cdp_client.send.Target.activateTarget(params={'targetId': session.target_id})await session.cdp_client.send.Runtime.runIfWaitingForDebugger(session_id=session.session_id)else:self.logger.debug(diff --git a/browser_use/browser/watchdog_base.py b/browser_use/browser/watchdog_base.pyindex 1094cd1ef..582f73090 100644--- a/browser_use/browser/watchdog_base.py+++ b/browser_use/browser/watchdog_base.py@@ -118,7 +118,7 @@ class BaseWatchdog(BaseModel):'👈 by Agent', '👉 returned to Agent')browser_session.logger.debug(- f'🚌 {watchdog_and_handler_str} ✅ Succeeded ({time_elapsed:.2f}s){result_summary}{parents_summary}'+ f'🚌 {watchdog_and_handler_str} Succeeded ({time_elapsed:.2f}s){result_summary}{parents_summary}')return resultexcept Exception as e:@@ -131,18 +131,16 @@ class BaseWatchdog(BaseModel):# attempt to repair potentially crashed CDP sessiontry:+ target_id_to_restore = Noneif browser_session.agent_focus and browser_session.agent_focus.target_id:- # Common issue with CDP, some calls need the target to be active/foreground to succeed:- # screenshot, scroll, Page.handleJavaScriptDialog, and some others+ # Common issue with CDP, recreate session with new socket to recover+ target_id_to_restore = browser_session.agent_focus.target_idbrowser_session.logger.debug(- f'🚌 {watchdog_and_handler_str} ⚠️ Re-foregrounding target to try and recover crashed CDP session\n\t{browser_session.agent_focus}'+ f'🚌 {watchdog_and_handler_str} ⚠️ Recreating session to try and recover crashed CDP session\n\t{browser_session.agent_focus}')del browser_session._cdp_session_pool[browser_session.agent_focus.target_id]browser_session.agent_focus = await browser_session.get_or_create_cdp_session(- target_id=browser_session.agent_focus.target_id, new_socket=True- )- await browser_session.agent_focus.cdp_client.send.Target.activateTarget(- params={'targetId': browser_session.agent_focus.target_id}+ target_id=target_id_to_restore, new_socket=True)else:await browser_session.get_or_create_cdp_session(target_id=None, new_socket=True, focus=True)diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.pyindex 8b021c411..428c8c523 100644--- a/browser_use/browser/watchdogs/default_action_watchdog.py+++ b/browser_use/browser/watchdogs/default_action_watchdog.py@@ -192,11 +192,6 @@ class DefaultActionWatchdog(BaseWatchdog):# Positive pixels = scroll down, negative = scroll uppixels = event.amount if event.direction == 'down' else -event.amount- # CRITICAL: CDP calls time out without this, even if the target is already active- await self.browser_session.agent_focus.cdp_client.send.Target.activateTarget(- params={'targetId': self.browser_session.agent_focus.target_id}- )-# Element-specific scrolling if node is providedif event.node is not None:element_node = event.node@@ -212,7 +207,7 @@ class DefaultActionWatchdog(BaseWatchdog):f'📜 Scrolled element {index_for_logging} container {event.direction} by {event.amount} pixels')- # CRITICAL: For iframe scrolling, we need to force a full DOM refresh+ # For iframe scrolling, we need to force a full DOM refresh# because the iframe's content has changed positionif is_iframe:self.logger.debug('🔄 Forcing DOM refresh after iframe scroll')@@ -227,11 +222,6 @@ class DefaultActionWatchdog(BaseWatchdog):# Perform target-level scrollawait self._scroll_with_cdp_gesture(pixels)- # CRITICAL: CDP calls time out without this, even if the target is already active- await self.browser_session.agent_focus.cdp_client.send.Target.activateTarget(- params={'targetId': self.browser_session.agent_focus.target_id}- )-# Note: We don't clear cached state here - let multi_act handle DOM change detection# by explicitly rebuilding and comparing when needed@@ -614,7 +604,6 @@ class DefaultActionWatchdog(BaseWatchdog):finally:# always re-focus back to original top-level page session context in case click opened a new tab/popup/window/dialog/etc.cdp_session = await self.browser_session.get_or_create_cdp_session(focus=True)- await cdp_session.cdp_client.send.Target.activateTarget(params={'targetId': cdp_session.target_id})await cdp_session.cdp_client.send.Runtime.runIfWaitingForDebugger(session_id=cdp_session.session_id)except URLNotAllowedError as e:@@ -640,7 +629,6 @@ class DefaultActionWatchdog(BaseWatchdog):try:# Get CDP client and sessioncdp_session = await self.browser_session.get_or_create_cdp_session(target_id=None, focus=True)- await cdp_session.cdp_client.send.Target.activateTarget(params={'targetId': cdp_session.target_id})# Type the text character by character to the focused elementfor char in text: