Skip to content

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

allow checking BrowserSession.__eq__ to determine underlying browser identity

Nick Sweeting8b4f166398a3
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 51286adb9..75818f528 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -442,6 +442,22 @@ class BrowserSession(BaseModel):
await self.start()
return self
+ def __eq__(self, other: object) -> bool:
+ """Check if two BrowserSession instances are using the same browser and have the same Agent state."""
+
+ if not isinstance(other, BrowserSession):
+ return False
+
+ # Two sessions are considered equal if they're connected to the same remote browser and are looking at the same page
+ # the .browser, .browser_context, .playwright object identities don't have to match
+ return (
+ self.browser_pid == other.browser_pid
+ and self.cdp_url == other.cdp_url
+ and self.wss_url == other.wss_url
+ and self.agent_current_page == other.agent_current_page
+ and self.human_current_page == other.human_current_page
+ )
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
# self.logger.debug(
# f'⏹️ Stopping gracefully browser_pid={self.browser_pid} user_data_dir= {_log_pretty_path(self.browser_profile.user_data_dir) or "<incognito>"} keep_alive={self.browser_profile.keep_alive} (context manager exit)'