Skip to content

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

keep reference to the original browser session when copying

Nick Sweeting747959a3d09d
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 65acb90c9..2caf88d46 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -270,6 +270,10 @@ class AgentHistoryList(BaseModel):
total += h.metadata.duration_seconds
return total
+ def __len__(self) -> int:
+ """Return the number of history items"""
+ return len(self.history)
+
def __str__(self) -> str:
"""Representation of the AgentHistoryList object"""
return f'AgentHistoryList(all_results={self.action_results()}, all_model_outputs={self.model_actions()})'
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 1eac26d13..ee666bbd4 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -217,6 +217,7 @@ class BrowserSession(BaseModel):
_tab_visibility_callback: Any = PrivateAttr(default=None)
_logger: logging.Logger | None = PrivateAttr(default=None)
_downloaded_files: list[str] = PrivateAttr(default_factory=list)
+ _original_browser_session: Any = PrivateAttr(default=None) # Reference to prevent GC of the original session when copied
@model_validator(mode='after')
def apply_session_overrides_to_profile(self) -> Self:
@@ -2658,7 +2659,7 @@ class BrowserSession(BaseModel):
# Take the screenshot using CDP
# https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot
# Use the internal channel.send method to bypass some of the async wrapper issues
- from playwright._impl._cdp_session import locals_to_params
+ from playwright._impl._helper import locals_to_params
result = await cdp_session._impl_obj._channel.send(
'send', locals_to_params({'method': 'Page.captureScreenshot', 'params': cdp_params})
@@ -2683,7 +2684,7 @@ class BrowserSession(BaseModel):
if cdp_session:
try:
# Use internal method to ensure proper cleanup
- from playwright._impl._cdp_session import locals_to_params
+ from playwright._impl._helper import locals_to_params
await cdp_session._impl_obj._channel.send('detach', locals_to_params({}))
except Exception: