Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Fix screenshot
Magnus Müller37446f54cff5
diff --git a/browser_use/browser/dom_watchdog.py b/browser_use/browser/dom_watchdog.pyindex 605cb7fe8..221e814e2 100644--- a/browser_use/browser/dom_watchdog.py+++ b/browser_use/browser/dom_watchdog.py@@ -264,14 +264,10 @@ class DOMWatchdog(BaseWatchdog):screenshot_event = self.event_bus.dispatch(ScreenshotEvent(full_page=False, event_timeout=6.0))self.logger.debug('📸 Dispatched ScreenshotEvent, waiting for event to complete...')- # Get the screenshot bytes directly from event_result()- screenshot_bytes = await screenshot_event.event_result(raise_if_any=True, raise_if_none=True)- if screenshot_bytes:- # Convert bytes back to base64 string for BrowserStateSummary- import base64-- screenshot_b64 = base64.b64encode(screenshot_bytes).decode('utf-8')- self.logger.debug(f'📸 Got screenshot: {len(screenshot_bytes)} bytes')+ # Get the screenshot base64 string directly from event_result()+ screenshot_b64 = await screenshot_event.event_result(raise_if_any=True, raise_if_none=True)+ if screenshot_b64:+ self.logger.debug(f'📸 Got screenshot: {len(screenshot_b64)} characters')else:screenshot_b64 = Noneself.logger.warning('📸 Screenshot handler returned None')diff --git a/browser_use/browser/screenshot_watchdog.py b/browser_use/browser/screenshot_watchdog.pyindex dd7e3e0cc..5dbe2f598 100644--- a/browser_use/browser/screenshot_watchdog.py+++ b/browser_use/browser/screenshot_watchdog.py@@ -21,14 +21,14 @@ class ScreenshotWatchdog(BaseWatchdog):# Events this watchdog emitsEMITS: ClassVar[list[type[BaseEvent[Any]]]] = []- async def on_ScreenshotEvent(self, event: ScreenshotEvent) -> bytes:+ async def on_ScreenshotEvent(self, event: ScreenshotEvent) -> str:"""Handle screenshot request using CDP.Args:event: ScreenshotEvent with optional full_page and clip parametersReturns:- Screenshot as bytes (base64-encoded PNG data)+ Screenshot as base64-encoded stringRaises:Exception: If screenshot capture fails@@ -58,7 +58,7 @@ class ScreenshotWatchdog(BaseWatchdog):self.logger.debug(f'[ScreenshotWatchdog] Taking screenshot with params: {params}')result = await cdp_session.cdp_client.send.Page.captureScreenshot(params=params, session_id=cdp_session.session_id)- # Return base64-encoded screenshot data as bytes+ # Return base64-encoded screenshot data as stringif result and 'data' in result:self.logger.debug('[ScreenshotWatchdog] Screenshot captured successfully')@@ -69,10 +69,8 @@ class ScreenshotWatchdog(BaseWatchdog):except Exception as e:self.logger.debug(f'[ScreenshotWatchdog] Failed to remove highlights: {e}')- # Convert base64 string to bytes- import base64-- return base64.b64decode(result['data'])+ # Return the base64 string directly (no need to decode and re-encode)+ return result['data']else:self.logger.warning('[ScreenshotWatchdog] Screenshot result missing data')raise Exception('Screenshot capture failed: no data returned')