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
always re-focus and resume runtime if paused
Nick Sweetingef64704cd72f
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.pyindex 23d090fb4..c7fd0082c 100644--- a/browser_use/browser/default_action_watchdog.py+++ b/browser_use/browser/default_action_watchdog.py@@ -247,7 +247,8 @@ class DefaultActionWatchdog(BaseWatchdog):)# Get CDP client- cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=element_node.target_id, focus=True)+ await self.browser_session.get_or_create_cdp_session(focus=True)+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=element_node.target_id, focus=False)# Get the correct session ID for the element's framesession_id = cdp_session.session_id@@ -495,7 +496,7 @@ class DefaultActionWatchdog(BaseWatchdog):},session_id=session_id,),- timeout=1.0, # 1 second timeout for mouseReleased+ timeout=3.0, # 1 second timeout for mouseReleased)except TimeoutError:self.logger.debug('⏱️ Mouse up timed out (possibly due to lag or dialog popup), continuing...')@@ -528,6 +529,9 @@ class DefaultActionWatchdog(BaseWatchdog):except Exception as js_e:self.logger.error(f'CDP JavaScript click also failed: {js_e}')raise Exception(f'Failed to click element: {e}')+ finally:+ # always re-focus back to original top-level page session context in case click opened a new tab/popup/window/dialog/etc.+ await self.browser_session.get_or_create_cdp_session(focus=True)except URLNotAllowedError as e:raise ediff --git a/browser_use/browser/dom_watchdog.py b/browser_use/browser/dom_watchdog.pyindex 330043c44..4038add4d 100644--- a/browser_use/browser/dom_watchdog.py+++ b/browser_use/browser/dom_watchdog.py@@ -256,6 +256,10 @@ class DOMWatchdog(BaseWatchdog):# Skip DOM building if not requestedcontent = SerializedDOMState(_root=None, selector_map={})+ # re-focus top-level page session context+ assert self.browser_session.agent_focus is not None, 'No current target ID'+ await self.browser_session.get_or_create_cdp_session(target_id=self.browser_session.agent_focus.target_id, focus=True)+# Get screenshot if requestedscreenshot_b64 = Noneif event.include_screenshot:diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.pyindex 6922bccfc..cdd878117 100644--- a/browser_use/browser/profile.py+++ b/browser_use/browser/profile.py@@ -606,7 +606,7 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Brodescription='Window position to use for the browser x,y from the top left when headless=False.',)cross_origin_iframes: bool = Field(- default=True,+ default=False,description='Enable cross-origin iframe support (OOPIF/Out-of-Process iframes). When False (default), only same-origin frames are processed to avoid complexity and hanging.',)diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex ef9368a84..c0748defc 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -694,6 +694,9 @@ 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+ if 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})')return session@@ -723,6 +726,8 @@ 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(f'[get_or_create_cdp_session] Created session for {target_id} without changing focus (still on {self.agent_focus.target_id})'diff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex 86e8a896b..bae957f9c 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -28,7 +28,7 @@ if TYPE_CHECKING:# TODO: enable cross origin iframes -> experimental for now-ENABLE_CROSS_ORIGIN_IFRAMES = True+ENABLE_CROSS_ORIGIN_IFRAMES = Falseclass DomService: