Skip to content

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

Fix Bug: Async Function Error in Screenshot Handling

Magnus Müllere10826ca3484
diff --git a/browser_use/browser/python_highlights.py b/browser_use/browser/python_highlights.py
index 46362099f..96ab7ff38 100644
--- a/browser_use/browser/python_highlights.py
+++ b/browser_use/browser/python_highlights.py
@@ -481,10 +481,13 @@ async def create_highlighted_screenshot_async(
filename = os.getenv('BROWSER_USE_SCREENSHOT_FILE')
if filename:
- async def _write_screenshot():
- with open(filename, 'wb') as f:
- f.write(base64.b64decode(final_screenshot))
- logger.debug('Saved screenshot to ' + str(filename))
+ def _write_screenshot():
+ try:
+ with open(filename, 'wb') as f:
+ f.write(base64.b64decode(final_screenshot))
+ logger.debug('Saved screenshot to ' + str(filename))
+ except Exception as e:
+ logger.warning(f'Failed to save screenshot to {filename}: {e}')
await asyncio.to_thread(_write_screenshot)
return final_screenshot