Skip to content

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

hack to force browser validity

Nick Sweeting96e174396234
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index cccec9649..4c389132b 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -709,6 +709,14 @@ class BrowserSession(BaseModel):
async def _take_screenshot_hybrid(self, page: Page, clip: dict[str, int] | None = None) -> str:
"""Take screenshot using Playwright, with retry and semaphore protection."""
# Use Playwright screenshot directly
+
+ assert self.browser_context and self.is_connected(restart=True)
+ try:
+ page = [p for p in self.browser_context.pages if p.url == page.url][0]
+ except Exception:
+ pass
+ assert await page.evaluate('() => true'), 'Page is not usable before screenshot!'
+
screenshot = await page.screenshot(
full_page=False,
scale='css',
@@ -717,6 +725,7 @@ class BrowserSession(BaseModel):
animations='allow',
caret='initial',
)
+ assert await page.evaluate('() => true'), 'Page is not usable after screenshot!'
screenshot_b64 = base64.b64encode(screenshot).decode('utf-8')
assert screenshot_b64, 'Playwright page.screenshot() returned empty base64'
return screenshot_b64
diff --git a/tests/ci/test_sequential_agents_reuse.py b/tests/ci/test_sequential_agents_reuse.py
index 44429aedd..0a149db92 100644
--- a/tests/ci/test_sequential_agents_reuse.py
+++ b/tests/ci/test_sequential_agents_reuse.py
@@ -243,15 +243,15 @@ class TestSequentialAgentsReuse:
# Agent 2 switches back to first tab
agent2_actions = [
- """{{
+ """{
"thinking": "Switching to first tab",
"evaluation_previous_goal": "Two tabs are open",
"memory": "Need to switch to tab 0",
"next_goal": "Switch to first tab",
"action": [
- {{"switch_tab": {{"page_id": 0}}}}
+ {"switch_tab": {"page_id": 0}}
]
- }}"""
+ }"""
]
agent2 = Agent(