Skip to content

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

improve chrome launch args handling for disable security case

Nick Sweetingdbde8af4b818
diff --git a/browser_use/browser/browser.py b/browser_use/browser/browser.py
index 075c6a5ab..78b870aaf 100644
--- a/browser_use/browser/browser.py
+++ b/browser_use/browser/browser.py
@@ -6,9 +6,11 @@ import asyncio
import gc
import logging
import os
+import subprocess
from dataclasses import dataclass, field
from typing import Literal
+import requests
from playwright._impl._api_structures import ProxySettings
from playwright.async_api import Browser as PlaywrightBrowser
from playwright.async_api import (
@@ -16,7 +18,7 @@ from playwright.async_api import (
async_playwright,
)
-from browser_use.browser.chrome import CHROME_ARGS, CHROME_DOCKER_ARGS, CHROME_HEADLESS_ARGS
+from browser_use.browser.chrome import CHROME_ARGS, CHROME_DISABLE_SECURITY_ARGS, CHROME_DOCKER_ARGS, CHROME_HEADLESS_ARGS
from browser_use.browser.context import BrowserContext, BrowserContextConfig
from browser_use.browser.utils.screen_resolution import get_screen_resolution, get_window_adjustments
from browser_use.utils import time_execution_async
@@ -86,14 +88,6 @@ class Browser:
self.playwright: Playwright | None = None
self.playwright_browser: PlaywrightBrowser | None = None
- self.disable_security_args = []
- if self.config.disable_security:
- self.disable_security_args = ['--disable-web-security', '--disable-site-isolation-trials']
- if self.config.browser_class == 'chromium':
- self.disable_security_args += [
- '--disable-features=IsolateOrigins,site-per-process',
- ]
-
async def new_context(self, config: BrowserContextConfig = BrowserContextConfig()) -> BrowserContext:
"""Create a browser context"""
return BrowserContext(config=config, browser=self)
@@ -143,9 +137,6 @@ class Browser:
"""Sets up and returns a Playwright Browser instance with anti-detection measures."""
if not self.config.browser_instance_path:
raise ValueError('Chrome instance path is required')
- import subprocess
-
- import requests
try:
# Check if browser is already running
@@ -168,6 +159,7 @@ class Browser:
*CHROME_ARGS,
*(CHROME_DOCKER_ARGS if IN_DOCKER else []),
*(CHROME_HEADLESS_ARGS if self.config.headless else []),
+ *(CHROME_DISABLE_SECURITY_ARGS if self.config.disable_security else []),
*self.config.extra_browser_args,
}
)
@@ -207,38 +199,23 @@ class Browser:
offset_x, offset_y = get_window_adjustments()
browser_class = getattr(playwright, self.config.browser_class)
args = {
- 'chromium': [
- '--no-sandbox',
- '--disable-blink-features=AutomationControlled',
- '--disable-infobars',
- '--disable-background-timer-throttling',
- '--disable-popup-blocking',
- '--disable-backgrounding-occluded-windows',
- '--disable-renderer-backgrounding',
- '--disable-window-activation',
- '--disable-focus-on-load',
- '--no-first-run',
- '--no-default-browser-check',
- '--no-startup-window',
- f'--window-position={offset_x},{offset_y}',
- f'--window-size={screen_size["width"]},{screen_size["height"]}',
- '--force-device-scale-factor=1',
- '--window-position=0,0',
- '--enable-experimental-extension-apis',
- *(CHROME_DOCKER_ARGS if IN_DOCKER else []),
- *(CHROME_HEADLESS_ARGS if self.config.headless else []),
- *CHROME_ARGS,
- *self.disable_security_args,
- *self.config.extra_browser_args,
- ],
+ 'chromium': list(
+ {
+ *CHROME_ARGS,
+ *(CHROME_DOCKER_ARGS if IN_DOCKER else []),
+ *(CHROME_HEADLESS_ARGS if self.config.headless else []),
+ *(CHROME_DISABLE_SECURITY_ARGS if self.config.disable_security else []),
+ f'--window-position={offset_x},{offset_y}',
+ f'--window-size={screen_size["width"]},{screen_size["height"]}',
+ *self.config.extra_browser_args,
+ }
+ ),
'firefox': [
'-no-remote',
- *self.disable_security_args,
*self.config.extra_browser_args,
],
'webkit': [
'--no-startup-window',
- *self.disable_security_args,
*self.config.extra_browser_args,
],
}
diff --git a/browser_use/browser/chrome.py b/browser_use/browser/chrome.py
index e62a7428c..5a952a157 100644
--- a/browser_use/browser/chrome.py
+++ b/browser_use/browser/chrome.py
@@ -52,15 +52,13 @@ CHROME_DISABLE_SECURITY_ARGS = [
'--disable-web-security', # <- WARNING, breaks some sites that expect/enforce strict CORS headers (try webflow.com)
'--disable-site-isolation-trials',
'--disable-features=IsolateOrigins,site-per-process',
- # '--allow-running-insecure-content', # Breaks CORS/CSRF/HSTS etc., useful sometimes but very easy to detect
- # '--disable-web-security',
- # '--disable-features=IsolateOrigins,site-per-process', // useful for injecting JS, but some very strict sites can panic / show error pages when isolation is disabled (e.g. webflow.com)
# '--allow-file-access-from-files', # <- WARNING, dangerous, allows JS to read filesystem using file:// URLs
# DANGER: Disable HTTPS verification
- # '--ignore-certificate-errors',
- # '--ignore-ssl-errors',
- # '--ignore-certificate-errors-spki-list',
- # '--allow-insecure-localhost',
+ '--allow-running-insecure-content', # Breaks CORS/CSRF/HSTS etc., useful sometimes but very easy to detect
+ '--ignore-certificate-errors',
+ '--ignore-ssl-errors',
+ '--ignore-certificate-errors-spki-list',
+ '--allow-insecure-localhost',
]
CHROME_ARGS = [
@@ -87,8 +85,9 @@ CHROME_ARGS = [
# chrome://version
# f'--user-agent="{DEFAULT_USER_AGENT}"',
# f'--window-size={DEFAULT_VIEWPORT.width},{DEFAULT_VIEWPORT.height}',
- '--window-position=0,0',
- '--start-maximized',
+ # '--window-position=0,0',
+ # '--start-maximized',
+ '--force-device-scale-factor=1',
'--hide-scrollbars', # hide scrollbars because otherwise they show up in screenshots
'--install-autogenerated-theme=0,0,0', # black border makes it easier to see which chrome window is browser-use's
#'--virtual-time-budget=60000', # fast-forward all animations & timers by 60s, dont use this it's unfortunately buggy and breaks screenshot and PDF capture sometimes
@@ -104,6 +103,7 @@ CHROME_ARGS = [
# GPU, canvas, text, and pdf rendering config
# chrome://gpu
'--enable-webgl', # enable web-gl graphics support
+ '--enable-experimental-extension-apis', # add support for tab groups
'--font-render-hinting=none', # make rendering more deterministic by ignoring OS font hints, may also need css override, try: * {text-rendering: geometricprecision !important; -webkit-font-smoothing: antialiased;}
'--force-color-profile=srgb', # make rendering more deterministic by using consitent color profile, if browser looks weird, try: generic-rgb
'--disable-partial-raster', # make rendering more deterministic (TODO: verify if still needed)
@@ -126,6 +126,8 @@ CHROME_ARGS = [
'--disable-extensions-http-throttling', # dont throttle http traffic based on runtime heuristics
'--disable-field-trial-config', # disable shared field trial state between browser processes
'--disable-back-forward-cache', # disable browsing navigation cache
+ '--disable-focus-on-load', # prevent browser from hijacking focus
+ '--disable-window-activation',
# '--in-process-gpu', <- DONT USE THIS, makes headful startup time ~5-10s slower (tested v121 Google Chrome.app on macOS)
# '--disable-component-extensions-with-background-pages', # TODO: check this, disables chrome components that only run in background with no visible UI (could lower startup time)
# uncomment to disable hardware camera/mic/speaker access + present fake devices to websites
@@ -141,6 +143,7 @@ CHROME_ARGS = [
'--no-pings',
'--no-first-run',
'--no-default-browser-check',
+ '--no-startup-window',
'--disable-default-apps',
'--ash-no-nudges',
'--disable-infobars',