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
minor comments and logging improvements
Nick Sweeting3a86fac15f38
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex c1e34c4c0..340117516 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -41,7 +41,7 @@ logger = logging.getLogger('browser_use.browser.session')_GLOB_WARNING_SHOWN = False # used inside _is_url_allowed to avoid spamming the logs with the same warning multiple times-def _show_glob_warning(domain: str, glob: str):+def _log_glob_warning(domain: str, glob: str):global _GLOB_WARNING_SHOWNif not _GLOB_WARNING_SHOWN:logger.warning(@@ -53,7 +53,7 @@ def _show_glob_warning(domain: str, glob: str):_GLOB_WARNING_SHOWN = True-def truncate_url(s: str, max_len: int | None = 22) -> str:+def _log_pretty_url(s: str, max_len: int | None = 22) -> str:"""Truncate/pretty-print a URL with a maximum length, removing the protocol and www. prefix"""s = s.replace('https://', '').replace('http://', '').replace('www.', '')if max_len is not None and len(s) > max_len:@@ -61,8 +61,8 @@ def truncate_url(s: str, max_len: int | None = 22) -> str:return s-def pretty_path(path: Path) -> str:- """Pretty-print a path, removing the drive letter on Windows"""+def _log_pretty_path(path: Path) -> str:+ """Pretty-print a path, shorten home dir to ~ and cwd to ."""return str(path).replace(str(Path.home()), '~').replace(str(Path.cwd().resolve()), '.')@@ -249,6 +249,8 @@ class BrowserSession(BaseModel):async def stop(self) -> None:"""Shuts down the BrowserSession, killing the browser process if keep_alive=False"""+ self.initialized = False+if self.browser_profile.keep_alive:return # nothing to do if keep_alive=True, leave the browser running@@ -394,7 +396,7 @@ class BrowserSession(BaseModel):logger.info(f'🌎 Launching local browser 'f'driver={str(type(self.playwright).__module__).split(".")[0]} channel={self.browser_profile.channel.name.lower()} '- f'user_data_dir={pretty_path(self.browser_profile.user_data_dir) if self.browser_profile.user_data_dir else "None (incognito)"}'+ f'user_data_dir={_log_pretty_path(self.browser_profile.user_data_dir) if self.browser_profile.user_data_dir else "None (incognito)"}')if not self.browser_profile.user_data_dir:# if no user_data_dir is provided, launch an incognito context with no persistent user_data_dir@@ -413,7 +415,7 @@ class BrowserSession(BaseModel):if f'--user-data-dir={self.browser_profile.user_data_dir}' in (proc.info['cmdline'] or []):logger.warning(f'🚨 Found potentially conflicting browser process browser_pid={proc.info["pid"]} '- f'already running with the same user_data_dir={pretty_path(self.browser_profile.user_data_dir)}'+ f'already running with the same user_data_dir={_log_pretty_path(self.browser_profile.user_data_dir)}')# self._fork_locked_user_data_dir()break@@ -483,7 +485,7 @@ class BrowserSession(BaseModel):# - window focus/blur events# - pointermove events- # This multi-method approach provides more reliable detection across browsers.+ # This annoying multi-method approach is needed for more reliable detection across browsers because playwright provides no API for this.# TODO: pester the playwright team to add a new event that fires when a headful tab is focused.# OR implement a browser-use chrome extension that acts as a bridge to the chrome.tabs API.@@ -529,9 +531,9 @@ class BrowserSession(BaseModel):agent_tab_idx = self.browser_context.pages.index(self.agent_current_page)if old_url != new_url:logger.info(- f'👁️ Foregound tab changed by human from [{old_tab_idx}]{truncate_url(old_url)} '- f'➡️ [{new_tab_idx}]{truncate_url(new_url)} '- f'(agent will stay on [{agent_tab_idx}]{truncate_url(agent_url)})'+ f'👁️ Foregound tab changed by human from [{old_tab_idx}]{_log_pretty_url(old_url)} '+ f'➡️ [{new_tab_idx}]{_log_pretty_url(new_url)} '+ f'(agent will stay on [{agent_tab_idx}]{_log_pretty_url(agent_url)})')await self.browser_context.expose_binding('_BrowserUseonTabVisibilityChange', _BrowserUseonTabVisibilityChange)@@ -550,7 +552,7 @@ class BrowserSession(BaseModel):console.log('BrowserUse Foreground tab change event fired', document.location.href);});- // --- Method 3: pointermove events (may be fired by agent if we implement AI hover movements) ---+ // --- Method 3: pointermove events (may be fired by agent if we implement AI hover movements, also very noisy) ---// Use a throttled handler to avoid excessive calls// let lastMove = 0;// window.addEventListener('pointermove', async () => {@@ -564,12 +566,16 @@ class BrowserSession(BaseModel):"""await self.browser_context.add_init_script(update_tab_focus_script)- # set the user agent to the one we want# Set up visibility listeners for all existing tabsfor page in self.browser_context.pages:- if not page.url.startswith('chrome-extension://') and not page.url.startswith('chrome://') and not page.is_closed():- await page.evaluate(update_tab_focus_script)+ try:# logger.debug(f'👁️ Added visibility listener to existing tab: {page.url}')+ await page.evaluate(update_tab_focus_script)+ except Exception as e:+ page_idx = self.browser_context.pages.index(page)+ logger.debug(+ f'⚠️ Failed to add visibility listener to existing tab, is it crashed or ignoring CDP commands?: [{page_idx}]{page.url}: {type(e).__name__}: {e}'+ )async def _setup_viewports(self) -> None:"""Resize any existing page viewports to match the configured size"""@@ -596,6 +602,7 @@ class BrowserSession(BaseModel):+ (f'locale={self.browser_profile.locale} ' if self.browser_profile.locale else '')+ (f'timezone_id={self.browser_profile.timezone_id} ' if self.browser_profile.timezone_id else '')+ (f'geolocation={self.browser_profile.geolocation} ' if self.browser_profile.geolocation else '')+ + (f'permissions={",".join(self.browser_profile.permissions or ["<none>"])} '))# if we have any viewport settings in the profile, make sure to apply them to the entire browser_context as defaults@@ -614,14 +621,16 @@ class BrowserSession(BaseModel):except Exception as e:logger.warning(f'⚠️ Failed to set playwright timeout settings '- f'calls={self.browser_profile.default_timeout} '- f'nav={self.browser_profile.default_navigation_timeout}: {type(e).__name__}: {e}'+ f'cdp_api={self.browser_profile.default_timeout} '+ f'navigation={self.browser_profile.default_navigation_timeout}: {type(e).__name__}: {e}')try:if self.browser_profile.extra_http_headers:await self.browser_context.set_extra_http_headers(self.browser_profile.extra_http_headers)except Exception as e:- logger.warning(f'⚠️ Failed to setup playwright extra_http_headers: {type(e).__name__}: {e}')+ logger.warning(+ f'⚠️ Failed to setup playwright extra_http_headers: {type(e).__name__}: {e}'+ ) # dont print the secret header contents in the logs!try:if self.browser_profile.geolocation:@@ -630,7 +639,7 @@ class BrowserSession(BaseModel):logger.warning(f'⚠️ Failed to update browser geolocation {self.browser_profile.geolocation}: {type(e).__name__}: {e}')# if self.storage_state:- # TODO: implement applying self.stroage_state to an existing browser_context+ # TODO: implement applying self.stroage_state to an existing browser_context, currently only works on browser.new_context() I think# await self.browser_context.set_storage_state(self.storage_state)for page in self.browser_context.pages:@@ -1117,11 +1126,11 @@ class BrowserSession(BaseModel):tab_idx = self.tabs.index(page)if bytes_used is not None:logger.debug(- f'➡️ Page navigation [{tab_idx}]{truncate_url(page.url, 40)} used {bytes_used / 1024:.1f} KB in {elapsed:.2f}s, waiting +{remaining:.2f}s for all frames to finish'+ f'➡️ Page navigation [{tab_idx}]{_log_pretty_url(page.url, 40)} used {bytes_used / 1024:.1f} KB in {elapsed:.2f}s, waiting +{remaining:.2f}s for all frames to finish')else:logger.debug(- f'➡️ Page navigation [{tab_idx}]{truncate_url(page.url, 40)} took {elapsed:.2f}s, waiting +{remaining:.2f}s for all frames to finish'+ f'➡️ Page navigation [{tab_idx}]{_log_pretty_url(page.url, 40)} took {elapsed:.2f}s, waiting +{remaining:.2f}s for all frames to finish')# Sleep remaining time if needed@@ -1153,7 +1162,7 @@ class BrowserSession(BaseModel):if '*' in allowed_domain:parsed_url = urlparse(url)domain = parsed_url.hostname.lower() if parsed_url.hostname else ''- _show_glob_warning(domain, allowed_domain)+ _log_glob_warning(domain, allowed_domain)return Trueexcept AssertionError:# This would only happen if about:blank is passed to match_url_with_domain_pattern,diff --git a/examples/browser/stealth.py b/examples/browser/stealth.pyindex e5d23e0b6..1d48fbe63 100644--- a/examples/browser/stealth.py+++ b/examples/browser/stealth.py@@ -73,13 +73,15 @@ async def main():if Path('/Applications/Brave Browser.app/Contents/MacOS/Brave Browser').is_file():print('\n\nBRAVE + PATCHRIGHT STEALTH BROWSER:')+ patchright = await async_patchright().start()brave_patchright_browser_session = BrowserSession(executable_path='/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',- playwright=await async_patchright().start(),+ playwright=patchright,headless=False,disable_security=False,user_data_dir=None,deterministic_rendering=False,+ **patchright.devices['iPhone 13'], # emulate other devices: https://playwright.dev/python/docs/emulation)await brave_patchright_browser_session.start()await brave_patchright_browser_session.create_new_tab('https://abrahamjuliot.github.io/creepjs/')