Skip to content

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

added better error handling

paperboardofficial76c361f8435c
diff --git a/browser_use/browser/utils/screen_resolution.py b/browser_use/browser/utils/screen_resolution.py
index 7f6422ffd..30541b34d 100644
--- a/browser_use/browser/utils/screen_resolution.py
+++ b/browser_use/browser/utils/screen_resolution.py
@@ -1,24 +1,31 @@
import sys
def get_screen_resolution():
- if sys.platform == "darwin": # Correct check for macOS
+ if sys.platform == "darwin": # macOS
try:
- from AppKit import NSScreen # macOS-only import
+ from AppKit import NSScreen
screen = NSScreen.mainScreen().frame()
return {"width": int(screen.size.width), "height": int(screen.size.height)}
except ImportError:
print("AppKit is not available. Make sure you are running this on macOS.")
- return {"width": 1920, "height": 1080}
+ except Exception as e:
+ print(f"Error retrieving macOS screen resolution: {e}")
+ return {"width": 2560, "height": 1664}
else: # Windows & Linux
try:
- from screeninfo import get_monitors # Cross-platform library
- monitor = get_monitors()[0] # Get primary monitor
+ from screeninfo import get_monitors
+ monitors = get_monitors()
+ if not monitors:
+ raise Exception("No monitors detected.")
+ monitor = monitors[0]
return {"width": monitor.width, "height": monitor.height}
except ImportError:
- print("screeninfo package not found. Install it using 'pip install screeninfo'")
- return {"width": 1920, "height": 1080}
-
+ print("screeninfo package not found. Install it using 'pip install screeninfo'.")
+ except Exception as e:
+ print(f"Error retrieving screen resolution: {e}")
+
+ return {"width": 1920, "height": 1080}
def get_window_adjustments():
"""Returns recommended x, y offsets for window positioning"""
@@ -26,5 +33,5 @@ def get_window_adjustments():
return -4, 24 # macOS has a small title bar, no border
elif sys.platform == "win32": # Windows
return -8, 0 # Windows has a border on the left
- else: # Linux (varies by window manager)
- return 0, 0 # Adjust as needed for your WM
\ No newline at end of file
+ else: # Linux
+ return 0, 0
\ No newline at end of file