Skip to content

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

Refactor screenshot saving in python_highlights.py to use aiofiles for asynchronous file writing and update .gitignore to ensure screenshot.png is properly excluded.

Magnus Müller267ff76e5064
diff --git a/.gitignore b/.gitignore
index 3072cda88..1b5494b93 100644
--- a/.gitignore
+++ b/.gitignore
@@ -57,4 +57,4 @@ token.json
temp-profile-*
-screenshot.png
\ No newline at end of file
+screenshot.png
diff --git a/browser_use/browser/python_highlights.py b/browser_use/browser/python_highlights.py
index 46362099f..9146bdb2d 100644
--- a/browser_use/browser/python_highlights.py
+++ b/browser_use/browser/python_highlights.py
@@ -4,7 +4,6 @@ This module replaces JavaScript-based highlighting with fast Python image proces
to draw bounding boxes around interactive elements directly on screenshots.
"""
-import asyncio
import base64
import io
import logging
@@ -480,11 +479,9 @@ async def create_highlighted_screenshot_async(
filename = os.getenv('BROWSER_USE_SCREENSHOT_FILE')
if filename:
+ import aiofiles
- async def _write_screenshot():
- with open(filename, 'wb') as f:
- f.write(base64.b64decode(final_screenshot))
- logger.debug('Saved screenshot to ' + str(filename))
-
- await asyncio.to_thread(_write_screenshot)
+ async with aiofiles.open(filename, 'wb') as f:
+ await f.write(base64.b64decode(final_screenshot))
+ logger.debug('Saved screenshot to ' + str(filename))
return final_screenshot