Skip to content

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

cap all screenshots at 6000px max height

Nick Sweeting75fe98ff659d
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 194c6d152..c5d412cee 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -2540,11 +2540,12 @@ class BrowserSession(BaseModel):
# Fallback method: manually expand the viewport and take a screenshot of the entire viewport
- # 1. Get current page dimensions
+ # 1. Get current viewport dimensions (not page dimensions)
dimensions = await page.evaluate("""() => {
return {
- width: window.innerWidth,
- height: window.innerHeight,
+ width: Math.max(window.innerWidth, document.documentElement.clientWidth),
+ height: Math.max(window.innerHeight, document.documentElement.clientHeight),
+ pageHeight: document.documentElement.scrollHeight,
devicePixelRatio: window.devicePixelRatio || 1
};
}""")
@@ -2553,9 +2554,20 @@ class BrowserSession(BaseModel):
original_viewport = page.viewport_size
viewport_expansion = self.browser_profile.viewport_expansion if self.browser_profile.viewport_expansion else 0
+ # Use viewport height, not full page height
expanded_width = dimensions['width'] # Keep width unchanged
expanded_height = dimensions['height'] + viewport_expansion
+ # Limit the height to avoid timeouts on very long pages
+ # Even for full_page=True, we'll take a large but bounded screenshot
+ max_screenshot_height = 6000 # 6k pixels max to prevent timeouts
+ if full_page and dimensions['pageHeight'] > max_screenshot_height:
+ self.logger.warning(
+ f'Page height ({dimensions["pageHeight"]}px) exceeds max screenshot height ({max_screenshot_height}px). '
+ f'Clipping to {max_screenshot_height}px.'
+ )
+ expanded_height = min(expanded_height, max_screenshot_height)
+
# 3. Expand the viewport if we are using one
if original_viewport:
await asyncio.wait_for(
diff --git a/tests/ci/test_browser_session_screenshots.py b/tests/ci/test_browser_session_screenshots.py
index 375b4bbab..0e5ea5f3e 100644
--- a/tests/ci/test_browser_session_screenshots.py
+++ b/tests/ci/test_browser_session_screenshots.py
@@ -213,11 +213,9 @@ class TestHeadlessScreenshots:
# Verify PNG signature
assert screenshot_bytes.startswith(b'\x89PNG\r\n\x1a\n'), f'Session {i} screenshot is not a valid PNG'
- # Full page screenshot of 50,000px+ page should be quite large
- # Typically at least 100KB+ for such a long page
- assert len(screenshot_bytes) > 50000, (
- f'Session {i} screenshot too small for a 50,000px+ page: {len(screenshot_bytes)} bytes'
- )
+ # Full page screenshot should be reasonably large
+ # Due to our 6,000px height limit, expect at least 30KB
+ assert len(screenshot_bytes) > 30000, f'Session {i} screenshot too small: {len(screenshot_bytes)} bytes'
print(f'All {len(screenshots)} screenshots validated successfully!')