Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Works to remove cookies
Magnus Müllerf50cc45b51db
diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.pyindex 15f638e97..cce3286c8 100644--- a/browser_use/browser/profile.py+++ b/browser_use/browser/profile.py@@ -172,6 +172,11 @@ CHROME_DEFAULT_ARGS = ['--disable-extensions-http-throttling','--extensions-on-chrome-urls','--disable-default-apps',+ '--disable-component-extensions-with-background-pages',+ '--disable-background-networking',+ '--disable-extensions-except-api',+ '--disable-extension-content-verification',+ '--allow-running-insecure-content',f'--disable-features={",".join(CHROME_DISABLED_COMPONENTS)}',]@@ -558,6 +563,7 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Brodefault=True,description="Enable automation-optimized extensions: ad blocking (uBlock Origin), cookie handling (I still don't care about cookies), and URL cleaning (ClearURLs). All extensions work automatically without manual intervention. Extensions are automatically downloaded and loaded when enabled.",)+window_size: ViewportSize | None = Field(default=None,description='Browser window size to use when headless=False.',@@ -753,32 +759,34 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro"""# Extension definitions - optimized for automation and content extraction+ # Combines uBlock Origin (ad blocking) + "I still don't care about cookies" (cookie banner handling)extensions = [{'name': 'uBlock Origin','id': 'cjpalhdlnbpafiamejdnhcphjbkeiagm',- 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dcjpalhdlnbpafiamejdnhcphjbkeiagm%26uc',+ 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=133&acceptformat=crx3&x=id%3Dcjpalhdlnbpafiamejdnhcphjbkeiagm%26uc',+ },+ {+ 'name': "I still don't care about cookies",+ 'id': 'edibdbjcniadpccecjdfdjjppcpchdlm',+ 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=133&acceptformat=crx3&x=id%3Dedibdbjcniadpccecjdfdjjppcpchdlm%26uc',},- # {- # 'name': "I still don't care about cookies",- # 'id': 'edibdbjcniadpccecjdfdjjppcpchdlm',- # 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dedibdbjcniadpccecjdfdjjppcpchdlm%26uc',- # },{'name': 'ClearURLs','id': 'lckanjgmijmafbedllaakclkaicjfmnk',- 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dlckanjgmijmafbedllaakclkaicjfmnk%26uc',+ 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=133&acceptformat=crx3&x=id%3Dlckanjgmijmafbedllaakclkaicjfmnk%26uc',},# {# 'name': 'Captcha Solver: Auto captcha solving service',# 'id': 'pgojnojmmhpofjgdmaebadhbocahppod',# 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dpgojnojmmhpofjgdmaebadhbocahppod%26uc',# },- {- 'name': 'Consent-O-Matic',- 'id': 'mdjildafknihdffpkfmmpnpoiajfjnjd',- 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dmdjildafknihdffpkfmmpnpoiajfjnjd%26uc',- },+ # Consent-O-Matic disabled - using uBlock Origin's cookie lists instead for simplicity+ # {+ # 'name': 'Consent-O-Matic',+ # 'id': 'mdjildafknihdffpkfmmpnpoiajfjnjd',+ # 'url': 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=130&acceptformat=crx3&x=id%3Dmdjildafknihdffpkfmmpnpoiajfjnjd%26uc',+ # },# {# 'name': 'Privacy | Protect Your Payments',# 'id': 'hmgpakheknboplhmlicfkkgjipfabmhp',@@ -816,6 +824,10 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro# Extract extensionlogger.info(f'📂 Extracting {ext["name"]} extension...')self._extract_extension(crx_file, ext_dir)++ # Log extension version info+ self._log_extension_version(ext_dir, ext['name'])+extension_paths.append(str(ext_dir))loaded_extension_names.append(ext['name'])@@ -823,6 +835,11 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Brologger.warning(f'⚠️ Failed to setup {ext["name"]} extension: {e}')continue+ # Apply minimal patch to cookie extension - hardcode nature.com whitelist+ for i, path in enumerate(extension_paths):+ if loaded_extension_names[i] == "I still don't care about cookies":+ self._apply_minimal_extension_patch(Path(path))+if extension_paths:logger.debug(f'[BrowserProfile] 🧩 Extensions loaded ({len(extension_paths)}): [{", ".join(loaded_extension_names)}]')else:@@ -830,6 +847,111 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Broreturn extension_paths+ def _log_extension_version(self, ext_dir: Path, ext_name: str) -> None:+ """Log extension version information from manifest."""+ try:+ manifest_path = ext_dir / 'manifest.json'+ if manifest_path.exists():+ import json++ with open(manifest_path, 'r', encoding='utf-8') as f:+ manifest = json.load(f)++ version = manifest.get('version', 'unknown')+ manifest_version = manifest.get('manifest_version', 'unknown')++ logger.info(f'📦 {ext_name} v{version} (manifest v{manifest_version}) loaded')++ # Special logging for uBlock Origin to show it's current+ if ext_name == 'uBlock Origin':+ logger.info(f'🛡️ uBlock Origin version {version} - Latest is 1.61.2+ (as of Aug 2025)')++ except Exception as e:+ logger.debug(f'Could not read version for {ext_name}: {e}')++ def _get_extension_name(self, ext_dir: Path) -> str | None:+ """Get the actual extension name from manifest."""+ try:+ manifest_path = ext_dir / 'manifest.json'+ if manifest_path.exists():+ import json++ with open(manifest_path, 'r', encoding='utf-8') as f:+ manifest = json.load(f)++ name = manifest.get('name', '')+ if name.startswith('__MSG_'):+ # Resolve localized name+ locale_path = ext_dir / '_locales' / 'en' / 'messages.json'+ if locale_path.exists():+ with open(locale_path, 'r', encoding='utf-8') as f:+ messages = json.load(f)+ key = name.replace('__MSG_', '').replace('__', '')+ return messages.get(key, {}).get('message', name)+ return name+ except Exception:+ pass+ return None++ def _apply_minimal_extension_patch(self, ext_dir: Path) -> None:+ """Minimal patch: pre-populate chrome.storage.local with nature.com whitelist."""+ try:+ bg_path = ext_dir / 'data' / 'background.js'+ if not bg_path.exists():+ return++ with open(bg_path, 'r', encoding='utf-8') as f:+ content = f.read()++ # Find the initialize() function and inject storage setup before updateSettings()+ # The actual function uses 2-space indentation, not tabs+ old_init = """async function initialize(checkInitialized, magic) {+ if (checkInitialized && initialized) {+ return;+ }+ loadCachedRules();+ await updateSettings();+ await recreateTabList(magic);+ initialized = true;+}"""++ # New function with nature.com whitelist initialization+ new_init = """// Pre-populate storage with nature.com whitelist if empty+async function ensureWhitelistStorage() {+ const result = await chrome.storage.local.get({ settings: null });+ if (!result.settings) {+ const defaultSettings = {+ statusIndicators: true,+ whitelistedDomains: { "nature.com": true }+ };+ await chrome.storage.local.set({ settings: defaultSettings });+ }+}++async function initialize(checkInitialized, magic) {+ if (checkInitialized && initialized) {+ return;+ }+ loadCachedRules();+ await ensureWhitelistStorage(); // Add storage initialization+ await updateSettings();+ await recreateTabList(magic);+ initialized = true;+}"""++ if old_init in content:+ content = content.replace(old_init, new_init)++ with open(bg_path, 'w', encoding='utf-8') as f:+ f.write(content)++ logger.info('[BrowserProfile] ✅ Cookie extension: nature.com pre-populated in storage')+ else:+ logger.debug('[BrowserProfile] Initialize function not found for patching')++ except Exception as e:+ logger.debug(f'[BrowserProfile] Could not patch extension storage: {e}')+def _download_extension(self, url: str, output_path: Path) -> None:"""Download extension .crx file."""import urllib.requestdiff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 5e6e9d578..be3858964 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -630,6 +630,9 @@ class BrowserSession(BaseModel):# # Wait a bit to ensure page starts loading# await asyncio.sleep(0.5)+ # Close any extension options pages that might have opened+ await self._close_extension_options_pages()+# Dispatch navigation completeself.logger.debug(f'Dispatching NavigationCompleteEvent for {event.url} (tab #{target_id[-4:]})')await self.event_bus.dispatch(@@ -1589,21 +1592,29 @@ class BrowserSession(BaseModel):except Exception as e:self.logger.warning(f'Failed to remove highlights: {e}')- # Try again with simpler script if the complex one fails- try:- simple_script = """- const highlights = document.querySelectorAll('[data-browser-use-highlight]');- highlights.forEach(el => el.remove());- const container = document.getElementById('browser-use-debug-highlights');- if (container) container.remove();- """- cdp_session = await self.get_or_create_cdp_session()- await cdp_session.cdp_client.send.Runtime.evaluate(- params={'expression': simple_script}, session_id=cdp_session.session_id- )- self.logger.debug('Fallback highlight removal completed')- except Exception as fallback_error:- self.logger.error(f'Both highlight removal attempts failed: {fallback_error}')++ async def _close_extension_options_pages(self) -> None:+ """Close any extension options/welcome pages that have opened."""+ try:+ # Get all open pages+ targets = await self._cdp_get_all_pages()++ for target in targets:+ target_url = target.get('url', '')+ target_id = target.get('targetId', '')++ # Check if this is an extension options/welcome page+ if 'chrome-extension://' in target_url and (+ 'options.html' in target_url or 'welcome.html' in target_url or 'onboarding.html' in target_url+ ):+ self.logger.info(f'[BrowserSession] 🚫 Closing extension options page: {target_url}')+ try:+ await self._cdp_close_page(target_id)+ except Exception as e:+ self.logger.debug(f'[BrowserSession] Could not close extension page {target_id}: {e}')++ except Exception as e:+ self.logger.debug(f'[BrowserSession] Error closing extension options pages: {e}')@propertydef downloaded_files(self) -> list[str]: