Skip to content

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

better ignore_default_args set and no_viewport logic

Nick Sweeting8aacc64938ed
diff --git a/browser_use/browser/types.py b/browser_use/browser/types.py
index 353a4a615..ff2f7505f 100644
--- a/browser_use/browser/types.py
+++ b/browser_use/browser/types.py
@@ -117,7 +117,7 @@ CHROME_DETERMINISTIC_RENDERING_ARGS = [
CHROME_DEFAULT_ARGS = [
# provided by playwright by default: https://github.com/microsoft/playwright/blob/41008eeddd020e2dee1c540f7c0cdfa337e99637/packages/playwright-core/src/server/chromium/chromiumSwitches.ts#L76
- # we don't need to include them twice in our own config
+ # we don't need to include them twice in our own config, but it's harmless
'--disable-field-trial-config', # https://source.chromium.org/chromium/chromium/src/+/main:testing/variations/README.md
'--disable-background-networking',
'--disable-background-timer-throttling',
@@ -132,7 +132,7 @@ CHROME_DEFAULT_ARGS = [
'--disable-dev-shm-usage',
# '--disable-extensions',
# '--disable-features=' + disabledFeatures(assistantMode).join(','),
- '--allow-pre-commit-input',
+ '--allow-pre-commit-input', # let page JS run a little early before GPU rendering finishes
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-popup-blocking',
@@ -318,7 +318,7 @@ CliArgStr = Annotated[str, AfterValidator(validate_cli_arg)]
class BrowserContextArgs(BaseModel):
"""
Base model for common browser context parameters used by
- both new_context() and launch_persistent_context().
+ both BrowserType.new_context() and BrowserType.launch_persistent_context().
https://playwright.dev/python/docs/api/class-browser#browser-new-context
"""
@@ -408,7 +408,7 @@ class BrowserLaunchArgs(BaseModel):
headless: bool | None = None
args: list[CliArgStr] = Field(default_factory=list)
ignore_default_args: list[CliArgStr] = Field(
- default_factory=lambda: ['--enable-automation'],
+ default_factory=lambda: ['--enable-automation', '--disable-extensions'],
description='List of default CLI args to stop playwright from applying (see https://github.com/microsoft/playwright/blob/41008eeddd020e2dee1c540f7c0cdfa337e99637/packages/playwright-core/src/server/chromium/chromiumSwitches.ts)',
)
channel: BrowserChannel = BrowserChannel.CHROMIUM
@@ -456,9 +456,11 @@ class BrowserNewContextArgs(BrowserContextArgs):
https://playwright.dev/python/docs/api/class-browser#browser-new-context
"""
+ model_config = ConfigDict(extra='ignore', validate_assignment=False, revalidate_instances='always')
+
# storage_state is not supported in launch_persistent_context()
storage_state: str | Path | dict[str, Any] | None = None
- # TODO: use StorageState type
+ # TODO: use StorageState type instead of dict[str, Any]
# to apply this to existing contexts (incl cookies, localStorage, IndexedDB), see:
# - https://github.com/microsoft/playwright/pull/34591/files
@@ -489,6 +491,8 @@ class BrowserLaunchPersistentContextArgs(BrowserLaunchArgs, BrowserContextArgs):
https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context
"""
+ model_config = ConfigDict(extra='ignore', validate_assignment=False, revalidate_instances='always')
+
# Required parameter specific to launch_persistent_context
user_data_dir: str | Path = BROWSERUSE_PROFILES_DIR / 'default'
@@ -650,9 +654,9 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro
self.user_data_dir = Path(self.user_data_dir).expanduser().resolve()
self.user_data_dir.mkdir(parents=True, exist_ok=True)
- # clear any existing locks by any other chrome processes
- # hacky way to prevent chrome crashes from leaving dirs locked,
- # but can cause conflicts if they are actually running)
+ # clear any existing locks by any other chrome processes (hacky)
+ # helps stop chrome crashes from leaving the profile dir in a locked state and breaking subsequent runs,
+ # but can cause conflicts if the user actually tries to run multiple chrome copies on the same user_data_dir
singleton_lock = self.user_data_dir / 'SingletonLock'
if singleton_lock.exists():
singleton_lock.unlink()
@@ -660,6 +664,22 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro
f'⚠️ Multiple chrome processes may be trying to share user_data_dir={self.user_data_dir} which can lead to crashes and profile data corruption!'
)
+ # def preinstall_extensions(self) -> None:
+ # """Preinstall the extensions."""
+
+ # # create the local unpacked extensions dir
+ # extensions_dir = self.user_data_dir / 'Extensions'
+ # extensions_dir.mkdir(parents=True, exist_ok=True)
+
+ # # download from the chrome web store using the chrome web store api
+ # for extension_id in self.extension_ids_to_preinstall:
+ # extension_path = extensions_dir / f'{extension_id}.crx'
+ # if extension_path.exists():
+ # logger.warning(f'⚠️ Extension {extension_id} is already installed, skipping preinstall.')
+ # else:
+ # logger.info(f'🔍 Preinstalling extension {extension_id}...')
+ # # TODO: copy this from ArchiveBox implementation
+
def detect_display_configuration(self) -> None:
"""Detect the display size and set the screen option."""
@@ -687,11 +707,14 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro
# automatically setup viewport if any config requires it
use_viewport = self.headless or self.viewport or self.device_scale_factor
self.no_viewport = not use_viewport if self.no_viewport is None else self.no_viewport
+ use_viewport = not self.no_viewport
if use_viewport:
+ # if we are using viewport, make device_scale_factor and screen are set to real values to avoid easy fingerprinting
self.viewport = self.viewport or display_size or ViewportSize(width=1280, height=1100)
self.device_scale_factor = self.device_scale_factor or 1.0
self.screen = self.screen or display_size or ViewportSize(width=1280, height=1100)
else:
+ # device_scale_factor and screen are not supported non-viewport mode, the system monitor determines these
self.viewport = None
self.device_scale_factor = None
self.screen = None