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
finish connecting feature flags
Nick Sweeting31fb754473d9
diff --git a/browser_use/agent/message_manager/service.py b/browser_use/agent/message_manager/service.pyindex ac34d53fe..f20dd91e9 100644--- a/browser_use/agent/message_manager/service.py+++ b/browser_use/agent/message_manager/service.py@@ -107,6 +107,7 @@ class MessageManager:max_history_items: int | None = None,vision_detail_level: Literal['auto', 'low', 'high'] = 'auto',include_tool_call_examples: bool = False,+ include_recent_events: bool = False,):self.task = taskself.state = state@@ -117,6 +118,7 @@ class MessageManager:self.max_history_items = max_history_itemsself.vision_detail_level = vision_detail_levelself.include_tool_call_examples = include_tool_call_examples+ self.include_recent_events = include_recent_eventsassert max_history_items is None or max_history_items > 5, 'max_history_items must be None or greater than 5'@@ -286,6 +288,7 @@ class MessageManager:available_file_paths=available_file_paths,screenshots=screenshots,vision_detail_level=self.vision_detail_level,+ include_recent_events=self.include_recent_events,).get_user_message(use_vision)# Set the state message with caching enableddiff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.pyindex 374dcd7c2..946bab866 100644--- a/browser_use/agent/prompts.py+++ b/browser_use/agent/prompts.py@@ -178,11 +178,16 @@ class AgentMessagePrompt:if self.browser_state.is_pdf_viewer:pdf_message = 'PDF viewer cannot be rendered. In this page, DO NOT use the extract_structured_data action as PDF content cannot be rendered. Use the read_file action on the downloaded PDF in available_file_paths to read the full content.\n\n'+ # Add recent events if available and requested+ recent_events_text = ''+ if self.include_recent_events and self.browser_state.recent_events:+ recent_events_text = f'Recent browser events: {self.browser_state.recent_events}\n'+browser_state = f"""{current_tab_text}Available tabs:{tabs_text}{page_info_text}-{pdf_message}Interactive elements from top layer of the current page inside the viewport{truncated_text}:+{recent_events_text}{pdf_message}Interactive elements from top layer of the current page inside the viewport{truncated_text}:{elements_text}"""return browser_statediff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 56e85a930..cf2eacc94 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -350,6 +350,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):max_history_items=self.settings.max_history_items,vision_detail_level=self.settings.vision_detail_level,include_tool_call_examples=self.settings.include_tool_call_examples,+ include_recent_events=self.include_recent_events,)if isinstance(browser, BrowserSession):@@ -705,6 +706,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):cache_clickable_elements_hashes=True,include_screenshot=True,cached=use_cache,+ include_recent_events=self.include_recent_events,)if browser_state_summary.screenshot:self.logger.debug(f'๐ธ Got browser state WITH screenshot, length: {len(browser_state_summary.screenshot)}')diff --git a/browser_use/browser/aboutblank_watchdog.py b/browser_use/browser/aboutblank_watchdog.pyindex fe4a30005..c76a97a62 100644--- a/browser_use/browser/aboutblank_watchdog.py+++ b/browser_use/browser/aboutblank_watchdog.py@@ -91,42 +91,15 @@ class AboutBlankWatchdog(BaseWatchdog):# For quick checks, just get page targets without titles to reduce noisepage_targets = await self.browser_session._cdp_get_all_pages()- # Only get full tabs info if we actually need to check titles- tabs_info = None-- # For AboutBlankWatchdog, we only care about tab count not titles- # Only get full tabs info if we actually have animation tabs to check+ # If no tabs exist at all, create one to keep browser aliveif len(page_targets) == 0:- tabs_info = []- else:- # Skip the expensive get_tabs_info call - we just need tab count- tabs_info = None-- # We don't need to track animation tabs anymore since we're only ensuring- # that there's at least one tab open- animation_tabs = []-- # logger.debug(f'[AboutBlankWatchdog] Found {len(animation_tabs)} animation tabs out of {len(page_targets)} total tabs')-- # If no animation tabs exist, create one only if there are no tabs at all- if not animation_tabs:- if len(page_targets) == 0:- # Only create a new tab if there are no tabs at all- # logger.info('[AboutBlankWatchdog] No tabs exist, creating new about:blank DVD screensaver tab')- navigate_event = self.event_bus.dispatch(NavigateToUrlEvent(url='about:blank', new_tab=True))- await navigate_event- # Show DVD screensaver on the new tab- await self._show_dvd_screensaver_on_about_blank_tabs()- else:- # There are other tabs - don't create new about:blank tabs during scripting- # logger.debug(- # f'[AboutBlankWatchdog] {len(tabs_info)} tabs exist, not creating animation tab to avoid interfering with scripting'- # )- pass- # If more than one animation tab exists, just log it - don't close anything- elif len(animation_tabs) > 1:- # logger.debug(f'[AboutBlankWatchdog] Found {len(animation_tabs)} animation tabs, allowing them to exist')- pass+ # Only create a new tab if there are no tabs at all+ self.logger.info('[AboutBlankWatchdog] No tabs exist, creating new about:blank DVD screensaver tab')+ navigate_event = self.event_bus.dispatch(NavigateToUrlEvent(url='about:blank', new_tab=True))+ await navigate_event+ # Show DVD screensaver on the new tab+ await self._show_dvd_screensaver_on_about_blank_tabs()+ # Otherwise there are tabs, don't create new ones to avoid interferingexcept Exception as e:self.logger.error(f'[AboutBlankWatchdog] Error ensuring about:blank tab: {e}')diff --git a/browser_use/browser/dom_watchdog.py b/browser_use/browser/dom_watchdog.pyindex 6c74ca76a..33b3fb310 100644--- a/browser_use/browser/dom_watchdog.py+++ b/browser_use/browser/dom_watchdog.py@@ -108,6 +108,33 @@ class DOMWatchdog(BaseWatchdog):# Re-raise other errorsraise+ def _get_recent_events_csv(self, limit: int = 10) -> str | None:+ """Get the most recent event names from the event bus as CSV.++ Args:+ limit: Maximum number of recent events to include++ Returns:+ CSV string of recent event names or None if not available+ """+ try:+ # Get all events from history, sorted by creation time (most recent first)+ all_events = sorted(+ self.browser_session.event_bus.event_history.values(),+ key=lambda e: e.event_created_at.timestamp(),+ reverse=True+ )++ # Take the most recent events and get their names+ recent_event_names = [event.event_type for event in all_events[:limit]]++ if recent_event_names:+ return ', '.join(recent_event_names)+ except Exception as e:+ self.logger.debug(f'Failed to get recent events: {e}')++ return None+async def on_BrowserStateRequestEvent(self, event: BrowserStateRequestEvent) -> 'BrowserStateSummary':"""Handle browser state request by coordinating DOM building and screenshot capture.@@ -173,6 +200,7 @@ class DOMWatchdog(BaseWatchdog):pixels_below=0,browser_errors=[],is_pdf_viewer=False,+ recent_events=self._get_recent_events_csv() if event.include_recent_events else None,)# Normal path: Build DOM tree if requested@@ -280,6 +308,7 @@ class DOMWatchdog(BaseWatchdog):pixels_below=0,browser_errors=[],is_pdf_viewer=is_pdf_viewer,+ recent_events=self._get_recent_events_csv() if event.include_recent_events else None,)# Cache the state@@ -314,6 +343,7 @@ class DOMWatchdog(BaseWatchdog):pixels_below=0,browser_errors=[str(e)],is_pdf_viewer=False,+ recent_events=None,)async def _build_dom_tree(self, previous_state: SerializedDOMState | None = None) -> SerializedDOMState:diff --git a/browser_use/browser/events.py b/browser_use/browser/events.pyindex f162810d1..77142f86b 100644--- a/browser_use/browser/events.py+++ b/browser_use/browser/events.py@@ -109,6 +109,7 @@ class BrowserStateRequestEvent(BaseEvent[BrowserStateSummary]):include_dom: bool = Trueinclude_screenshot: bool = Falsecache_clickable_elements_hashes: bool = True+ include_recent_events: bool = False# class WaitForConditionEvent(BaseEvent):diff --git a/browser_use/browser/security_watchdog.py b/browser_use/browser/security_watchdog.pyindex cac780f61..484f66d32 100644--- a/browser_use/browser/security_watchdog.py+++ b/browser_use/browser/security_watchdog.py@@ -17,6 +17,9 @@ from browser_use.browser.watchdog_base import BaseWatchdogif TYPE_CHECKING:pass+# Track if we've shown the glob warning+_GLOB_WARNING_SHOWN = False+class SecurityWatchdog(BaseWatchdog):"""Monitors and enforces security policies for URL access."""@@ -95,6 +98,16 @@ class SecurityWatchdog(BaseWatchdog):except Exception as e:self.logger.error(f'โ๏ธ Failed to close new tab with non-allowed URL: {str(e)}')+ def _log_glob_warning(self) -> None:+ """Log a warning about glob patterns in allowed_domains."""+ global _GLOB_WARNING_SHOWN+ if not _GLOB_WARNING_SHOWN:+ _GLOB_WARNING_SHOWN = True+ self.logger.warning(+ 'โ ๏ธ Using glob patterns in allowed_domains. '+ 'Note: Patterns like "*.example.com" will match both subdomains AND the main domain.'+ )+def _is_url_allowed(self, url: str) -> bool:"""Check if a URL is allowed based on the allowed_domains configuration.@@ -133,6 +146,7 @@ class SecurityWatchdog(BaseWatchdog):for pattern in self.browser_session.browser_profile.allowed_domains:# Handle glob patternsif '*' in pattern:+ self._log_glob_warning()import fnmatch# Check if pattern matches the hostdiff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 603528c58..a214de9d2 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -49,24 +49,10 @@ from browser_use.utils import _log_pretty_url, is_new_tab_pageDEFAULT_BROWSER_PROFILE = BrowserProfile()-_GLOB_WARNING_SHOWN = False # used inside _is_url_allowed to avoid spamming the logs with the same warning multiple times-MAX_SCREENSHOT_HEIGHT = 2000MAX_SCREENSHOT_WIDTH = 1920-def _log_glob_warning(domain: str, glob: str, logger: logging.Logger):- global _GLOB_WARNING_SHOWN- if not _GLOB_WARNING_SHOWN:- logger.warning(- # glob patterns are very easy to mess up and match too many domains by accident- # e.g. if you only need to access gmail, don't use *.google.com because an attacker could convince the agent to visit a malicious doc- # on docs.google.com/s/some/evil/doc to set up a prompt injection attack- f"โ ๏ธ Allowing agent to visit {domain} based on allowed_domains=['{glob}', ...]. Set allowed_domains=['{domain}', ...] explicitly to avoid matching too many domains!"- )- _GLOB_WARNING_SHOWN = True--class CDPSession(BaseModel):"""Info about a single CDP session bound to a specific target.@@ -299,9 +285,27 @@ class BrowserSession(BaseModel):await self.event_bus.dispatch(BrowserStartEvent())async def kill(self) -> None:- """Kill the browser session."""+ """Kill the browser session and reset all state."""+ # Dispatch stop event to kill the browserawait self.event_bus.dispatch(BrowserStopEvent(force=True))+ # Stop the event bus+ await self.event_bus.stop(clear=True, timeout=5)+ # Reset all state+ await self.reset()+ # Create fresh event bus+ self.event_bus = EventBus()++ async def stop(self) -> None:+ """Stop the browser session without killing the browser process.++ This clears event buses and cached state but keeps the browser alive.+ Useful when you want to clean up resources but plan to reconnect later.+ """+ # Stop the event bus without dispatching BrowserStopEventawait self.event_bus.stop(clear=True, timeout=5)+ # Reset all state+ await self.reset()+ # Create fresh event busself.event_bus = EventBus()async def on_BrowserStartEvent(self, event: BrowserStartEvent) -> dict[str, str]:@@ -543,13 +547,13 @@ class BrowserSession(BaseModel):assert self._cdp_client_root is not None, 'CDP client not initialized - browser may not be connected yet'return self._cdp_client_root- async def get_or_create_cdp_session(self, target_id: str | None = None, focus: bool = True, new_socket: bool = False) -> CDPSession:+ async def get_or_create_cdp_session(self, target_id: str | None = None, focus: bool = True, new_socket: bool | None = None) -> CDPSession:"""Get or create a CDP session for a target.Args:target_id: Target ID to get session for. If None, uses current agent focus.focus: If True, switches agent focus to this target. If False, just returns session without changing focus.- new_socket: If True, create a dedicated WebSocket connection for this target.+ new_socket: If True, create a dedicated WebSocket connection. If None (default), creates new socket for new targets only.Returns:CDPSession for the specified target.@@ -580,12 +584,14 @@ class BrowserSession(BaseModel):return self.agent_focus# Create new session for this target- self.logger.debug(f'[get_or_create_cdp_session] Creating new CDP session for target {target_id} (new_socket={new_socket})')+ # Default to True for new sessions (each new target gets its own WebSocket)+ should_use_new_socket = True if new_socket is None else new_socket+ self.logger.debug(f'[get_or_create_cdp_session] Creating new CDP session for target {target_id} (new_socket={should_use_new_socket})')session = await CDPSession.for_target(self._cdp_client_root,target_id,- new_socket=new_socket,- cdp_url=self.cdp_url if new_socket else None+ new_socket=should_use_new_socket,+ cdp_url=self.cdp_url if should_use_new_socket else None)self._cdp_session_pool[target_id] = session@@ -613,7 +619,7 @@ class BrowserSession(BaseModel):# ========== Helper Methods ==========async def get_browser_state_summary(- self, cache_clickable_elements_hashes: bool = True, include_screenshot: bool = True, cached: bool = False+ self, cache_clickable_elements_hashes: bool = True, include_screenshot: bool = True, cached: bool = False, include_recent_events: bool = False) -> BrowserStateSummary:if cached and self._cached_browser_state_summary is not None and self._cached_browser_state_summary.dom_state:# Don't use cached state if it has 0 interactive elements@@ -636,6 +642,7 @@ class BrowserSession(BaseModel):include_dom=True,include_screenshot=include_screenshot,cache_clickable_elements_hashes=cache_clickable_elements_hashes,+ include_recent_events=include_recent_events,)),)@@ -809,7 +816,7 @@ class BrowserSession(BaseModel):target_id = target['targetId']self.logger.info(f'๐ Redirecting {target_url} to about:blank for target {target_id}')try:- # Create a CDP session for this target+ # Create a temporary CDP session for this targettemp_session = await CDPSession.for_target(self._cdp_client_root, target_id)# Navigate to about:blankawait temp_session.cdp_client.send.Page.navigate(@@ -838,7 +845,8 @@ class BrowserSession(BaseModel):self.logger.info(f'๐ Using existing page with target ID: {target_id}')# Store the current page target ID and add to pool- self.agent_focus = await CDPSession.for_target(self._cdp_client_root, target_id)+ # For the initial connection, we'll use the shared root WebSocket+ self.agent_focus = await CDPSession.for_target(self._cdp_client_root, target_id, new_socket=False)self._cdp_session_pool[target_id] = self.agent_focus# Verify the session is working@@ -1236,7 +1244,7 @@ class BrowserSession(BaseModel):assert self._cdp_client_root is not None, 'CDP client not initialized - browser may not be connected yet'assert self.agent_focus is not None, 'CDP session not initialized - browser may not be connected yet'- self.agent_focus = await CDPSession.for_target(self._cdp_client_root, target_id or self.agent_focus.target_id)+ self.agent_focus = await self.get_or_create_cdp_session(target_id or self.agent_focus.target_id, focus=True)# Use helper to navigate on the targetawait self.agent_focus.cdp_client.send.Page.navigate(params={'url': url}, session_id=self.agent_focus.session_id)diff --git a/browser_use/cli.py b/browser_use/cli.pyindex b4c0fe2dd..baebd742f 100644--- a/browser_use/cli.py+++ b/browser_use/cli.py@@ -8,6 +8,9 @@ import timefrom pathlib import Pathfrom typing import Any+# Enable CDP debug logging BEFORE importing anything that uses cdp_use+os.environ['BROWSER_USE_CDP_DEBUG'] = 'true'+from dotenv import load_dotenvfrom browser_use.llm.anthropic.chat import ChatAnthropic@@ -430,7 +433,7 @@ class BrowserUseApp(App):log_handler.setLevel('RESULT')log_handler.setFormatter(BrowserUseFormatter('%(message)s'))else:- log_handler.setFormatter(BrowserUseFormatter('%(levelname)-8s [%(name)s] %(message)s'))+ log_handler.setFormatter(BrowserUseFormatter('%(message)s'))# Configure root logger - Replace ALL handlers, not just stdout handlersroot = logging.getLogger()@@ -447,11 +450,18 @@ class BrowserUseApp(App):else:root.setLevel(logging.INFO)- # Configure browser_use logger+ # Configure browser_use logger and all its sub-loggersbrowser_use_logger = logging.getLogger('browser_use')browser_use_logger.propagate = False # Don't propagate to root loggerbrowser_use_logger.handlers = [log_handler] # Replace any existing handlersbrowser_use_logger.setLevel(root.level)++ # Also ensure agent loggers go to the main output+ for logger_name in ['browser_use.Agent', 'browser_use.controller']:+ agent_logger = logging.getLogger(logger_name)+ agent_logger.propagate = False+ agent_logger.handlers = [log_handler]+ agent_logger.setLevel(root.level)# Silence third-party loggersfor logger_name in [@@ -648,9 +658,17 @@ class BrowserUseApp(App):if not self.browser_session or not self.browser_session.event_bus:return- # Check if we already have a handler registered+ # Clean up any existing handler before registering a new oneif self._event_bus_handler_id is not None:- return+ try:+ # Remove handler from the event bus's internal handlers dict+ if hasattr(self.browser_session.event_bus, 'handlers'):+ for event_type, handlers in list(self.browser_session.event_bus.handlers.items()):+ if self._event_bus_handler_id in handlers:+ del handlers[self._event_bus_handler_id]+ except Exception:+ pass # Handler might not exist anymore+ self._event_bus_handler_id = Nonetry:# Get the events log widget@@ -688,10 +706,8 @@ class BrowserUseApp(App):def setup_cdp_logger(self) -> None:"""Setup CDP message logger."""- # Enable CDP debug logging- os.environ['BROWSER_USE_CDP_DEBUG'] = 'true'-- # Need to re-import cdp_use.client to apply the new setting+ # CDP debug logging is enabled at module import time via environment variable+ # Just configure the loggers to use our handlerimport cdp_use.clientcdp_use.client.logger.setLevel(logging.DEBUG)@@ -766,9 +782,8 @@ class BrowserUseApp(App):# Update our browser_session reference to point to the agent'sif hasattr(self.agent, 'browser_session'):self.browser_session = self.agent.browser_session- # Set up event bus listener if not already set up- if self._event_bus_handler_id is None:- self.setup_event_bus_listener()+ # Set up event bus listener (will clean up any old handler first)+ self.setup_event_bus_listener()else:self.agent.add_new_task(task)diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex c6044267b..cbd34a1a0 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -444,13 +444,14 @@ Explain the content of the page and that the requested information is not availaraise RuntimeError(str(e))@self.registry.action(- 'Scroll the page by specified number of pages (set down=True to scroll down, down=False to scroll up, num_pages=number of pages to scroll like 0.5 for half page, 1.0 for one page, etc.). Optional index parameter to scroll within a specific element or its scroll container (works well for dropdowns and custom UI components).',+ 'Scroll the page by specified number of pages (set down=True to scroll down, down=False to scroll up, num_pages=number of pages to scroll like 0.5 for half page, 1.0 for one page, etc.). Optional index parameter to scroll within a specific element or its scroll container (works well for dropdowns and custom UI components). Use index=0 or omit index to scroll the entire page.',param_model=ScrollAction,)async def scroll(params: ScrollAction, browser_session: BrowserSession):# Look up the node from the selector map if index is provided+ # Special case: index 0 means scroll the whole page (root/body element)node = None- if params.index is not None:+ if params.index is not None and params.index != 0:try:node = await browser_session.get_element_by_index(params.index)if node is None:@@ -473,7 +474,8 @@ Explain the content of the page and that the requested information is not availaraise ValueError(f'Failed to scroll: {e}') from edirection = 'down' if params.down else 'up'- target = f'element {params.index}' if params.index is not None else 'the page'+ # If index is 0 or None, we're scrolling the page+ target = 'the page' if params.index is None or params.index == 0 else f'element {params.index}'if params.num_pages == 1.0:long_term_memory = f'Scrolled {direction} {target} by one page'@@ -524,66 +526,66 @@ Explain the content of the page and that the requested information is not availalong_term_memory=f"Tried scrolling to text '{text}' but it was not found",)- # # File System Actions- # @self.registry.action(- # 'Write or append content to file_name in file system. Allowed extensions are .md, .txt, .json, .csv, .pdf. For .pdf files, write the content in markdown format and it will automatically be converted to a properly formatted PDF document.'- # )- # async def write_file(- # file_name: str,- # content: str,- # file_system: FileSystem,- # append: bool = False,- # trailing_newline: bool = True,- # leading_newline: bool = False,- # ):- # if trailing_newline:- # content += '\n'- # if leading_newline:- # content = '\n' + content- # if append:- # result = await file_system.append_file(file_name, content)- # else:- # result = await file_system.write_file(file_name, content)- # logger.info(f'๐พ {result}')- # return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)+ # File System Actions+ @self.registry.action(+ 'Write or append content to file_name in file system. Allowed extensions are .md, .txt, .json, .csv, .pdf. For .pdf files, write the content in markdown format and it will automatically be converted to a properly formatted PDF document.'+ )+ async def write_file(+ file_name: str,+ content: str,+ file_system: FileSystem,+ append: bool = False,+ trailing_newline: bool = True,+ leading_newline: bool = False,+ ):+ if trailing_newline:+ content += '\n'+ if leading_newline:+ content = '\n' + content+ if append:+ result = await file_system.append_file(file_name, content)+ else:+ result = await file_system.write_file(file_name, content)+ logger.info(f'๐พ {result}')+ return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)- # @self.registry.action(- # 'Replace old_str with new_str in file_name. old_str must exactly match the string to replace in original text. Recommended tool to mark completed items in todo.md or change specific contents in a file.'- # )- # async def replace_file_str(file_name: str, old_str: str, new_str: str, file_system: FileSystem):- # result = await file_system.replace_file_str(file_name, old_str, new_str)- # logger.info(f'๐พ {result}')- # return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)-- # @self.registry.action('Read file_name from file system')- # async def read_file(file_name: str, available_file_paths: list[str], file_system: FileSystem):- # if available_file_paths and file_name in available_file_paths:- # result = await file_system.read_file(file_name, external_file=True)- # else:- # result = await file_system.read_file(file_name)-- # MAX_MEMORY_SIZE = 1000- # if len(result) > MAX_MEMORY_SIZE:- # lines = result.splitlines()- # display = ''- # lines_count = 0- # for line in lines:- # if len(display) + len(line) < MAX_MEMORY_SIZE:- # display += line + '\n'- # lines_count += 1- # else:- # break- # remaining_lines = len(lines) - lines_count- # memory = f'{display}{remaining_lines} more lines...' if remaining_lines > 0 else display- # else:- # memory = result- # logger.info(f'๐พ {memory}')- # return ActionResult(- # extracted_content=result,- # include_in_memory=True,- # long_term_memory=memory,- # include_extracted_content_only_once=True,- # )+ @self.registry.action(+ 'Replace old_str with new_str in file_name. old_str must exactly match the string to replace in original text. Recommended tool to mark completed items in todo.md or change specific contents in a file.'+ )+ async def replace_file_str(file_name: str, old_str: str, new_str: str, file_system: FileSystem):+ result = await file_system.replace_file_str(file_name, old_str, new_str)+ logger.info(f'๐พ {result}')+ return ActionResult(extracted_content=result, include_in_memory=True, long_term_memory=result)++ @self.registry.action('Read file_name from file system')+ async def read_file(file_name: str, available_file_paths: list[str], file_system: FileSystem):+ if available_file_paths and file_name in available_file_paths:+ result = await file_system.read_file(file_name, external_file=True)+ else:+ result = await file_system.read_file(file_name)++ MAX_MEMORY_SIZE = 1000+ if len(result) > MAX_MEMORY_SIZE:+ lines = result.splitlines()+ display = ''+ lines_count = 0+ for line in lines:+ if len(display) + len(line) < MAX_MEMORY_SIZE:+ display += line + '\n'+ lines_count += 1+ else:+ break+ remaining_lines = len(lines) - lines_count+ memory = f'{display}{remaining_lines} more lines...' if remaining_lines > 0 else display+ else:+ memory = result+ logger.info(f'๐พ {memory}')+ return ActionResult(+ extracted_content=result,+ include_in_memory=True,+ long_term_memory=memory,+ include_extracted_content_only_once=True,+ )# TODO: Refactor to use events instead of direct page/dom access# @self.registry.action(diff --git a/tests/ci/test_ClickElementEvent.py b/tests/ci/test_ClickElementEvent.pyindex 0e1034929..643fea738 100644--- a/tests/ci/test_ClickElementEvent.py+++ b/tests/ci/test_ClickElementEvent.py@@ -191,7 +191,7 @@ class TestClickElementEvent:# Note: The click action doesn't include button text in the result, only the index# Verify the click actually had an effect on the page using CDP- cdp_session = await browser_session.get_or_create_cdp_session(browser_session.agent_focus)+ cdp_session = await browser_session.get_or_create_cdp_session()result_js = await browser_session.cdp_client.send.Runtime.evaluate(params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},session_id=cdp_session.session_id,@@ -241,7 +241,7 @@ class TestClickElementEvent:# Find the custom element indexcustom_element_index = None- for index, element in state.selector_map.items():+ for index, element in state.dom_state.selector_map.items():if element.tag_name == 'custom-element':custom_element_index = indexbreak@@ -333,7 +333,7 @@ class TestClickElementEvent:# Find the link element in the selector maplink_index = None- for index, element in state.selector_map.items():+ for index, element in state.dom_state.selector_map.items():if hasattr(element, 'tag_name') and element.tag_name == 'a':link_index = indexbreak@@ -397,7 +397,7 @@ class TestClickElementEvent:# Get browser state and find link elementsstate = await browser_session.get_browser_state_with_recovery()link_indices = []- for index, element in state.selector_map.items():+ for index, element in state.dom_state.selector_map.items():if hasattr(element, 'tag_name') and element.tag_name == 'a':link_indices.append(index)@@ -426,3 +426,381 @@ class TestClickElementEvent:# Should have one more tabassert len(browser_session.tabs) == initial_tab_count + 1++ async def test_inline_element_mostly_offscreen(self, controller, browser_session, base_url, http_server):+ """Test clicking an inline element that's mostly outside the viewport."""+ # Add route for test page with inline element extending beyond viewport+ http_server.expect_request('/inline_offscreen').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Inline Offscreen Test</title>+ <style>+ body { margin: 0; padding: 20px; }+ .container { position: relative; width: 200%; }+ .inline-link {+ display: inline;+ position: absolute;+ left: -100px;+ width: 500px;+ padding: 10px;+ background: #007bff;+ color: white;+ cursor: pointer;+ }+ #result { margin-top: 100px; }+ </style>+ </head>+ <body>+ <div class="container">+ <span class="inline-link" onclick="document.getElementById('result').textContent = 'Inline clicked'">+ This is a very long inline element that extends way beyond the viewport edge+ </span>+ </div>+ <div id="result">Not clicked</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/inline_offscreen', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the inline element+ inline_index = None+ for idx, element in selector_map.items():+ if 'inline-link' in str(element.attributes.get('class', '')):+ inline_index = idx+ break++ assert inline_index is not None, 'Could not find inline element'++ # Click the element - should click the visible portion+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=inline_index)),+ browser_session+ )++ assert result.error is None, f'Click failed: {result.error}'++ # Verify click worked using CDP+ cdp_session = await browser_session.get_or_create_cdp_session()+ result_js = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},+ session_id=cdp_session.session_id+ )+ assert result_js.get('result', {}).get('value') == 'Inline clicked'++ async def test_block_inside_inline_multiline(self, controller, browser_session, base_url, http_server):+ """Test clicking a block element inside an inline element that spans multiple lines."""+ # Add route for complex nested layout+ http_server.expect_request('/block_in_inline').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Block in Inline Test</title>+ <style>+ body { margin: 20px; width: 300px; }+ .inline-wrapper {+ display: inline;+ background: #f0f0f0;+ line-height: 1.5;+ }+ .block-inside {+ display: block;+ margin: 10px 0;+ padding: 10px;+ background: #007bff;+ color: white;+ cursor: pointer;+ }+ #result { margin-top: 50px; }+ </style>+ </head>+ <body>+ <span class="inline-wrapper">+ This is some text that wraps around and contains+ <div class="block-inside" onclick="document.getElementById('result').textContent = 'Block clicked'">+ Click this block element+ </div>+ and continues after the block element with more text that will wrap to multiple lines+ </span>+ <div id="result">Not clicked</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/block_in_inline', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the block element inside inline+ block_index = None+ for idx, element in selector_map.items():+ if 'block-inside' in str(element.attributes.get('class', '')):+ block_index = idx+ break++ assert block_index is not None, 'Could not find block element'++ # Click the block element+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=block_index)),+ browser_session+ )++ assert result.error is None, f'Click failed: {result.error}'++ # Verify click worked+ cdp_session = await browser_session.get_or_create_cdp_session()+ result_js = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},+ session_id=cdp_session.session_id+ )+ assert result_js.get('result', {}).get('value') == 'Block clicked'++ async def test_element_covered_by_overlay(self, controller, browser_session, base_url, http_server):+ """Test clicking an element that's mostly covered by another element."""+ # Add route for overlapping elements+ http_server.expect_request('/covered_element').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Covered Element Test</title>+ <style>+ body { margin: 20px; position: relative; }+ .target {+ position: absolute;+ top: 50px;+ left: 50px;+ width: 200px;+ height: 100px;+ padding: 20px;+ background: #28a745;+ color: white;+ cursor: pointer;+ z-index: 1;+ }+ .overlay {+ position: absolute;+ top: 60px;+ left: 60px;+ width: 180px;+ height: 80px;+ background: rgba(255, 0, 0, 0.7);+ z-index: 2;+ pointer-events: none; /* Allow clicks through */+ }+ #result { margin-top: 200px; }+ </style>+ </head>+ <body>+ <div class="target" onclick="document.getElementById('result').textContent = 'Target clicked'">+ Click me (partially covered)+ </div>+ <div class="overlay">Overlaying element</div>+ <div id="result">Not clicked</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/covered_element', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the target element+ target_index = None+ for idx, element in selector_map.items():+ if 'target' in str(element.attributes.get('class', '')):+ target_index = idx+ break++ assert target_index is not None, 'Could not find target element'++ # Click should still work on the visible portion+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=target_index)),+ browser_session+ )++ assert result.error is None, f'Click failed: {result.error}'++ # Verify click worked+ cdp_session = await browser_session.get_or_create_cdp_session()+ result_js = await browser_session.cdp_client.send.Runtime.evaluate(+ params={'expression': "document.getElementById('result').textContent", 'returnByValue': True},+ session_id=cdp_session.session_id+ )+ assert result_js.get('result', {}).get('value') == 'Target clicked'++ async def test_file_input_click_prevention(self, controller, browser_session, base_url, http_server):+ """Test that clicking a file input element raises an exception."""+ # Add route with file input+ http_server.expect_request('/file_input').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>File Input Test</title>+ </head>+ <body>+ <h1>File Upload Test</h1>+ <input type="file" id="fileInput" />+ <div id="result">No file selected</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/file_input', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the file input+ file_input_index = None+ for idx, element in selector_map.items():+ if element.tag_name and element.tag_name.lower() == 'input':+ if element.attributes and element.attributes.get('type') == 'file':+ file_input_index = idx+ break++ assert file_input_index is not None, 'Could not find file input element'++ # Attempt to click should raise an exception+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=file_input_index)),+ browser_session+ )++ # Should have an error about file inputs+ assert result.error is not None, 'Expected error for file input click'+ assert 'file input' in result.error.lower() or 'file upload' in result.error.lower(), \+ f'Error message should mention file input, got: {result.error}'++ async def test_select_dropdown_click_prevention(self, controller, browser_session, base_url, http_server):+ """Test that clicking a select dropdown element raises an exception."""+ # Add route with select dropdown+ http_server.expect_request('/select_dropdown').respond_with_data(+ """+ <!DOCTYPE html>+ <html>+ <head>+ <title>Select Dropdown Test</title>+ </head>+ <body>+ <h1>Select Test</h1>+ <select id="testSelect">+ <option value="">Choose one</option>+ <option value="opt1">Option 1</option>+ <option value="opt2">Option 2</option>+ </select>+ <div id="result">Nothing selected</div>+ </body>+ </html>+ """,+ content_type='text/html',+ )++ # Navigate to the page+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/select_dropdown', new_tab=False)}++ from browser_use.agent.views import ActionModel++ class GoToUrlActionModel(ActionModel):+ go_to_url: GoToUrlAction | None = None++ await controller.act(GoToUrlActionModel(**goto_action), browser_session)+ await asyncio.sleep(0.5)++ # Get the clickable elements+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)+ selector_map = await browser_session.get_selector_map()++ # Find the select element+ select_index = None+ for idx, element in selector_map.items():+ if element.tag_name and element.tag_name.lower() == 'select':+ select_index = idx+ break++ assert select_index is not None, 'Could not find select element'++ # Attempt to click should raise an exception+ class ClickActionModel(ActionModel):+ click_element_by_index: ClickElementAction | None = None++ result = await controller.act(+ ClickActionModel(click_element_by_index=ClickElementAction(index=select_index)),+ browser_session+ )++ # Should have an error about select elements+ assert result.error is not None, 'Expected error for select element click'+ assert 'select' in result.error.lower() and 'dropdown' in result.error.lower(), \+ f'Error message should mention select/dropdown, got: {result.error}'diff --git a/tests/ci/test_ScrollEvent.py b/tests/ci/test_ScrollEvent.pyindex 370c20c7d..fc9b52ed9 100644--- a/tests/ci/test_ScrollEvent.py+++ b/tests/ci/test_ScrollEvent.py@@ -165,89 +165,20 @@ class TestScrollActions:assert event_result is not Noneassert event_result.get('success') is True- async def test_scroll_with_element_node(self, browser_session, base_url, http_server):- """Test scrolling a specific element via node parameter."""+ async def test_scroll_without_hanging(self, browser_session):+ """Test that scrolling doesn't hang with cross_origin_iframes=False."""from browser_use.browser.events import ScrollEvent- # Add a page with a scrollable div element- http_server.expect_request('/scrollable-div').respond_with_data(- """- <!DOCTYPE html>- <html>- <head>- <title>Scrollable Div Test</title>- <style>- body { margin: 0; padding: 20px; }- #scrollable-container {- width: 400px;- height: 300px;- overflow-y: scroll;- border: 2px solid #333;- background: #f0f0f0;- }- .inner-content {- height: 1500px;- background: linear-gradient(to bottom, #f0f0f0, #333);- padding: 20px;- }- .marker {- padding: 20px;- background: #007bff;- color: white;- margin: 200px 0;- }- </style>- </head>- <body>- <h1>Page with Scrollable Div</h1>- <div id="scrollable-container">- <div class="inner-content">- <p>Start of scrollable content</p>- <div class="marker">Marker 1</div>- <div class="marker">Marker 2</div>- <div class="marker">Marker 3</div>- <p>End of scrollable content</p>- </div>- </div>- <p>Content outside scrollable div</p>- </body>- </html>- """,- content_type='text/html',- )-- # Navigate to the page with scrollable div- await browser_session._cdp_navigate(f'{base_url}/scrollable-div')- await asyncio.sleep(0.5)-- # Get browser state to find the scrollable element- state = await browser_session.get_browser_state_summary()-- # Find the scrollable container element in the selector map- scrollable_node = None- for index, node in state.selector_map.items():- # Look for the div with id="scrollable-container"- if (hasattr(node, 'attributes') and- node.attributes.get('id') == 'scrollable-container'):- scrollable_node = node- break-- assert scrollable_node is not None, "Could not find scrollable-container element"-- # Test scrolling the specific element down- event = browser_session.event_bus.dispatch(- ScrollEvent(direction='down', amount=300, node=scrollable_node)- )- result = await asyncio.wait_for(event, timeout=3.0)+ # Simple test on about:blank - should not hang+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=100))+ result = await asyncio.wait_for(event, timeout=2.0)event_result = await result.event_result()assert event_result is not Noneassert event_result.get('success') is True- # Test scrolling the specific element up- event = browser_session.event_bus.dispatch(- ScrollEvent(direction='up', amount=150, node=scrollable_node)- )- result = await asyncio.wait_for(event, timeout=3.0)+ # Test scroll up+ event = browser_session.event_bus.dispatch(ScrollEvent(direction='up', amount=50))+ result = await asyncio.wait_for(event, timeout=2.0)event_result = await result.event_result()assert event_result is not Noneassert event_result.get('success') is True\ No newline at end of filediff --git a/tests/ci/test_action_parameter_injection.py b/tests/ci/test_action_parameter_injection.pyindex 5abe6300c..890e5aace 100644--- a/tests/ci/test_action_parameter_injection.py+++ b/tests/ci/test_action_parameter_injection.py@@ -81,13 +81,13 @@ class TestBrowserContext:the allowed domains configuration."""# Scenario 1: allowed_domains is None, any URL should be allowed.- from browser_use.browser.navigation_watchdog import NavigationWatchdog+ from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBusconfig1 = BrowserProfile(allowed_domains=None, headless=True, user_data_dir=None)context1 = BrowserSession(browser_profile=config1)event_bus1 = EventBus()- watchdog1 = NavigationWatchdog(browser_session=context1, event_bus=event_bus1)+ watchdog1 = SecurityWatchdog(browser_session=context1, event_bus=event_bus1)assert watchdog1._is_url_allowed('http://anydomain.com') is Trueassert watchdog1._is_url_allowed('https://anotherdomain.org/path') is True@@ -97,7 +97,7 @@ class TestBrowserContext:config2 = BrowserProfile(allowed_domains=allowed, headless=True, user_data_dir=None)context2 = BrowserSession(browser_profile=config2)event_bus2 = EventBus()- watchdog2 = NavigationWatchdog(browser_session=context2, event_bus=event_bus2)+ watchdog2 = SecurityWatchdog(browser_session=context2, event_bus=event_bus2)# URL exactly matchingassert watchdog2._is_url_allowed('http://example.com') is Truediff --git a/tests/ci/test_browser_session_allowed_domains.py b/tests/ci/test_browser_session_allowed_domains.pyindex 1c6429227..bf8e7875c 100644--- a/tests/ci/test_browser_session_allowed_domains.py+++ b/tests/ci/test_browser_session_allowed_domains.py@@ -6,14 +6,14 @@ class TestUrlAllowlistSecurity:def test_authentication_bypass_prevention(self):"""Test that the URL allowlist cannot be bypassed using authentication credentials."""- from browser_use.browser.navigation_watchdog import NavigationWatchdog+ from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBus# Create a context config with a sample allowed domainbrowser_profile = BrowserProfile(allowed_domains=['example.com'], headless=True, user_data_dir=None)browser_session = BrowserSession(browser_profile=browser_profile)event_bus = EventBus()- watchdog = NavigationWatchdog(browser_session=browser_session, event_bus=event_bus)+ watchdog = SecurityWatchdog(browser_session=browser_session, event_bus=event_bus)# Security vulnerability test cases# These should all be detected as malicious despite containing "example.com"@@ -27,14 +27,14 @@ class TestUrlAllowlistSecurity:def test_glob_pattern_matching(self):"""Test that glob patterns in allowed_domains work correctly."""- from browser_use.browser.navigation_watchdog import NavigationWatchdog+ from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBus# Test *.example.com pattern (should match subdomains and main domain)browser_profile = BrowserProfile(allowed_domains=['*.example.com'], headless=True, user_data_dir=None)browser_session = BrowserSession(browser_profile=browser_profile)event_bus = EventBus()- watchdog = NavigationWatchdog(browser_session=browser_session, event_bus=event_bus)+ watchdog = SecurityWatchdog(browser_session=browser_session, event_bus=event_bus)# Should match subdomainsassert watchdog._is_url_allowed('https://sub.example.com') is True@@ -55,7 +55,7 @@ class TestUrlAllowlistSecurity:)browser_session = BrowserSession(browser_profile=browser_profile)event_bus = EventBus()- watchdog = NavigationWatchdog(browser_session=browser_session, event_bus=event_bus)+ watchdog = SecurityWatchdog(browser_session=browser_session, event_bus=event_bus)# Should match domains ending with google.comassert watchdog._is_url_allowed('https://google.com') is True@@ -90,14 +90,14 @@ class TestUrlAllowlistSecurity:def test_glob_pattern_edge_cases(self):"""Test edge cases for glob pattern matching to ensure proper behavior."""- from browser_use.browser.navigation_watchdog import NavigationWatchdog+ from browser_use.browser.security_watchdog import SecurityWatchdogfrom bubus import EventBus# Test with domains containing glob pattern in the middlebrowser_profile = BrowserProfile(allowed_domains=['*.google.com', 'https://wiki.org'], headless=True, user_data_dir=None)browser_session = BrowserSession(browser_profile=browser_profile)event_bus = EventBus()- watchdog = NavigationWatchdog(browser_session=browser_session, event_bus=event_bus)+ watchdog = SecurityWatchdog(browser_session=browser_session, event_bus=event_bus)# Verify that 'wiki*' pattern doesn't match domains that merely contain 'wiki' in the middleassert watchdog._is_url_allowed('https://notawiki.com') is False@@ -111,7 +111,7 @@ class TestUrlAllowlistSecurity:browser_profile = BrowserProfile(allowed_domains=['*.google.com', '*.google.co.uk'], headless=True, user_data_dir=None)browser_session = BrowserSession(browser_profile=browser_profile)event_bus = EventBus()- watchdog = NavigationWatchdog(browser_session=browser_session, event_bus=event_bus)+ watchdog = SecurityWatchdog(browser_session=browser_session, event_bus=event_bus)# Should match legitimate Google domainsassert watchdog._is_url_allowed('https://www.google.com') is Truediff --git a/tests/ci/test_watchdog.py b/tests/ci/test_watchdog.pyindex 041409457..096827e79 100644--- a/tests/ci/test_watchdog.py+++ b/tests/ci/test_watchdog.py@@ -57,7 +57,7 @@ async def test_watchdog_integration_with_session_lifecycle():# Verify all watchdogs are still operationalassert session._crash_watchdog is not Noneassert session._downloads_watchdog is not None- assert session._navigation_watchdog is not None+ assert session._security_watchdog is not Noneassert session._storage_state_watchdog is not Noneassert session._aboutblank_watchdog is not None@@ -92,7 +92,7 @@ async def test_watchdog_event_handler_registration():for watchdog in [session._crash_watchdog,session._downloads_watchdog,- session._navigation_watchdog,+ session._security_watchdog,session._storage_state_watchdog,session._aboutblank_watchdog,]: