Skip to content

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

dynamic screen setting for browser

paperboardofficial34902a105817
diff --git a/browser_use/browser/browser.py b/browser_use/browser/browser.py
index 60ae374da..396cb8eec 100644
--- a/browser_use/browser/browser.py
+++ b/browser_use/browser/browser.py
@@ -15,6 +15,7 @@ from playwright.async_api import (
)
from browser_use.browser.context import BrowserContext, BrowserContextConfig
+from browser_use.browser.utils.screen_resolution import get_screen_resolution, get_window_adjustments
logger = logging.getLogger(__name__)
@@ -179,6 +180,8 @@ class Browser:
async def _setup_standard_browser(self, playwright: Playwright) -> PlaywrightBrowser:
"""Sets up and returns a Playwright Browser instance with anti-detection measures."""
+ screen_size = get_screen_resolution()
+ offset_x, offset_y = get_window_adjustments()
browser = await playwright.chromium.launch(
headless=self.config.headless,
args=[
@@ -194,8 +197,9 @@ class Browser:
'--no-first-run',
'--no-default-browser-check',
'--no-startup-window',
- '--window-position=0,0',
- # '--window-size=1280,1000',
+ f'--window-position={offset_x},{offset_y}',
+ f'--window-size={screen_size["width"]},{screen_size["height"]}',
+ '--force-device-scale-factor=1',
]
+ self.disable_security_args
+ self.config.extra_chromium_args,
diff --git a/browser_use/browser/context.py b/browser_use/browser/context.py
index 3016e117a..8c1c506da 100644
--- a/browser_use/browser/context.py
+++ b/browser_use/browser/context.py
@@ -266,8 +266,7 @@ class BrowserContext:
else:
# Original code for creating new context
context = await browser.new_context(
- viewport=self.config.browser_window_size,
- no_viewport=False,
+ no_viewport=True,
user_agent=self.config.user_agent,
java_script_enabled=True,
bypass_csp=self.config.disable_security,
diff --git a/browser_use/browser/tests/screenshot_test.py b/browser_use/browser/tests/screenshot_test.py
index 7255ccb61..be157e5ca 100644
--- a/browser_use/browser/tests/screenshot_test.py
+++ b/browser_use/browser/tests/screenshot_test.py
@@ -1,37 +1,34 @@
+import asyncio
import base64
import pytest
from browser_use.browser.browser import Browser, BrowserConfig
-
-async def browser():
- browser_service = Browser(config=BrowserConfig(headless=True))
- yield browser_service
-
- await browser_service.close()
-
-
-# @pytest.mark.skip(reason='takes too long')
-def test_take_full_page_screenshot(browser):
- # Go to a test page
- browser.go_to_url('https://example.com')
-
- # Take full page screenshot
- screenshot_b64 = browser.take_screenshot(full_page=True)
-
- # Verify screenshot is not empty and is valid base64
- assert screenshot_b64 is not None
- assert isinstance(screenshot_b64, str)
- assert len(screenshot_b64) > 0
-
- # Test we can decode the base64 string
+async def test_take_full_page_screenshot():
+ browser = Browser(config=BrowserConfig(headless=False, disable_security=True))
try:
- base64.b64decode(screenshot_b64)
- except Exception as e:
- pytest.fail(f'Failed to decode base64 screenshot: {str(e)}')
-
+ async with await browser.new_context() as context:
+ page = await context.get_current_page()
+ # Go to a test page
+ await page.goto('https://example.com')
+
+ await asyncio.sleep(3)
+ # Take full page screenshot
+ screenshot_b64 = await context.take_screenshot(full_page=True)
+ await asyncio.sleep(3)
+ # Verify screenshot is not empty and is valid base64
+ assert screenshot_b64 is not None
+ assert isinstance(screenshot_b64, str)
+ assert len(screenshot_b64) > 0
+
+ # Test we can decode the base64 string
+ try:
+ base64.b64decode(screenshot_b64)
+ except Exception as e:
+ pytest.fail(f'Failed to decode base64 screenshot: {str(e)}')
+ finally:
+ await browser.close()
if __name__ == '__main__':
- test_take_full_page_screenshot(Browser(config=BrowserConfig(headless=False)))
+ asyncio.run(test_take_full_page_screenshot())
diff --git a/browser_use/browser/utils/screen_resolution.py b/browser_use/browser/utils/screen_resolution.py
new file mode 100644
index 000000000..7f6422ffd
--- /dev/null
+++ b/browser_use/browser/utils/screen_resolution.py
@@ -0,0 +1,30 @@
+import sys
+
+def get_screen_resolution():
+ if sys.platform == "darwin": # Correct check for macOS
+ try:
+ from AppKit import NSScreen # macOS-only import
+ 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}
+
+ else: # Windows & Linux
+ try:
+ from screeninfo import get_monitors # Cross-platform library
+ monitor = get_monitors()[0] # Get primary monitor
+ 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}
+
+
+def get_window_adjustments():
+ """Returns recommended x, y offsets for window positioning"""
+ if sys.platform == "darwin": # macOS
+ 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