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
stop agent if browser closed
Laith Weinberger9e95241bbbb3
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 170cc30e7..f497d010b 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -1187,6 +1187,13 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.logger.warning(f'{error_msg}')return+ # Handle browser closed/disconnected errors - stop immediately instead of retrying+ if self._is_browser_closed_error(error):+ self.logger.warning(f'🛑 Browser closed or disconnected: {error}')+ self.state.stopped = True+ self._external_pause_event.set()+ return+# Handle all other exceptionsinclude_trace = self.logger.isEnabledFor(logging.DEBUG)error_msg = AgentError.format_error(error, include_trace=include_trace)@@ -1209,6 +1216,10 @@ class Agent(Generic[Context, AgentStructuredOutput]):self.state.last_result = [ActionResult(error=error_msg)]return None+ def _is_browser_closed_error(self, error: Exception) -> bool:+ """Check if the browser has been closed or disconnected."""+ return self.browser_session._cdp_client_root is None+async def _finalize(self, browser_state_summary: BrowserStateSummary | None) -> None:"""Finalize the step with history, logging, and events"""step_end_time = time.time()