Skip to content

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

tweak screenshot timing

Nick Sweeting54a413d25595
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index a85f709ac..943b44c39 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -713,11 +713,12 @@ class BrowserSession(BaseModel):
@retry(
wait=1, # wait 1s between each attempt to take a screenshot
retries=2, # try up to 2 times to take the screenshot
- timeout=20, # allow up to 20s for each attempt to take a screenshot
+ timeout=15, # allow up to 15s for each attempt to take a screenshot
semaphore_limit=1, # Only one screenshot at a time across all processes
semaphore_name='screenshot_global',
semaphore_scope='multiprocess',
semaphore_lax=True, # Continue without semaphore if it can't be acquired
+ semaphore_timeout=20, # Wait up to 20s for semaphore acquisition
)
async def _take_screenshot_hybrid(self, page: Page, clip: dict[str, int] | None = None) -> str:
"""Take screenshot using Playwright, with retry and semaphore protection."""
diff --git a/tests/ci/test_browser_session_screenshots.py b/tests/ci/test_browser_session_screenshots.py
index a3d04323e..c80d9a036 100644
--- a/tests/ci/test_browser_session_screenshots.py
+++ b/tests/ci/test_browser_session_screenshots.py
@@ -4,6 +4,7 @@ Test that screenshots work correctly in headless browser mode.
import asyncio
import base64
+import time
from browser_use.browser import BrowserProfile, BrowserSession
@@ -195,10 +196,17 @@ class TestHeadlessScreenshots:
print('Navigating all sessions to the long test page...')
await asyncio.gather(*[session.navigate(test_url) for session in browser_sessions])
- # Take screenshots from all sessions at the same time
- print('Taking screenshots from all 10 sessions simultaneously...')
+ # Take screenshots from all sessions
+ # Due to semaphore_limit=1, these will execute sequentially
+ print('Taking screenshots from all 10 sessions...')
+ start_time = time.time()
screenshot_tasks = [session.take_screenshot() for session in browser_sessions]
screenshots = await asyncio.gather(*screenshot_tasks)
+ total_time = time.time() - start_time
+
+ # Verify all screenshots completed within 1 minute
+ assert total_time < 60, f'Screenshots took too long: {total_time:.1f}s (should be < 60s)'
+ print(f'All screenshots completed in {total_time:.1f}s')
# Verify all screenshots are valid
print('Verifying all screenshots...')
@@ -223,9 +231,13 @@ class TestHeadlessScreenshots:
print(f'All {len(screenshots)} screenshots validated successfully!')
- # Also test taking regular (viewport) screenshots in parallel
- print('Taking viewport screenshots from all sessions simultaneously...')
+ # Also test taking regular (viewport) screenshots
+ print('Taking viewport screenshots from all sessions...')
+ start_time = time.time()
viewport_screenshots = await asyncio.gather(*[session.take_screenshot() for session in browser_sessions])
+ viewport_time = time.time() - start_time
+ assert viewport_time < 60, f'Viewport screenshots took too long: {viewport_time:.1f}s (should be < 60s)'
+ print(f'All viewport screenshots completed in {viewport_time:.1f}s')
# Verify viewport screenshots
for i, screenshot in enumerate(viewport_screenshots):