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 clear context and check connection properly using async call
Nick Sweeting51b7e8ae663a
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex b489bb02b..35d17c1c9 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -1422,17 +1422,26 @@ class BrowserSession(BaseModel):# Check if the browser_context itself is closed/unusabletry:- # TODO: figure out a better synchronous test for whether browser_context is usable- # this is a hacky workaround for the fact that playwright's browser_context has no is_connected() method- # and browser_context.browser is None when we launch with a persistent context (basically always)+ # The only reliable way to check if a browser context is still valid+ # is to try to use it. We'll try a simple page.evaluate() call.if self.browser_context.pages:- return True+ # Use the first available page to test the connection+ test_page = self.browser_context.pages[0]+ # Try a simple evaluate to check if the connection is alive+ result = await test_page.evaluate('() => true')+ return result is Trueelif restart:await self.create_new_tab()- return True+ # Test the new tab+ if self.browser_context.pages:+ test_page = self.browser_context.pages[0]+ result = await test_page.evaluate('() => true')+ return result is True+ return Falseelse:return Falseexcept Exception:+ # Any exception means the context is closed or invalidreturn Falsedef _reset_connection_state(self) -> None:@@ -3288,6 +3297,7 @@ class BrowserSession(BaseModel):new_page = await self.browser_context.new_page()except Exception:self.initialized = False+ self.browser_context = None # Clear the closed contextif not self.initialized or not self.browser_context:# If we were initialized but lost connection, reset state first to avoid infinite loops