Skip to content

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

raise BrowserError exceptions instead of returning success=False

Nick Sweetingd1e569eb3112
diff --git a/CLAUDE.md b/CLAUDE.md
index 87a2365c0..d49262791 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -15,12 +15,22 @@ We want our library APIs to be ergonomic, intuitive, and hard to get wrong.
- Run tests using `uv run pytest -vxs tests/ci`
- Run the type checker using `uv run pyright`
+## CDP-Use
+
+We use a thin wrapper around CDP called cdp-use: https://github.com/browser-use/cdp-use. cdp-use only provides shallow typed interfaces for the websocket calls, all CDP client and session management + other CDP helpers still live in browser_use/browser/session.py.
+
+- CDP-Use: All CDP APIs are exposed in an automatically typed interfaces via cdp-use `cdp_client.send.DomainHere.methodNameHere(params=...)` like so:
+ - `cdp_client.send.DOMSnapshot.enable(session_id=session_id)`
+ - `cdp_client.send.Target.attachToTarget(params={'targetId': target_id, 'flatten': True})` or better:
+ `cdp_client.send.Target.attachToTarget(params=ActivateTargetParameters(targetId=target_id, flatten=True))` (import `from cdp_use.cdp.target import ActivateTargetParameters`)
+ - `cdp_client.register.Browser.downloadWillBegin(callback_func_here)` for event registration, INSTEAD OF `cdp_client.on(...)` which does not exist!
+
## Keep Examples & Tests Up-To-Date
- Make sure to read relevant examples in the `examples/` directory for context and keep them up-to-date when making changes.
- Make sure to read the relevant tests in the `tests/` directory (especially `tests/ci/*.py`) and keep them up-to-date as well.
-- Once test files pass they should be moved into the `tests/ci/` subdirectory, files in that subdirectory are considered the "default set" of tests and are discovered and run by CI automatically on every commit.
-- Never use mocks in tests other than for the llm, instead use pytest fixtures to set up real objects and pytest-httpserver
+- Once test files pass they should be moved into the `tests/ci/` subdirectory, files in that subdirectory are considered the "default set" of tests and are discovered and run by CI automatically on every commit. Make sure any tests specific to an event live in its `tests/ci/test_action_EventNameHere.py` file.
+- Never mock anything in tests, always use real objects!! The **only** exception is the llm, for the llm you can use pytest fixtures and utils in `conftest.py` to set up LLM responses. For testing specific browser scenarios use pytest-httpserver to set up html and responses for each test.
- Never use real remote URLs in tests (e.g. `https://google.com` or `https://example.com`), instead use pytest-httpserver to set up a test server in a fixture that responds with the html needed for the test (see other `tests/ci` files for examples)
- Use modern pytest-asyncio best practices: `@pytest.mark.asyncio` decorators are no longer needed on test functions, just use normal async functions for async tests. Use `loop = asyncio.get_event_loop()` inside tests that need it instead of passing `event_loop` as a function argument. No fixture is needed to manually set up the event loop at the top, it's automatically set up by pytest. Fixture functions (even async ones) only need a simple `@pytest.fixture` decorator with no arguments.
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index dda837ec5..b23194a54 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1487,8 +1487,14 @@ class Agent(Generic[Context, AgentStructuredOutput]):
assert self.browser_session is not None, 'BrowserSession is not set up'
try:
- cached_selector_map = self.browser_session._cached_browser_state_summary.dom_state.selector_map
- cached_element_hashes = {hash(e) for e in cached_selector_map.values()}
+ if (
+ self.browser_session._cached_browser_state_summary is not None
+ and self.browser_session._cached_browser_state_summary.dom_state is not None
+ ):
+ cached_selector_map = self.browser_session._cached_browser_state_summary.dom_state.selector_map
+ cached_element_hashes = {hash(e) for e in cached_selector_map.values()}
+ else:
+ cached_element_hashes = set()
except Exception as e:
self.logger.error(f'Error getting cached selector map: {e}')
cached_element_hashes = set()
diff --git a/browser_use/browser/crash_watchdog.py b/browser_use/browser/crash_watchdog.py
index 310df1973..12581c768 100644
--- a/browser_use/browser/crash_watchdog.py
+++ b/browser_use/browser/crash_watchdog.py
@@ -45,7 +45,7 @@ class CrashWatchdog(BaseWatchdog):
# Configuration
network_timeout_seconds: float = Field(default=10.0)
- check_interval_seconds: float = Field(default=30.0) # Reduced frequency to reduce noise
+ check_interval_seconds: float = Field(default=5.0) # Reduced frequency to reduce noise
# Private state
_active_requests: dict[str, NetworkRequestTracker] = PrivateAttr(default_factory=dict)
@@ -58,7 +58,7 @@ class CrashWatchdog(BaseWatchdog):
"""Start monitoring when browser is connected."""
# logger.debug('[CrashWatchdog] Browser connected event received, beginning monitoring')
- await self._start_monitoring()
+ asyncio.create_task(self._start_monitoring())
# logger.debug(f'[CrashWatchdog] Monitoring task started: {self._monitoring_task and not self._monitoring_task.done()}')
async def on_BrowserStoppedEvent(self, event: BrowserStoppedEvent) -> None:
@@ -167,6 +167,11 @@ class CrashWatchdog(BaseWatchdog):
async def _on_target_crash_cdp(self, target_id: str) -> None:
"""Handle target crash detected via CDP."""
+ # Remove crashed session from pool
+ if session := self.browser_session._cdp_session_pool.pop(target_id, None):
+ await session.disconnect()
+ self.logger.info(f'[CrashWatchdog] Removed crashed session from pool: {target_id}')
+
# Get target info
cdp_client = self.browser_session.cdp_client
targets = await cdp_client.send.Target.getTargets()
@@ -234,6 +239,7 @@ class CrashWatchdog(BaseWatchdog):
async def _monitoring_loop(self) -> None:
"""Main monitoring loop."""
+ await asyncio.sleep(10) # give browser time to start up and load the first page after first LLM call
while True:
try:
await self._check_network_timeouts()
@@ -305,6 +311,12 @@ class CrashWatchdog(BaseWatchdog):
try:
if proc.status() in (psutil.STATUS_ZOMBIE, psutil.STATUS_DEAD):
self.logger.error(f'[CrashWatchdog] Browser process {proc.pid} has crashed')
+ # Clear all sessions from pool when browser crashes
+ for session in self.browser_session._cdp_session_pool.values():
+ await session.disconnect()
+ self.browser_session._cdp_session_pool.clear()
+ self.logger.info('[CrashWatchdog] Cleared all sessions from pool due to browser crash')
+
self.event_bus.dispatch(
BrowserErrorEvent(
error_type='BrowserProcessCrashed',
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.py
index 1908274da..dd42e8631 100644
--- a/browser_use/browser/default_action_watchdog.py
+++ b/browser_use/browser/default_action_watchdog.py
@@ -5,7 +5,6 @@ import platform
from typing import Any
from browser_use.browser.events import (
- BrowserErrorEvent,
ClickElementEvent,
GoBackEvent,
GoForwardEvent,
@@ -32,21 +31,14 @@ UploadFileEvent.model_rebuild()
class DefaultActionWatchdog(BaseWatchdog):
"""Handles default browser actions like click, type, and scroll using CDP."""
- async def on_ClickElementEvent(self, event: ClickElementEvent) -> dict[str, str] | None:
+ async def on_ClickElementEvent(self, event: ClickElementEvent) -> None:
"""Handle click request with CDP."""
try:
# Check if session is alive before attempting any operations
if not self.browser_session.agent_focus or not self.browser_session.agent_focus.target_id:
error_msg = 'Cannot execute click: browser session is corrupted (target_id=None). Session may have crashed.'
self.logger.error(f'⚠️ {error_msg}')
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='SessionCorrupted',
- message=error_msg,
- details={'action': 'click', 'target_id': None},
- )
- )
- return {'success': 'false', 'error': error_msg}
+ raise BrowserError(error_msg)
# Use the provided node
element_node = event.node
@@ -59,15 +51,8 @@ class DefaultActionWatchdog(BaseWatchdog):
if self.browser_session.is_file_input(element_node):
msg = f'Index {index_for_logging} - has an element which opens file upload dialog. To upload files please use a specific function to upload files'
self.logger.info(msg)
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='FileInputElement',
- message=msg,
- details={'index': index_for_logging},
- )
- )
- raise Exception(
- 'Click triggered a FileInputElement which could not be handled, use the dedicated upload file function instead'
+ raise BrowserError(
+ 'Click triggered a file input element which could not be handled, use the dedicated file upload function instead'
)
# Perform the actual click using internal implementation
@@ -100,26 +85,22 @@ class DefaultActionWatchdog(BaseWatchdog):
new_tab_msg = 'New tab opened - switching to it'
msg += f' - {new_tab_msg}'
self.logger.info(f'🔗 {new_tab_msg}')
- # Switch to the last tab (newly created tab)
- from browser_use.browser.events import SwitchTabEvent
-
- last_tab_index = len(after_target_ids) - 1
- await self.event_bus.dispatch(SwitchTabEvent(tab_index=last_tab_index))
-
- # Return download_path if any
- if download_path:
- return {'download_path': download_path}
+
+ # Optional:Switch to the newly created tab
+ # Not recommended usually in order to match normal behavior with Cmd/Ctrl+Click
+ # If agent wanted to focus on the new page they would've clicked without new_tab
+ # from browser_use.browser.events import SwitchTabEvent
+
+ # last_tab_index = len(after_target_ids) - 1
+ # switch_event = self.event_bus.dispatch(SwitchTabEvent(tab_index=last_tab_index))
+ # await switch_event
+
+ # Successfully clicked, return None
+ return None
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='ClickFailed',
- message=str(e),
- details={'index': index_for_logging if 'index_for_logging' in locals() else 'unknown'},
- )
- )
raise
- async def on_TypeTextEvent(self, event: TypeTextEvent) -> dict[str, bool | str] | None:
+ async def on_TypeTextEvent(self, event: TypeTextEvent) -> None:
"""Handle text input request with CDP."""
try:
# Use the provided node
@@ -150,31 +131,16 @@ class DefaultActionWatchdog(BaseWatchdog):
if self.browser_session._dom_watchdog:
self.browser_session._dom_watchdog.clear_cache()
- return {'success': True}
+ return None
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='InputTextFailed',
- message=str(e),
- details={'index': element_node.element_index or 'unknown', 'text': event.text},
- )
- )
- return {'success': False, 'error': str(e)}
+ raise
- async def on_ScrollEvent(self, event: ScrollEvent) -> dict[str, str | bool] | None:
+ async def on_ScrollEvent(self, event: ScrollEvent) -> None:
"""Handle scroll request with CDP."""
- try:
- # Check if we have a current target for scrolling
- if not self.browser_session.agent_focus:
- raise ValueError('No active target for scrolling')
- except ValueError as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='NoActivePage',
- message=str(e),
- )
- )
- return {'success': False, 'error': 'No active page for scrolling'}
+ # Check if we have a current target for scrolling
+ if not self.browser_session.agent_focus:
+ error_msg = 'No active target for scrolling'
+ raise BrowserError(error_msg)
try:
# Convert direction and amount to pixels
@@ -192,23 +158,16 @@ class DefaultActionWatchdog(BaseWatchdog):
self.logger.info(
f'📜 Scrolled element {index_for_logging} container {event.direction} by {event.amount} pixels'
)
- return {'success': True}
+ return None
# Perform target-level scroll
await self._scroll_with_cdp_gesture(pixels)
# Log success
self.logger.info(f'📜 Scrolled {event.direction} by {event.amount} pixels')
- return {'success': True}
+ return None
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='ScrollFailed',
- message=str(e),
- details={'direction': event.direction, 'amount': event.amount},
- )
- )
- return {'success': False, 'error': f'Scroll failed: {str(e)}'}
+ raise
# ========== Implementation Methods ==========
@@ -425,31 +384,44 @@ class DefaultActionWatchdog(BaseWatchdog):
# Mouse down
self.logger.debug(f'👆🏾 Clicking x: {center_x}px y: {center_y}px with modifiers: {modifiers} ...')
- await cdp_session.cdp_client.send.Input.dispatchMouseEvent(
- params={
- 'type': 'mousePressed',
- 'x': center_x,
- 'y': center_y,
- 'button': 'left',
- 'clickCount': 1,
- 'modifiers': modifiers,
- },
- session_id=session_id,
- )
- await asyncio.sleep(0.145)
+ try:
+ await asyncio.wait_for(
+ cdp_session.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mousePressed',
+ 'x': center_x,
+ 'y': center_y,
+ 'button': 'left',
+ 'clickCount': 1,
+ 'modifiers': modifiers,
+ },
+ session_id=session_id,
+ ),
+ timeout=1.0 # 1 second timeout for mousePressed
+ )
+ await asyncio.sleep(0.145)
+ except asyncio.TimeoutError:
+ self.logger.debug('⏱️ Mouse down timed out (likely due to dialog), continuing...')
+ # Don't sleep if we timed out
# Mouse up
- await cdp_session.cdp_client.send.Input.dispatchMouseEvent(
- params={
- 'type': 'mouseReleased',
- 'x': center_x,
- 'y': center_y,
- 'button': 'left',
- 'clickCount': 1,
- 'modifiers': modifiers,
- },
- session_id=session_id,
- )
+ try:
+ await asyncio.wait_for(
+ cdp_session.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mouseReleased',
+ 'x': center_x,
+ 'y': center_y,
+ 'button': 'left',
+ 'clickCount': 1,
+ 'modifiers': modifiers,
+ },
+ session_id=session_id,
+ ),
+ timeout=1.0 # 1 second timeout for mouseReleased
+ )
+ except asyncio.TimeoutError:
+ self.logger.debug('⏱️ Mouse up timed out (likely due to dialog), continuing...')
self.logger.debug('🖱️ Clicked successfully using x,y coordinates')
@@ -906,12 +878,7 @@ class DefaultActionWatchdog(BaseWatchdog):
self.logger.info(f'🔙 Navigated back to {entries[current_index - 1]["url"]}')
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='NavigateBackFailed',
- message=str(e),
- )
- )
+ raise
async def on_GoForwardEvent(self, event: GoForwardEvent) -> None:
"""Handle navigate forward request with CDP."""
@@ -939,12 +906,7 @@ class DefaultActionWatchdog(BaseWatchdog):
self.logger.info(f'🔜 Navigated forward to {entries[current_index + 1]["url"]}')
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='NavigateForwardFailed',
- message=str(e),
- )
- )
+ raise
async def on_RefreshEvent(self, event: RefreshEvent) -> None:
"""Handle target refresh request with CDP."""
@@ -967,12 +929,7 @@ class DefaultActionWatchdog(BaseWatchdog):
self.logger.info('🔄 Target refreshed')
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='RefreshFailed',
- message=str(e),
- )
- )
+ raise
async def on_WaitEvent(self, event: WaitEvent) -> None:
"""Handle wait request."""
@@ -986,12 +943,7 @@ class DefaultActionWatchdog(BaseWatchdog):
await asyncio.sleep(actual_seconds)
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='WaitFailed',
- message=str(e),
- )
- )
+ raise
async def on_SendKeysEvent(self, event: SendKeysEvent) -> None:
"""Handle send keys request with CDP."""
@@ -1084,13 +1036,7 @@ class DefaultActionWatchdog(BaseWatchdog):
if self.browser_session._dom_watchdog:
self.browser_session._dom_watchdog.clear_cache()
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='SendKeysFailed',
- message=str(e),
- details={'keys': event.keys},
- )
- )
+ raise
async def on_UploadFileEvent(self, event: UploadFileEvent) -> None:
"""Handle file upload request with CDP."""
@@ -1119,70 +1065,64 @@ class DefaultActionWatchdog(BaseWatchdog):
self.logger.info(f'📎 Uploaded file {event.file_path} to element {index_for_logging}')
except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='UploadFileFailed',
- message=str(e),
- details={'element_index': element_node.element_index or 'unknown', 'file_path': event.file_path},
- )
- )
+ raise
async def on_ScrollToTextEvent(self, event: ScrollToTextEvent) -> None:
- """Handle scroll to text request with CDP."""
- try:
- # Get CDP client and session
- cdp_client = self.browser_session.cdp_client
- assert self.browser_session.agent_focus is not None, 'CDP session not initialized - browser may not be connected yet'
- session_id = self.browser_session.agent_focus.session_id
-
- # Enable DOM
- await cdp_client.send.DOM.enable(session_id=session_id)
-
- # Get document
- doc = await cdp_client.send.DOM.getDocument(params={'depth': -1}, session_id=session_id)
- root_node_id = doc['root']['nodeId']
-
- # Search for text using XPath
- search_queries = [
- f'//*[contains(text(), "{event.text}")]',
- f'//*[contains(., "{event.text}")]',
- f'//*[@*[contains(., "{event.text}")]]',
- ]
-
- found = False
- for query in search_queries:
- try:
- # Perform search
- search_result = await cdp_client.send.DOM.performSearch(params={'query': query}, session_id=session_id)
- search_id = search_result['searchId']
- result_count = search_result['resultCount']
-
- if result_count > 0:
- # Get the first match
- node_ids = await cdp_client.send.DOM.getSearchResults(
- params={'searchId': search_id, 'fromIndex': 0, 'toIndex': 1},
- session_id=session_id,
- )
+ """Handle scroll to text request with CDP. Raises exception if text not found."""
+ # Get CDP client and session
+ cdp_client = self.browser_session.cdp_client
+ if self.browser_session.agent_focus is None:
+ raise BrowserError('CDP session not initialized - browser may not be connected yet')
+ session_id = self.browser_session.agent_focus.session_id
+
+ # Enable DOM
+ await cdp_client.send.DOM.enable(session_id=session_id)
+
+ # Get document
+ doc = await cdp_client.send.DOM.getDocument(params={'depth': -1}, session_id=session_id)
+ root_node_id = doc['root']['nodeId']
+
+ # Search for text using XPath
+ search_queries = [
+ f'//*[contains(text(), "{event.text}")]',
+ f'//*[contains(., "{event.text}")]',
+ f'//*[@*[contains(., "{event.text}")]]',
+ ]
+
+ found = False
+ for query in search_queries:
+ try:
+ # Perform search
+ search_result = await cdp_client.send.DOM.performSearch(params={'query': query}, session_id=session_id)
+ search_id = search_result['searchId']
+ result_count = search_result['resultCount']
+
+ if result_count > 0:
+ # Get the first match
+ node_ids = await cdp_client.send.DOM.getSearchResults(
+ params={'searchId': search_id, 'fromIndex': 0, 'toIndex': 1},
+ session_id=session_id,
+ )
- if node_ids['nodeIds']:
- node_id = node_ids['nodeIds'][0]
+ if node_ids['nodeIds']:
+ node_id = node_ids['nodeIds'][0]
- # Scroll the element into view
- await cdp_client.send.DOM.scrollIntoViewIfNeeded(params={'nodeId': node_id}, session_id=session_id)
+ # Scroll the element into view
+ await cdp_client.send.DOM.scrollIntoViewIfNeeded(params={'nodeId': node_id}, session_id=session_id)
- found = True
- self.logger.info(f'📜 Scrolled to text: "{event.text}"')
- break
+ found = True
+ self.logger.info(f'📜 Scrolled to text: "{event.text}"')
+ break
- # Clean up search
- await cdp_client.send.DOM.discardSearchResults(params={'searchId': search_id}, session_id=session_id)
- except Exception as e:
- self.logger.debug(f'Search query failed: {query}, error: {e}')
- continue
+ # Clean up search
+ await cdp_client.send.DOM.discardSearchResults(params={'searchId': search_id}, session_id=session_id)
+ except Exception as e:
+ self.logger.debug(f'Search query failed: {query}, error: {e}')
+ continue
- if not found:
- # Fallback: Try JavaScript search
- js_result = await cdp_client.send.Runtime.evaluate(
+ if not found:
+ # Fallback: Try JavaScript search
+ js_result = await cdp_client.send.Runtime.evaluate(
params={
'expression': f'''
(() => {{
@@ -1206,15 +1146,15 @@ class DefaultActionWatchdog(BaseWatchdog):
session_id=session_id,
)
- if js_result.get('result', {}).get('value'):
- self.logger.info(f'📜 Scrolled to text: "{event.text}" (via JS)')
- else:
- self.logger.warning(f'⚠️ Text not found: "{event.text}"')
- except Exception as e:
- self.event_bus.dispatch(
- BrowserErrorEvent(
- error_type='ScrollToTextFailed',
- message=str(e),
- details={'text': event.text},
- )
- )
+ if js_result.get('result', {}).get('value'):
+ self.logger.info(f'📜 Scrolled to text: "{event.text}" (via JS)')
+ return None
+ else:
+ self.logger.warning(f'⚠️ Text not found: "{event.text}"')
+ raise BrowserError(f'Text not found: "{event.text}"', details={'text': event.text})
+
+ # If we got here and found is True, return None (success)
+ if found:
+ return None
+ else:
+ raise BrowserError(f'Text not found: "{event.text}"', details={'text': event.text})
diff --git a/browser_use/browser/dom_watchdog.py b/browser_use/browser/dom_watchdog.py
index 8fde0b4db..684c68977 100644
--- a/browser_use/browser/dom_watchdog.py
+++ b/browser_use/browser/dom_watchdog.py
@@ -146,7 +146,9 @@ class DOMWatchdog(BaseWatchdog):
"""
from browser_use.browser.views import BrowserStateSummary, PageInfo
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: STARTING browser state request')
page_url = await self.browser_session.get_current_page_url()
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Got page URL: {page_url}')
if self.browser_session.agent_focus:
self.logger.debug(
f'📍 Current page URL: {page_url}, target_id: {self.browser_session.agent_focus.target_id}, session_id: {self.browser_session.agent_focus.session_id}'
@@ -159,16 +161,17 @@ class DOMWatchdog(BaseWatchdog):
# Wait for page stability using browser profile settings (main branch pattern)
if not not_a_meaningful_website:
- self.logger.debug('⏳ Waiting for page stability...')
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: ⏳ Waiting for page stability...')
try:
await self._wait_for_stable_network()
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: ✅ Page stability complete')
except Exception as e:
- self.logger.warning(f'Network waiting failed: {e}, continuing anyway...')
+ self.logger.warning(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Network waiting failed: {e}, continuing anyway...')
# Get tabs info once at the beginning for all paths
- self.logger.debug('Getting tabs info...')
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: Getting tabs info...')
tabs_info = await self.browser_session.get_tabs()
- self.logger.debug(f'Got {len(tabs_info)} tabs')
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Got {len(tabs_info)} tabs')
try:
# Fast path for empty pages
@@ -218,7 +221,7 @@ class DOMWatchdog(BaseWatchdog):
# Normal path: Build DOM tree if requested
if event.include_dom:
- self.logger.debug('🌳 Building DOM tree...')
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: 🌳 Building DOM tree...')
# Build the DOM directly using the internal method
previous_state = (
@@ -229,9 +232,11 @@ class DOMWatchdog(BaseWatchdog):
try:
# Call the DOM building method directly
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: Starting _build_dom_tree...')
content = await self._build_dom_tree(previous_state)
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: ✅ _build_dom_tree completed')
except Exception as e:
- self.logger.warning(f'DOM build failed: {e}, using minimal state')
+ self.logger.warning(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: DOM build failed: {e}, using minimal state')
content = SerializedDOMState(_root=None, selector_map={})
if not content:
@@ -245,7 +250,7 @@ class DOMWatchdog(BaseWatchdog):
# Get screenshot if requested
screenshot_b64 = None
if event.include_screenshot:
- self.logger.debug(f'📸 DOM watchdog requesting screenshot, include_screenshot={event.include_screenshot}')
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: 📸 DOM watchdog requesting screenshot, include_screenshot={event.include_screenshot}')
try:
# Check if handler is registered
handlers = self.event_bus.handlers.get('ScreenshotEvent', [])
@@ -255,19 +260,16 @@ class DOMWatchdog(BaseWatchdog):
screenshot_event = self.event_bus.dispatch(ScreenshotEvent(full_page=False, event_timeout=6.0))
self.logger.debug('📸 Dispatched ScreenshotEvent, waiting for event to complete...')
- # Wait for the event itself to complete (this waits for all handlers)
- await screenshot_event
-
- # Now get the results after the event has completed
- screenshot_result = await screenshot_event.event_results_flat_dict()
- self.logger.debug(f'📸 Got screenshot result: {screenshot_result.keys() if screenshot_result else None}')
-
- if screenshot_result:
- screenshot_b64 = screenshot_result.get('screenshot')
- if screenshot_b64:
- self.logger.debug(f'📸 Screenshot captured in DOM watchdog, length: {len(screenshot_b64)}')
- else:
- self.logger.warning('📸 Screenshot result returned but screenshot was None')
+ # Get the screenshot bytes directly from event_result()
+ screenshot_bytes = await screenshot_event.event_result(raise_if_any=True, raise_if_none=True)
+ if screenshot_bytes:
+ # Convert bytes back to base64 string for BrowserStateSummary
+ import base64
+ screenshot_b64 = base64.b64encode(screenshot_bytes).decode('utf-8')
+ self.logger.debug(f'📸 Got screenshot: {len(screenshot_bytes)} bytes')
+ else:
+ screenshot_b64 = None
+ self.logger.warning('📸 Screenshot handler returned None')
except TimeoutError:
self.logger.warning('📸 Screenshot timed out after 6 seconds - no handler registered or slow page?')
except Exception as e:
@@ -279,20 +281,20 @@ class DOMWatchdog(BaseWatchdog):
# Get target title safely
try:
- self.logger.debug('Getting page title...')
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: Getting page title...')
title = await asyncio.wait_for(self.browser_session.get_current_page_title(), timeout=2.0)
- self.logger.debug(f'Got title: {title}')
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Got title: {title}')
except Exception as e:
- self.logger.debug(f'Failed to get title: {e}')
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Failed to get title: {e}')
title = 'Page'
# Get comprehensive page info from CDP
try:
- self.logger.debug('Getting page info from CDP...')
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: Getting page info from CDP...')
page_info = await self._get_page_info()
- self.logger.debug(f'Got page info from CDP: {page_info}')
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Got page info from CDP: {page_info}')
except Exception as e:
- self.logger.debug(f'Failed to get page info from CDP: {e}, using fallback')
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: Failed to get page info from CDP: {e}, using fallback')
# Fallback to default viewport dimensions
viewport = self.browser_session.browser_profile.viewport or {'width': 1280, 'height': 720}
page_info = PageInfo(
@@ -313,9 +315,9 @@ class DOMWatchdog(BaseWatchdog):
# Build and cache the browser state summary
if screenshot_b64:
- self.logger.debug(f'📸 Creating BrowserStateSummary with screenshot, length: {len(screenshot_b64)}')
+ self.logger.debug(f'🔍 DOMWatchdog.on_BrowserStateRequestEvent: 📸 Creating BrowserStateSummary with screenshot, length: {len(screenshot_b64)}')
else:
- self.logger.debug('📸 Creating BrowserStateSummary WITHOUT screenshot')
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: 📸 Creating BrowserStateSummary WITHOUT screenshot')
browser_state = BrowserStateSummary(
dom_state=content,
@@ -334,7 +336,7 @@ class DOMWatchdog(BaseWatchdog):
# Cache the state
self.browser_session._cached_browser_state_summary = browser_state
- self.logger.debug('Returning browser state from on_BrowserStateRequestEvent')
+ self.logger.debug('🔍 DOMWatchdog.on_BrowserStateRequestEvent: ✅ COMPLETED - Returning browser state')
return browser_state
except Exception as e:
@@ -376,49 +378,63 @@ class DOMWatchdog(BaseWatchdog):
SerializedDOMState with serialized DOM and selector map
"""
try:
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: STARTING DOM tree build')
# Remove any existing highlights before building new DOM
try:
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: Removing existing highlights...')
await self.browser_session.remove_highlights()
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: ✅ Highlights removed')
except Exception as e:
- self.logger.debug(f'Failed to remove existing highlights: {e}')
+ self.logger.debug(f'🔍 DOMWatchdog._build_dom_tree: Failed to remove existing highlights: {e}')
# Create or reuse DOM service
if self._dom_service is None:
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: Creating DomService...')
self._dom_service = DomService(browser_session=self.browser_session, logger=self.logger)
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: ✅ DomService created')
+ else:
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: Reusing existing DomService')
# Get serialized DOM tree using the service
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: Calling DomService.get_serialized_dom_tree...')
start = time.time()
self.current_dom_state, self.enhanced_dom_tree, timing_info = await self._dom_service.get_serialized_dom_tree(
previous_cached_state=previous_state
)
end = time.time()
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: ✅ DomService.get_serialized_dom_tree completed')
self.logger.debug(f'Time taken to get DOM tree: {end - start} seconds')
self.logger.debug(f'Timing breakdown: {timing_info}')
# Update selector map for other watchdogs
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: Updating selector maps...')
self.selector_map = self.current_dom_state.selector_map
# Update BrowserSession's cached selector map
if self.browser_session:
self.browser_session.update_cached_selector_map(self.selector_map)
+ self.logger.debug(f'🔍 DOMWatchdog._build_dom_tree: ✅ Selector maps updated, {len(self.selector_map)} elements')
# Inject highlighting for visual feedback if we have elements
if self.selector_map and self._dom_service:
try:
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: Injecting highlighting script...')
from browser_use.dom.debug.highlights import inject_highlighting_script
await inject_highlighting_script(self._dom_service, self.selector_map)
- self.logger.debug(f'Injected highlighting for {len(self.selector_map)} elements')
+ self.logger.debug(f'🔍 DOMWatchdog._build_dom_tree: ✅ Injected highlighting for {len(self.selector_map)} elements')
except Exception as e:
- self.logger.debug(f'Failed to inject highlighting: {e}')
+ self.logger.debug(f'🔍 DOMWatchdog._build_dom_tree: Failed to inject highlighting: {e}')
# Build UUID selector map
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: Building UUID selector map...')
self.uuid_selector_map = {}
if self.selector_map:
for node in self.selector_map.values():
if hasattr(node, 'uuid'):
self.uuid_selector_map[node.uuid] = node
+ self.logger.debug('🔍 DOMWatchdog._build_dom_tree: ✅ COMPLETED DOM tree build')
return self.current_dom_state
except Exception as e:
diff --git a/browser_use/browser/downloads_watchdog.py b/browser_use/browser/downloads_watchdog.py
index aedb1a16d..da1e5bfa9 100644
--- a/browser_use/browser/downloads_watchdog.py
+++ b/browser_use/browser/downloads_watchdog.py
@@ -3,12 +3,14 @@
import asyncio
import json
import os
+import tempfile
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar
from urllib.parse import urlparse
import anyio
from bubus import BaseEvent
+from cdp_use.cdp.browser import DownloadProgressEvent, DownloadWillBeginEvent
from pydantic import PrivateAttr
from browser_use.browser.events import (
@@ -20,7 +22,6 @@ from browser_use.browser.events import (
TabCreatedEvent,
)
from browser_use.browser.watchdog_base import BaseWatchdog
-from cdp_use.cdp.browser import DownloadProgressEvent, DownloadWillBeginEvent
if TYPE_CHECKING:
pass
@@ -101,7 +102,7 @@ class DownloadsWatchdog(BaseWatchdog):
async def on_NavigationCompleteEvent(self, event: NavigationCompleteEvent) -> None:
"""Check for PDFs after navigation completes."""
self.logger.debug(f'[DownloadsWatchdog] NavigationCompleteEvent received for {event.url}, tab_index={event.tab_index}')
-
+
# Clear PDF cache for the navigated URL since content may have changed
if event.url in self._pdf_viewer_cache:
del self._pdf_viewer_cache[event.url]
@@ -114,7 +115,7 @@ class DownloadsWatchdog(BaseWatchdog):
target_id = await self.browser_session.get_target_id_by_tab_index(event.tab_index)
self.logger.debug(f'[DownloadsWatchdog] Got target_id={target_id} for tab_index={event.tab_index}')
-
+
if target_id:
is_pdf = await self.check_for_pdf_viewer(target_id)
if is_pdf:
@@ -224,6 +225,10 @@ class DownloadsWatchdog(BaseWatchdog):
async def _handle_cdp_download(self, event: DownloadWillBeginEvent, target_id: str, session_id: str | None) -> None:
"""Handle a CDP Page.downloadWillBegin event."""
+ downloads_dir = Path(
+ self.browser_session.browser_profile.downloads_path
+ or f'{tempfile.gettempdir()}/browser_use_downloads.{str(self.browser_session.id)[-4:]}'
+ )
try:
download_url = event.get('url', '')
suggested_filename = event.get('suggestedFilename', 'download')
@@ -232,21 +237,14 @@ class DownloadsWatchdog(BaseWatchdog):
self.logger.info(f'[DownloadsWatchdog] ⬇️ File download starting: {suggested_filename} from {download_url[:100]}...')
self.logger.debug(f'[DownloadsWatchdog] Full CDP event: {event}')
- # Get download directory
- downloads_dir = self.browser_session.browser_profile.downloads_path
- if not downloads_dir:
- downloads_dir = str(Path.home() / 'Downloads')
- else:
- downloads_dir = str(downloads_dir)
-
# Since Browser.setDownloadBehavior is already configured, the browser will download the file
# We just need to wait for it to appear in the downloads directory
- expected_path = Path(downloads_dir) / suggested_filename
+ expected_path = downloads_dir / suggested_filename
# Debug: List current directory contents
self.logger.debug(f'[DownloadsWatchdog] Downloads directory: {downloads_dir}')
- if Path(downloads_dir).exists():
- files_before = list(Path(downloads_dir).iterdir())
+ if downloads_dir.exists():
+ files_before = list(downloads_dir.iterdir())
self.logger.debug(f'[DownloadsWatchdog] Files before download: {[f.name for f in files_before]}')
# Browser.setDownloadBehavior doesn't work reliably with CDP connections
@@ -258,9 +256,12 @@ class DownloadsWatchdog(BaseWatchdog):
import json
escaped_url = json.dumps(download_url)
- cdp_client = self.browser_session.cdp_client
- result = await cdp_client.send.Runtime.evaluate(
+ # Get the proper session for the frame that initiated the download
+ cdp_session = await self.browser_session.cdp_client_for_frame(event.get('frameId'))
+ assert cdp_session
+
+ result = await cdp_session.cdp_client.send.Runtime.evaluate(
params={
'expression': f"""
(async () => {{
@@ -285,7 +286,7 @@ class DownloadsWatchdog(BaseWatchdog):
'awaitPromise': True,
'returnByValue': True,
},
- session_id=session_id if session_id else None,
+ session_id=cdp_session.session_id,
)
download_result = result.get('result', {}).get('value')
@@ -295,8 +296,8 @@ class DownloadsWatchdog(BaseWatchdog):
file_size = len(file_data)
# Ensure unique filename
- unique_filename = await self._get_unique_filename(downloads_dir, suggested_filename)
- final_path = Path(downloads_dir) / unique_filename
+ unique_filename = await self._get_unique_filename(str(downloads_dir), suggested_filename)
+ final_path = downloads_dir / unique_filename
# Write the file
import anyio
@@ -308,11 +309,11 @@ class DownloadsWatchdog(BaseWatchdog):
expected_path = final_path
else:
self.logger.error('[DownloadsWatchdog] ❌ No data received from fetch')
- return
+ # Fall through to polling logic
except Exception as fetch_error:
self.logger.error(f'[DownloadsWatchdog] ❌ Failed to download file via fetch: {fetch_error}')
- return
+ # Fall through to polling logic
# Determine file type from extension
file_ext = expected_path.suffix.lower().lstrip('.')
@@ -326,7 +327,7 @@ class DownloadsWatchdog(BaseWatchdog):
file_name=unique_filename,
file_size=file_size,
file_type=file_type,
- mime_type=download_result.get('contentType'),
+ mime_type=download_result.get('contentType') if download_result else None,
from_cache=False,
auto_download=False,
)
@@ -335,9 +336,62 @@ class DownloadsWatchdog(BaseWatchdog):
self.logger.info(
f'[DownloadsWatchdog] ✅ File download completed via CDP: {suggested_filename} ({file_size} bytes) saved to {expected_path}'
)
-
+ # Success - return early
+ return
except Exception as e:
- self.logger.error(f'[DownloadsWatchdog] ❌ Error handling CDP download: {e}')
+ self.logger.error(f'[DownloadsWatchdog] ❌ Error handling CDP download: {type(e).__name__} {e}')
+
+ # If we reach here, the fetch method failed, so wait for native download
+ # Poll the downloads directory for new files
+ self.logger.info(f'[DownloadsWatchdog] Checking if browser auto-download saved the file for us: {suggested_filename}')
+
+ # Get initial list of files in downloads directory
+ initial_files = set()
+ if Path(downloads_dir).exists():
+ for f in Path(downloads_dir).iterdir():
+ if f.is_file() and not f.name.startswith('.'):
+ initial_files.add(f.name)
+
+ # Poll for new files
+ max_wait = 20 # seconds
+ start_time = asyncio.get_event_loop().time()
+
+ while asyncio.get_event_loop().time() - start_time < max_wait:
+ await asyncio.sleep(2.0) # Check every 2 seconds
+
+ if Path(downloads_dir).exists():
+ for file_path in Path(downloads_dir).iterdir():
+ # Skip hidden files and files that were already there
+ if file_path.is_file() and not file_path.name.startswith('.') and file_path.name not in initial_files:
+ # Check if file has content (> 4 bytes)
+ try:
+ file_size = file_path.stat().st_size
+ if file_size > 4:
+ # Found a new download!
+ self.logger.info(f'[DownloadsWatchdog] ✅ Found downloaded file: {file_path} ({file_size} bytes)')
+
+ # Track the download
+ self.browser_session._downloaded_files.append(str(file_path))
+
+ # Determine file type from extension
+ file_ext = file_path.suffix.lower().lstrip('.')
+ file_type = file_ext if file_ext else None
+
+ # Dispatch download event
+ self.event_bus.dispatch(
+ FileDownloadedEvent(
+ url=download_url,
+ path=str(file_path),
+ file_name=file_path.name,
+ file_size=file_size,
+ file_type=file_type,
+ )
+ )
+ return
+ except Exception as e:
+ self.logger.debug(f'[DownloadsWatchdog] Error checking file {file_path}: {e}')
+
+ self.logger.warning(f'[DownloadsWatchdog] Download did not complete within {max_wait} seconds')
async def _handle_download(self, download: Any) -> None:
"""Handle a download event."""
@@ -454,7 +508,7 @@ class DownloadsWatchdog(BaseWatchdog):
Returns True if a PDF is detected and should be downloaded.
"""
self.logger.debug(f'[DownloadsWatchdog] Checking if target {target_id} is PDF viewer...')
-
+
# Get target info to get URL
cdp_client = self.browser_session.cdp_client
targets = await cdp_client.send.Target.getTargets()
@@ -474,7 +528,7 @@ class DownloadsWatchdog(BaseWatchdog):
try:
# Create a temporary CDP session for this target without switching focus
import asyncio
-
+
temp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False)
result = await asyncio.wait_for(
@@ -570,7 +624,7 @@ class DownloadsWatchdog(BaseWatchdog):
Returns the download path if successful, None otherwise.
"""
self.logger.debug(f'[DownloadsWatchdog] trigger_pdf_download called for target_id={target_id}')
-
+
if not self.browser_session.browser_profile.downloads_path:
self.logger.warning('[DownloadsWatchdog] ❌ No downloads path configured, cannot save PDF download')
return None
@@ -581,7 +635,7 @@ class DownloadsWatchdog(BaseWatchdog):
try:
# Create a temporary CDP session for this target without switching focus
import asyncio
-
+
self.logger.debug(f'[DownloadsWatchdog] Creating CDP session for PDF download from target {target_id}')
temp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False)
@@ -623,7 +677,7 @@ class DownloadsWatchdog(BaseWatchdog):
pdf_filename = os.path.basename(parsed.path) or 'document.pdf'
if not pdf_filename.endswith('.pdf'):
pdf_filename += '.pdf'
-
+
self.logger.debug(f'[DownloadsWatchdog] Generated filename: {pdf_filename}')
# Check if already downloaded by looking in the downloads directory
@@ -697,7 +751,9 @@ class DownloadsWatchdog(BaseWatchdog):
# Verify file was written successfully
if os.path.exists(download_path):
actual_size = os.path.getsize(download_path)
- self.logger.debug(f'[DownloadsWatchdog] PDF file written successfully: {download_path} ({actual_size} bytes)')
+ self.logger.debug(
+ f'[DownloadsWatchdog] PDF file written successfully: {download_path} ({actual_size} bytes)'
+ )
else:
self.logger.error(f'[DownloadsWatchdog] ❌ Failed to write PDF file to: {download_path}')
return None
diff --git a/browser_use/browser/events.py b/browser_use/browser/events.py
index a74762a1b..8b706bb0d 100644
--- a/browser_use/browser/events.py
+++ b/browser_use/browser/events.py
@@ -9,6 +9,7 @@ from pydantic import Field, field_validator
from browser_use.browser.views import BrowserStateSummary
from browser_use.dom.views import EnhancedDOMTreeNode
+from pydantic import BaseModel
# ============================================================================
# Agent/Controller -> BrowserSession Events (High-level browser actions)
@@ -49,59 +50,101 @@ class ElementSelectedEvent(BaseEvent[T_EventResultType]):
snapshot_node=None,
)
-
-class NavigateToUrlEvent(BaseEvent):
+# TODO: add page handle to events
+# class PageHandle(share a base with browser.session.CDPSession?):
+# url: str
+# tab_index: int
+# target_id: str
+# @classmethod
+# def from_tab_index(cls, tab_index: int) -> Self:
+# return cls(tab_index=tab_index)
+# @classmethod
+# def from_target_id(cls, target_id: str) -> Self:
+# return cls(target_id=target_id)
+# @classmethod
+# def from_url(cls, url: str) -> Self:
+# @property
+# def root_frame_id(self) -> str:
+# return self.target_id
+# @property
+# def session_id(self) -> str:
+# return browser_session.get_or_create_cdp_session(self.target_id).session_id
+
+# class PageSelectedEvent(BaseEvent[T_EventResultType]):
+# """An event like SwitchToTabEvent(page=PageHandle) or CloseTabEvent(page=PageHandle)"""
+# page: PageHandle
+
+
+class NavigateToUrlEvent(BaseEvent[None]):
"""Navigate to a specific URL."""
url: str
wait_until: Literal['load', 'domcontentloaded', 'networkidle', 'commit'] = 'load'
- new_tab: bool = False
timeout_ms: int | None = None
+ new_tab: bool = Field(default=False, description='Set True to leave the current tab alone and open a new tab in the foreground for the new URL')
+ # existing_tab: PageHandle | None = None # TODO
+
+ # limit enforced by bubus, not exposed to LLM:
+ event_timeout: float | None = 15.0 # seconds
-class ClickElementEvent(ElementSelectedEvent[dict[str, str] | None]):
+class ClickElementEvent(ElementSelectedEvent[None]):
"""Click an element."""
node: 'EnhancedDOMTreeNode'
button: Literal['left', 'right', 'middle'] = 'left'
- new_tab: bool = False
+ new_tab: bool = Field(default=False, description='Set True to open any link clicked in a new tab in the background, can use switch_tab(page_id=-1) after to focus it')
# click_count: int = 1 # TODO
# expect_download: bool = False # moved to downloads_watchdog.py
+ event_timeout: float | None = 15.0 # seconds
+
-class TypeTextEvent(ElementSelectedEvent):
+class TypeTextEvent(ElementSelectedEvent[None]):
"""Type text into an element."""
node: 'EnhancedDOMTreeNode'
text: str
clear_existing: bool = True
+ event_timeout: float | None = 15.0 # seconds
-class ScrollEvent(ElementSelectedEvent):
+
+class ScrollEvent(ElementSelectedEvent[None]):
"""Scroll the page or element."""
direction: Literal['up', 'down', 'left', 'right']
amount: int # pixels
node: 'EnhancedDOMTreeNode | None' = None # None means scroll page
+ event_timeout: float | None = 5.0 # seconds
+
-class SwitchTabEvent(BaseEvent):
+class SwitchTabEvent(BaseEvent[None]):
"""Switch to a different tab."""
tab_index: int
+ event_timeout: float | None = 10.0 # seconds
+
-class CloseTabEvent(BaseEvent):
+class CloseTabEvent(BaseEvent[None]):
"""Close a tab."""
tab_index: int
-class ScreenshotEvent(BaseEvent):
- """Request to take a screenshot."""
+ event_timeout: float | None = 10.0 # seconds
+
+class ScreenshotEvent(BaseEvent[bytes]):
+ """Request to take a screenshot. Returns screenshot as bytes."""
full_page: bool = False
clip: dict[str, float] | None = None # {x, y, width, height}
+
+
+ event_timeout: float | None = 15.0 # seconds
+
class BrowserStateRequestEvent(BaseEvent[BrowserStateSummary]):
@@ -112,6 +155,8 @@ class BrowserStateRequestEvent(BaseEvent[BrowserStateSummary]):
cache_clickable_elements_hashes: bool = True
include_recent_events: bool = False
+ event_timeout: float | None = 30.0 # seconds
+
# class WaitForConditionEvent(BaseEvent):
# """Wait for a condition."""
@@ -122,50 +167,58 @@ class BrowserStateRequestEvent(BaseEvent[BrowserStateSummary]):
# state: Literal['attached', 'detached', 'visible', 'hidden'] | None = None
-class GoBackEvent(BaseEvent):
+class GoBackEvent(BaseEvent[None]):
"""Navigate back in browser history."""
- pass
+ event_timeout: float | None = 15.0 # seconds
-class GoForwardEvent(BaseEvent):
+class GoForwardEvent(BaseEvent[None]):
"""Navigate forward in browser history."""
- pass
+ event_timeout: float | None = 15.0 # seconds
-class RefreshEvent(BaseEvent):
+class RefreshEvent(BaseEvent[None]):
"""Refresh/reload the current page."""
- pass
+ event_timeout: float | None = 15.0 # seconds
-class WaitEvent(BaseEvent):
+class WaitEvent(BaseEvent[None]):
"""Wait for a specified number of seconds."""
seconds: float = 3.0
max_seconds: float = 10.0 # Safety cap
+ event_timeout: float | None = 60.0 # seconds
+
-class SendKeysEvent(BaseEvent):
+class SendKeysEvent(BaseEvent[None]):
"""Send keyboard keys/shortcuts."""
keys: str # e.g., "ctrl+a", "cmd+c", "Enter"
+ event_timeout: float | None = 15.0 # seconds
+
-class UploadFileEvent(ElementSelectedEvent):
+class UploadFileEvent(ElementSelectedEvent[None]):
"""Upload a file to an element."""
node: 'EnhancedDOMTreeNode'
file_path: str
+ event_timeout: float | None = 30.0 # seconds
-class ScrollToTextEvent(BaseEvent):
- """Scroll to specific text on the page."""
+
+class ScrollToTextEvent(BaseEvent[None]):
+ """Scroll to specific text on the page. Raises exception if text not found."""
text: str
direction: Literal['up', 'down'] = 'down'
+ event_timeout: float | None = 15.0 # seconds
+
# ============================================================================
@@ -176,23 +229,35 @@ class BrowserStartEvent(BaseEvent):
cdp_url: str | None = None
launch_options: dict[str, Any] = Field(default_factory=dict)
+ event_timeout: float | None = 30.0 # seconds
+
class BrowserStopEvent(BaseEvent):
"""Stop/disconnect from browser."""
force: bool = False
+ event_timeout: float | None = 45.0 # seconds
+
+
+class BrowserLaunchResult(BaseModel):
+ """Result of launching a browser."""
+ # TODO: add browser executable_path, pid, version, latency, user_data_dir, X11 $DISPLAY, host IP address, etc.
+ cdp_url: str
-class BrowserLaunchEvent(BaseEvent[dict[str, str]]):
+
+class BrowserLaunchEvent(BaseEvent[BrowserLaunchResult]):
"""Launch a local browser process."""
- pass
+ # TODO: add executable_path, proxy settings, preferences, extra launch args, etc.
+
+ event_timeout: float | None = 30.0 # seconds
class BrowserKillEvent(BaseEvent):
"""Kill local browser subprocess."""
- pass
+ event_timeout: float | None = 30.0 # seconds
class ExecuteJavaScriptEvent(BaseEvent):
@@ -202,6 +267,8 @@ class ExecuteJavaScriptEvent(BaseEvent):
expression: str
await_promise: bool = True
+ event_timeout: float | None = 60.0 # seconds
+
class SetViewportEvent(BaseEvent):
"""Set the viewport size."""
@@ -210,18 +277,24 @@ class SetViewportEvent(BaseEvent):
height: int
device_scale_factor: float = 1.0
+ event_timeout: float | None = 15.0 # seconds
+
class SetCookiesEvent(BaseEvent):
"""Set browser cookies."""
cookies: list[dict[str, Any]]
+ event_timeout: float | None = 30.0 # only long to support the edge case of restoring a big localStorage / on many origins (has to O(n) visit each origin to restore)
+
class GetCookiesEvent(BaseEvent):
"""Get browser cookies."""
urls: list[str] | None = None
+ event_timeout: float | None = 30.0 # seconds
+
# ============================================================================
# DOM-related Events
@@ -233,18 +306,25 @@ class BrowserConnectedEvent(BaseEvent):
cdp_url: str
+ event_timeout: float | None = 30.0 # seconds
+
class BrowserStoppedEvent(BaseEvent):
"""Browser has stopped/disconnected."""
reason: str | None = None
+ event_timeout: float | None = 30.0 # seconds
+
class TabCreatedEvent(BaseEvent):
"""A new tab was created."""
tab_index: int
url: str
+ target_id: str
+
+ event_timeout: float | None = 30.0 # seconds
class TabClosedEvent(BaseEvent):
@@ -252,12 +332,20 @@ class TabClosedEvent(BaseEvent):
tab_index: int
+ # TODO:
+ # new_focus_tab_index: int | None = None
+ # new_focus_target_id: str | None = None
+ # new_focus_url: str | None = None
-class TabUpdatedEvent(BaseEvent):
- """Tab information updated (URL changed, etc.)."""
+ event_timeout: float | None = 10.0 # seconds
- tab_index: int
- url: str
+
+# TODO: emit this when DOM changes signficiantly, inner frame navigates, form submits, history.pushState(), etc.
+# class TabUpdatedEvent(BaseEvent):
+# """Tab information updated (URL changed, etc.)."""
+
+# tab_index: int
+# url: str
class AgentFocusChangedEvent(BaseEvent):
@@ -266,6 +354,8 @@ class AgentFocusChangedEvent(BaseEvent):
tab_index: int
url: str
+ event_timeout: float | None = 10.0 # seconds
+
class TargetCrashedEvent(BaseEvent):
"""A target has crashed."""
@@ -273,6 +363,8 @@ class TargetCrashedEvent(BaseEvent):
tab_index: int
error: str
+ event_timeout: float | None = 10.0 # seconds
+
class NavigationStartedEvent(BaseEvent):
"""Navigation started."""
@@ -280,6 +372,8 @@ class NavigationStartedEvent(BaseEvent):
tab_index: int
url: str
+ event_timeout: float | None = 30.0 # seconds
+
class NavigationCompleteEvent(BaseEvent):
"""Navigation completed."""
@@ -290,6 +384,8 @@ class NavigationCompleteEvent(BaseEvent):
error_message: str | None = None # Error/timeout message if navigation had issues
loading_status: str | None = None # Detailed loading status (e.g., network timeout info)
+ event_timeout: float | None = 30.0 # seconds
+
# ============================================================================
# Error Events
@@ -303,6 +399,8 @@ class BrowserErrorEvent(BaseEvent):
message: str
details: dict[str, Any] = Field(default_factory=dict)
+ event_timeout: float | None = 30.0 # seconds
+
# ============================================================================
# Storage State Events
@@ -314,6 +412,8 @@ class SaveStorageStateEvent(BaseEvent):
path: str | None = None # Optional path, uses profile default if not provided
+ event_timeout: float | None = 45.0 # seconds
+
class StorageStateSavedEvent(BaseEvent):
"""Notification that storage state was saved."""
@@ -322,13 +422,21 @@ class StorageStateSavedEvent(BaseEvent):
cookies_count: int
origins_count: int
+ event_timeout: float | None = 30.0 # seconds
+
class LoadStorageStateEvent(BaseEvent):
"""Request to load browser storage state."""
path: str | None = None # Optional path, uses profile default if not provided
+ event_timeout: float | None = 45.0 # seconds
+
+# TODO: refactor this to:
+# - on_BrowserConnectedEvent() -> dispatch(LoadStorageStateEvent()) -> _copy_storage_state_from_json_to_browser(json_file, new_cdp_session) + return storage_state from handler
+# - on_BrowserStopEvent() -> dispatch(SaveStorageStateEvent()) -> _copy_storage_state_from_browser_to_json(new_cdp_session, json_file)
+# and get rid of StorageStateSavedEvent and StorageStateLoadedEvent, have the original events + provide handler return values for any results
class StorageStateLoadedEvent(BaseEvent):
"""Notification that storage state was loaded."""
@@ -336,6 +444,8 @@ class StorageStateLoadedEvent(BaseEvent):
cookies_count: int
origins_count: int
+ event_timeout: float | None = 30.0 # seconds
+
# ============================================================================
# File Download Events
@@ -354,6 +464,8 @@ class FileDownloadedEvent(BaseEvent):
from_cache: bool = False
auto_download: bool = False # Whether this was an automatic download (e.g., PDF auto-download)
+ event_timeout: float | None = 30.0 # seconds
+
class AboutBlankDVDScreensaverShownEvent(BaseEvent):
"""AboutBlankWatchdog has shown DVD screensaver animation on an about:blank tab."""
@@ -362,6 +474,16 @@ class AboutBlankDVDScreensaverShownEvent(BaseEvent):
error: str | None = None
+class DialogOpenedEvent(BaseEvent):
+ """Event dispatched when a JavaScript dialog is opened and handled."""
+
+ dialog_type: str # 'alert', 'confirm', 'prompt', or 'beforeunload'
+ message: str
+ url: str
+ frame_id: str
+
+
+
# Note: Model rebuilding for forward references is handled in the importing modules
# Events with 'EnhancedDOMTreeNode' forward references (ClickElementEvent, TypeTextEvent,
# ScrollEvent, UploadFileEvent) need model_rebuild() called after imports are complete
diff --git a/browser_use/browser/local_browser_watchdog.py b/browser_use/browser/local_browser_watchdog.py
index 4238e71ac..2730fc5e1 100644
--- a/browser_use/browser/local_browser_watchdog.py
+++ b/browser_use/browser/local_browser_watchdog.py
@@ -13,6 +13,7 @@ from pydantic import PrivateAttr
from browser_use.browser.events import (
BrowserKillEvent,
BrowserLaunchEvent,
+ BrowserLaunchResult,
BrowserStopEvent,
)
from browser_use.browser.watchdog_base import BaseWatchdog
@@ -40,8 +41,9 @@ class LocalBrowserWatchdog(BaseWatchdog):
_temp_dirs_to_cleanup: list[Path] = PrivateAttr(default_factory=list)
_original_user_data_dir: str | None = PrivateAttr(default=None)
- async def on_BrowserLaunchEvent(self, event: BrowserLaunchEvent) -> dict[str, str]:
+ async def on_BrowserLaunchEvent(self, event: BrowserLaunchEvent) -> BrowserLaunchResult:
"""Launch a local browser process."""
+
try:
self.logger.info(
f'[LocalBrowserWatchdog] Received BrowserLaunchEvent, EventBus ID: {id(self.event_bus)}, launching local browser'
@@ -54,7 +56,7 @@ class LocalBrowserWatchdog(BaseWatchdog):
self._subprocess = process
self.logger.info(f'[LocalBrowserWatchdog] Browser launched successfully at {cdp_url}, PID: {process.pid}')
- return {'cdp_url': cdp_url}
+ return BrowserLaunchResult(cdp_url=cdp_url)
except Exception as e:
self.logger.error(f'[LocalBrowserWatchdog] Exception in on_BrowserLaunchEvent: {e}', exc_info=True)
raise
diff --git a/browser_use/browser/permissions_watchdog.py b/browser_use/browser/permissions_watchdog.py
new file mode 100644
index 000000000..9e2ff7982
--- /dev/null
+++ b/browser_use/browser/permissions_watchdog.py
@@ -0,0 +1,45 @@
+"""Permissions watchdog for granting browser permissions on connection."""
+
+from typing import TYPE_CHECKING, ClassVar
+
+from bubus import BaseEvent
+from cdp_use.cdp import browser as cdp_browser
+from cdp_use.cdp.browser import GrantPermissionsParameters
+
+from browser_use.browser.events import BrowserConnectedEvent
+from browser_use.browser.watchdog_base import BaseWatchdog
+
+if TYPE_CHECKING:
+ pass
+
+
+class PermissionsWatchdog(BaseWatchdog):
+ """Grants browser permissions when browser connects."""
+
+ # Event contracts
+ LISTENS_TO: ClassVar[list[type[BaseEvent]]] = [
+ BrowserConnectedEvent,
+ ]
+ EMITS: ClassVar[list[type[BaseEvent]]] = []
+
+ async def on_BrowserConnectedEvent(self, event: BrowserConnectedEvent) -> None:
+ """Grant permissions when browser connects."""
+ permissions = self.browser_session.browser_profile.permissions
+
+ if not permissions:
+ self.logger.debug('No permissions to grant')
+ return
+
+ self.logger.info(f'🔓 Granting browser permissions: {permissions}')
+
+ try:
+ # Grant permissions using CDP Browser.grantPermissions
+ # origin=None means grant to all origins
+ # Browser domain commands don't use session_id
+ await self.browser_session.cdp_client.send.Browser.grantPermissions(
+ params={'permissions': permissions} # type: ignore
+ )
+ self.logger.debug(f'✅ Successfully granted permissions: {permissions}')
+ except Exception as e:
+ self.logger.error(f'❌ Failed to grant permissions: {str(e)}')
+ # Don't raise - permissions are not critical to browser operation
\ No newline at end of file
diff --git a/browser_use/browser/popups_watchdog.py b/browser_use/browser/popups_watchdog.py
new file mode 100644
index 000000000..ebdef9e00
--- /dev/null
+++ b/browser_use/browser/popups_watchdog.py
@@ -0,0 +1,92 @@
+"""Watchdog for handling JavaScript dialogs (alert, confirm, prompt) automatically."""
+
+import asyncio
+from typing import ClassVar
+
+from bubus import BaseEvent
+from pydantic import PrivateAttr
+
+from browser_use.browser.events import AgentFocusChangedEvent, DialogOpenedEvent, TabCreatedEvent
+from browser_use.browser.watchdog_base import BaseWatchdog
+
+
+class PopupsWatchdog(BaseWatchdog):
+ """Handles JavaScript dialogs (alert, confirm, prompt) by automatically accepting them."""
+
+ # Events this watchdog listens to and emits
+ LISTENS_TO: ClassVar[list[type[BaseEvent]]] = [TabCreatedEvent, DialogOpenedEvent]
+ EMITS: ClassVar[list[type[BaseEvent]]] = [DialogOpenedEvent]
+
+ # Track which targets have dialog handlers registered
+ _dialog_listeners_registered: set[str] = PrivateAttr(default_factory=set)
+
+ def __init__(self, **kwargs):
+ super().__init__(**kwargs)
+ self.logger.info(f"🚀 PopupsWatchdog initialized with browser_session={self.browser_session}, ID={id(self)}")
+
+ async def on_TabCreatedEvent(self, event: TabCreatedEvent) -> None:
+ """Set up JavaScript dialog handling when a new tab is created."""
+ target_id = event.target_id
+ self.logger.info(f"🎯 PopupsWatchdog received TabCreatedEvent for target {target_id}")
+
+ # Skip if we've already registered for this target
+ if target_id in self._dialog_listeners_registered:
+ self.logger.debug(f"Already registered dialog handlers for target {target_id}")
+ return
+
+ self.logger.info(f"📌 Starting dialog handler setup for target {target_id}")
+ try:
+
+ # Set up handler for JavaScript dialogs
+ # CDP handlers are sync but we need to handle dialogs immediately to unblock the browser
+ def handle_dialog(event_data, _session_id: str | None=None):
+ self.browser_session.event_bus.dispatch(DialogOpenedEvent(
+ frame_id=event_data.get('frameId'),
+ dialog_type=event_data.get('type'),
+ message=event_data.get('message'),
+ url=event_data.get('url'),
+ ))
+
+ cdp_session = await self.browser_session.get_or_create_cdp_session(target_id, focus=False)
+ cdp_session.cdp_client.register.Page.javascriptDialogOpening(handle_dialog)
+ self.logger.info(f"✅ Successfully registered Page.javascriptDialogOpening handler for session {cdp_session.session_id}")
+
+ # Mark this target as having dialog handling set up
+ self._dialog_listeners_registered.add(target_id)
+
+ self.logger.info(f"✅ Set up JavaScript dialog handling for tab {target_id}")
+
+ except Exception as e:
+ self.logger.warning(f"Failed to set up dialog handling for tab {target_id}: {e}")
+
+ async def on_DialogOpenedEvent(self, event: DialogOpenedEvent) -> None:
+ """Handle the async closing of JavaScript dialogs."""
+ self.logger.info(f"📋 on_DialogOpenedEvent called with frame_id={event.frame_id} url={event.url} message={event.message}")
+
+ assert self.browser_session.agent_focus is not None, "Agent focus not set when handling DialogOpenedEvent"
+
+ cdp_session = await self.browser_session.cdp_client_for_frame(event.frame_id)
+ try:
+ # delay to look more human
+ await asyncio.sleep(0.25)
+ assert self.browser_session._cdp_client_root
+ await asyncio.wait_for(self.browser_session._cdp_client_root.send.Page.handleJavaScriptDialog(
+ params={'accept': True},
+ session_id=cdp_session.session_id,
+ ), timeout=5.0)
+ # CRITICAL: you must either wait for Page.javascriptDialogClosed or wait for some fixed time here
+ await asyncio.sleep(0.2)
+ assert (await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': '1+1'},
+ session_id=cdp_session.session_id
+ ))['result'].get('value') == 2, "Browser crashed after handling JS dialog popup"
+ self.logger.info("✅ JS dialog popup handled successfully")
+ # self.browser_session.agent_focus = await self.browser_session.get_or_create_cdp_session(current_focus.target_id, focus=True, new_socket=True)
+ # assert await self.browser_session.agent_focus.cdp_client.send.Page.getFrameTree(session_id=self.browser_session.agent_focus.session_id) is not None, "Agent focus not set after handling dialog"
+ except Exception as e:
+ self.logger.error(f"Failed to handle JavaScript dialog: {e}")
+ # finally:
+ # self.event_bus.dispatch(AgentFocusChangedEvent(
+ # tab_index=await self.browser_session.get_tab_index(current_focus.target_id),
+ # url=current_focus.url,
+ # ))
diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.py
index a82679729..21c9e0163 100644
--- a/browser_use/browser/profile.py
+++ b/browser_use/browser/profile.py
@@ -324,9 +324,9 @@ class BrowserContextArgs(BaseModel):
# Security options
proxy: ProxySettings | None = None
permissions: list[str] = Field(
- default_factory=lambda: ['clipboard-read', 'clipboard-write', 'notifications'],
- description='Browser permissions to grant (see playwright docs for valid permissions).',
- # clipboard is for google sheets and pyperclip automations
+ default_factory=lambda: ['clipboardReadWrite', 'notifications'],
+ description='Browser permissions to grant (CDP Browser.grantPermissions).',
+ # clipboardReadWrite is for google sheets and pyperclip automations
# notifications are to avoid browser fingerprinting
)
bypass_csp: bool = False
diff --git a/browser_use/browser/screenshot_watchdog.py b/browser_use/browser/screenshot_watchdog.py
index 3151209cc..e498d1258 100644
--- a/browser_use/browser/screenshot_watchdog.py
+++ b/browser_use/browser/screenshot_watchdog.py
@@ -21,14 +21,17 @@ class ScreenshotWatchdog(BaseWatchdog):
# Events this watchdog emits
EMITS: ClassVar[list[type[BaseEvent[Any]]]] = []
- async def on_ScreenshotEvent(self, event: ScreenshotEvent) -> dict[str, str | None]:
+ async def on_ScreenshotEvent(self, event: ScreenshotEvent) -> bytes:
"""Handle screenshot request using CDP.
Args:
event: ScreenshotEvent with optional full_page and clip parameters
Returns:
- Dict with 'screenshot' key containing base64-encoded screenshot or None
+ Screenshot as bytes (base64-encoded PNG data)
+
+ Raises:
+ Exception: If screenshot capture fails
"""
self.logger.debug('[ScreenshotWatchdog] Handler START - on_ScreenshotEvent called')
try:
@@ -54,7 +57,7 @@ class ScreenshotWatchdog(BaseWatchdog):
self.logger.debug(f'[ScreenshotWatchdog] Taking screenshot with params: {params}')
result = await cdp_session.cdp_client.send.Page.captureScreenshot(params=params, session_id=cdp_session.session_id)
- # Return base64-encoded screenshot data
+ # Return base64-encoded screenshot data as bytes
if result and 'data' in result:
self.logger.debug('[ScreenshotWatchdog] Screenshot captured successfully')
@@ -65,10 +68,12 @@ class ScreenshotWatchdog(BaseWatchdog):
except Exception as e:
self.logger.debug(f'[ScreenshotWatchdog] Failed to remove highlights: {e}')
- return {'screenshot': result['data']}
+ # Convert base64 string to bytes
+ import base64
+ return base64.b64decode(result['data'])
else:
self.logger.warning('[ScreenshotWatchdog] Screenshot result missing data')
- return {'screenshot': None}
+ raise Exception('Screenshot capture failed: no data returned')
except Exception as e:
self.logger.error(f'[ScreenshotWatchdog] Screenshot failed: {e}')
@@ -77,4 +82,4 @@ class ScreenshotWatchdog(BaseWatchdog):
await self.browser_session.remove_highlights()
except:
pass
- return {'screenshot': None}
+ raise
diff --git a/browser_use/browser/security_watchdog.py b/browser_use/browser/security_watchdog.py
index 484f66d32..4f51b4f3e 100644
--- a/browser_use/browser/security_watchdog.py
+++ b/browser_use/browser/security_watchdog.py
@@ -84,17 +84,14 @@ class SecurityWatchdog(BaseWatchdog):
BrowserErrorEvent(
error_type='TabCreationBlocked',
message=f'Tab created with non-allowed URL: {event.url}',
- details={'url': event.url, 'tab_index': event.tab_index},
+ details={'url': event.url, 'tab_index': event.tab_index, 'target_id': event.target_id},
)
)
# Try to close the offending tab
try:
- targets = await self.browser_session._cdp_get_all_pages()
- if 0 <= event.tab_index < len(targets):
- target_id = targets[event.tab_index]['targetId']
- await self.browser_session._cdp_close_page(target_id)
- self.logger.info(f'⛔️ Closed new tab with non-allowed URL: {event.url}')
+ await self.browser_session._cdp_close_page(event.target_id)
+ self.logger.info(f'⛔️ Closed new tab with non-allowed URL: {event.url}')
except Exception as e:
self.logger.error(f'⛔️ Failed to close new tab with non-allowed URL: {str(e)}')
@@ -177,4 +174,4 @@ class SecurityWatchdog(BaseWatchdog):
if host == pattern:
return True
- return False
\ No newline at end of file
+ return False
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 1fc7aa69a..becf83289 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -18,6 +18,7 @@ from browser_use.browser.events import (
BrowserConnectedEvent,
BrowserErrorEvent,
BrowserLaunchEvent,
+ BrowserLaunchResult,
BrowserStartEvent,
BrowserStateRequestEvent,
BrowserStopEvent,
@@ -60,7 +61,7 @@ class CDPSession(BaseModel):
owns_cdp_client: bool = False
@classmethod
- async def for_target(cls, cdp_client: CDPClient, target_id: str, new_socket: bool = False, cdp_url: str | None = None):
+ async def for_target(cls, cdp_client: CDPClient, target_id: str, new_socket: bool = False, cdp_url: str | None = None, domains: list[str] | None = None):
"""Create a CDP session for a target.
Args:
@@ -68,6 +69,7 @@ class CDPSession(BaseModel):
target_id: Target ID to attach to
new_socket: If True, create a dedicated WebSocket connection for this target
cdp_url: CDP URL (required if new_socket is True)
+ domains: List of CDP domains to enable. If None, enables default domains.
"""
if new_socket:
if not cdp_url:
@@ -95,9 +97,9 @@ class CDPSession(BaseModel):
session_id='connecting',
owns_cdp_client=False,
)
- return await cdp_session.attach()
+ return await cdp_session.attach(domains=domains)
- async def attach(self) -> Self:
+ async def attach(self, domains: list[str] | None = None) -> Self:
result = await self.cdp_client.send.Target.attachToTarget(
params={
'targetId': self.target_id,
@@ -110,18 +112,22 @@ class CDPSession(BaseModel):
)
self.session_id = result['sessionId']
+ # Use specified domains or default domains
+ domains = domains or ['Page', 'DOM', 'DOMSnapshot', 'Accessibility', 'Runtime', 'Inspector']
+
# Enable all domains in parallel
- await asyncio.gather(
- self.cdp_client.send.Page.enable(session_id=self.session_id),
- self.cdp_client.send.DOM.enable(session_id=self.session_id),
- self.cdp_client.send.DOMSnapshot.enable(session_id=self.session_id),
- self.cdp_client.send.Accessibility.enable(session_id=self.session_id),
- self.cdp_client.send.Runtime.enable(session_id=self.session_id),
- self.cdp_client.send.Inspector.enable(session_id=self.session_id),
- # TEMPORARILY DISABLED: Network.enable causes excessive event logging
- # self.cdp_client.send.Network.enable(session_id=self.session_id),
- return_exceptions=True, # Don't fail if some domains aren't available
- )
+ enable_tasks = []
+ for domain in domains:
+ # Get the enable method, e.g. self.cdp_client.send.Page.enable(session_id=self.session_id)
+ domain_api = getattr(self.cdp_client.send, domain, None)
+ # Browser and Target domains don't use session_id, dont pass it for those
+ enable_kwargs = {} if domain in ['Browser', 'Target'] else {'session_id': self.session_id}
+ assert domain_api and hasattr(domain_api, 'enable'), f'{domain_api} is not a recognized CDP domain with a .enable() method'
+ enable_tasks.append(domain_api.enable(**enable_kwargs))
+
+ results = await asyncio.gather(*enable_tasks, return_exceptions=True)
+ if any(isinstance(result, Exception) for result in results):
+ raise RuntimeError(f'Failed to enable requested CDP domain: {results}')
target_info = await self.get_target_info()
self.title = target_info['title']
self.url = target_info['url']
@@ -198,6 +204,7 @@ class BrowserSession(BaseModel):
_default_action_watchdog: Any | None = PrivateAttr(default=None)
_dom_watchdog: Any | None = PrivateAttr(default=None)
_screenshot_watchdog: Any | None = PrivateAttr(default=None)
+ _permissions_watchdog: Any | None = PrivateAttr(default=None)
_logger: Any = PrivateAttr(default=None)
@@ -249,6 +256,7 @@ class BrowserSession(BaseModel):
self._default_action_watchdog = None
self._dom_watchdog = None
self._screenshot_watchdog = None
+ self._permissions_watchdog = None
def model_post_init(self, __context) -> None:
"""Register event handlers after model initialization."""
@@ -270,6 +278,7 @@ class BrowserSession(BaseModel):
self.event_bus.on(NavigateToUrlEvent, self.on_NavigateToUrlEvent)
self.event_bus.on(SwitchTabEvent, self.on_SwitchTabEvent)
self.event_bus.on(TabClosedEvent, self.on_TabClosedEvent)
+ self.event_bus.on(AgentFocusChangedEvent, self.on_AgentFocusChangedEvent)
# Track downloaded files
self.event_bus.on(FileDownloadedEvent, self.on_FileDownloadedEvent)
@@ -279,6 +288,11 @@ class BrowserSession(BaseModel):
async def kill(self) -> None:
"""Kill the browser session and reset all state."""
+ # First save storage state while CDP is still connected
+ from browser_use.browser.events import SaveStorageStateEvent
+ save_event = self.event_bus.dispatch(SaveStorageStateEvent())
+ await save_event
+
# Dispatch stop event to kill the browser
await self.event_bus.dispatch(BrowserStopEvent(force=True))
# Stop the event bus
@@ -294,7 +308,15 @@ class BrowserSession(BaseModel):
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 BrowserStopEvent
+ # First save storage state while CDP is still connected
+ from browser_use.browser.events import SaveStorageStateEvent
+ save_event = self.event_bus.dispatch(SaveStorageStateEvent())
+ await save_event
+
+ # Now dispatch BrowserStopEvent to notify watchdogs
+ await self.event_bus.dispatch(BrowserStopEvent(force=False))
+
+ # Stop the event bus
await self.event_bus.stop(clear=True, timeout=5)
# Reset all state
await self.reset()
@@ -322,24 +344,23 @@ class BrowserSession(BaseModel):
await launch_event
# Get the CDP URL from LocalBrowserWatchdog handler result
- results = await launch_event.event_results_flat_dict(
- raise_if_none=True, raise_if_any=True, raise_if_conflicts=True
- )
- self.cdp_url = results.get('cdp_url')
-
- if not self.cdp_url:
- raise RuntimeError('LocalBrowserWatchdog failed to return a CDP URL for the launched browser')
+ launch_result: BrowserLaunchResult = cast(BrowserLaunchResult, await launch_event.event_result(raise_if_none=True, raise_if_any=True))
+ self.cdp_url = launch_result.cdp_url
else:
raise ValueError('Got BrowserSession(is_local=False) but no cdp_url was provided to connect to!')
assert self.cdp_url and '://' in self.cdp_url
- # Setup browser via CDP (for both local and remote cases)
- await self.connect(cdp_url=self.cdp_url)
- assert self.cdp_client is not None
+ # Only connect if not already connected
+ if self._cdp_client_root is None:
+ # Setup browser via CDP (for both local and remote cases)
+ await self.connect(cdp_url=self.cdp_url)
+ assert self.cdp_client is not None
- # Notify that browser is connected (single place)
- self.event_bus.dispatch(BrowserConnectedEvent(cdp_url=self.cdp_url))
+ # Notify that browser is connected (single place)
+ self.event_bus.dispatch(BrowserConnectedEvent(cdp_url=self.cdp_url))
+ else:
+ self.logger.debug('Already connected to CDP, skipping reconnection')
# Return the CDP URL for other components
return {'cdp_url': self.cdp_url}
@@ -356,6 +377,7 @@ class BrowserSession(BaseModel):
async def on_NavigateToUrlEvent(self, event: NavigateToUrlEvent) -> None:
"""Handle navigation requests - core browser functionality."""
+ self.logger.info(f'[on_NavigateToUrlEvent] Received NavigateToUrlEvent: url={event.url}, new_tab={event.new_tab}')
if not self.agent_focus:
self.logger.warning('Cannot navigate - browser not connected')
return
@@ -365,12 +387,16 @@ class BrowserSession(BaseModel):
target_id = None
tab_index = 0
+ self.logger.info(f'[on_NavigateToUrlEvent] Processing new_tab={event.new_tab}')
if event.new_tab:
# Look for existing about:blank tab that's not the current one
targets = await self._cdp_get_all_pages()
+ self.logger.info(f'[on_NavigateToUrlEvent] Found {len(targets)} existing tabs')
current_target_id = self.agent_focus.target_id if self.agent_focus else None
+ self.logger.info(f'[on_NavigateToUrlEvent] Current target_id: {current_target_id}')
for idx, target in enumerate(targets):
+ self.logger.debug(f'[on_NavigateToUrlEvent] Tab {idx}: url={target.get("url")}, targetId={target["targetId"]}')
if target.get('url') == 'about:blank' and target['targetId'] != current_target_id:
target_id = target['targetId']
tab_index = idx
@@ -379,46 +405,43 @@ class BrowserSession(BaseModel):
# Create new tab if no reusable one found
if not target_id:
- target_id = await self._cdp_create_new_page('about:blank')
- targets = await self._cdp_get_all_pages()
- tab_index = len(targets) - 1
- self.logger.info(f'Created new tab at index {tab_index}')
- # Dispatch TabCreatedEvent for new tab
- self.event_bus.dispatch(TabCreatedEvent(tab_index=tab_index, url='about:blank'))
+ self.logger.info('[on_NavigateToUrlEvent] No reusable about:blank tab found, creating new tab...')
+ try:
+ target_id = await self._cdp_create_new_page('about:blank')
+ self.logger.info(f'[on_NavigateToUrlEvent] Created new page with target_id: {target_id}')
+ targets = await self._cdp_get_all_pages()
+ tab_index = len(targets) - 1
+ self.logger.info(f'Created new tab at index {tab_index}')
+ # Dispatch TabCreatedEvent for new tab
+ await self.event_bus.dispatch(TabCreatedEvent(tab_index=tab_index, url='about:blank', target_id=target_id))
+ except Exception as e:
+ self.logger.error(f'[on_NavigateToUrlEvent] Failed to create new tab: {e}')
+ # Fall back to using current tab
+ target_id = self.agent_focus.target_id
+ tab_index = await self.get_tab_index(target_id)
+ self.logger.warning(f'[on_NavigateToUrlEvent] Falling back to current tab at index {tab_index}')
else:
# Use current tab
target_id = self.agent_focus.target_id
tab_index = await self.get_tab_index(target_id)
# Activate target (bring to foreground)
- await self.cdp_client.send.Target.activateTarget(params={'targetId': target_id})
+ await self.event_bus.dispatch(SwitchTabEvent(tab_index=tab_index))
# Update agent focus to the target
self.agent_focus = await self.get_or_create_cdp_session(target_id)
- # Clear any cached state since we're navigating
- if self._dom_watchdog:
- self._dom_watchdog.clear_cache()
-
- # Dispatch focus changed event
- self.event_bus.dispatch(
- AgentFocusChangedEvent(
- tab_index=tab_index,
- url=event.url,
- )
- )
-
# Dispatch navigation started
- self.event_bus.dispatch(
- NavigationStartedEvent(
- tab_index=tab_index,
- url=event.url,
- )
- )
+ await self.event_bus.dispatch(NavigationStartedEvent(tab_index=tab_index, url=event.url))
# Navigate to URL
await self.agent_focus.cdp_client.send.Page.navigate(
- params={'url': event.url}, session_id=self.agent_focus.session_id
+ params={
+ 'url': event.url,
+ 'transitionType': 'address_bar',
+ # 'referrer': 'https://www.google.com',
+ },
+ session_id=self.agent_focus.session_id
)
# Wait a bit to ensure page starts loading
@@ -426,13 +449,14 @@ class BrowserSession(BaseModel):
# Dispatch navigation complete
self.logger.debug(f'Dispatching NavigationCompleteEvent for {event.url} (tab_index={tab_index})')
- self.event_bus.dispatch(
+ await self.event_bus.dispatch(
NavigationCompleteEvent(
tab_index=tab_index,
url=event.url,
status=None, # CDP doesn't provide status directly
)
)
+ await self.event_bus.dispatch(AgentFocusChangedEvent(tab_index=tab_index, url=event.url))
# Note: These should be handled by dedicated watchdogs:
# - Security checks (security_watchdog)
@@ -459,22 +483,15 @@ class BrowserSession(BaseModel):
return
targets = await self._cdp_get_all_pages()
- if 0 <= event.tab_index < len(targets):
+ if -len(targets) <= event.tab_index < len(targets):
target_id = targets[event.tab_index]['targetId']
# Activate target
await self.cdp_client.send.Target.activateTarget(params={'targetId': target_id})
- # Update agent focus
- self.agent_focus = await self.get_or_create_cdp_session(target_id)
-
- # Clear cached state when switching tabs
- if self._dom_watchdog:
- self._dom_watchdog.clear_cache()
-
# Dispatch focus changed event
target_url = targets[event.tab_index].get('url', '')
- self.event_bus.dispatch(
+ await self.event_bus.dispatch(
AgentFocusChangedEvent(
tab_index=event.tab_index,
url=target_url,
@@ -500,14 +517,12 @@ class BrowserSession(BaseModel):
# Try to stay at same index or go to previous
new_index = min(current_tab_index, len(targets) - 1)
if new_index >= 0:
- new_target_id = targets[new_index]['targetId']
- self.agent_focus = await self.get_or_create_cdp_session(new_target_id)
-
# Dispatch focus changed
- self.event_bus.dispatch(
+ await self.event_bus.dispatch(
AgentFocusChangedEvent(
tab_index=new_index,
url=targets[new_index].get('url', ''),
+ # target_id=new_target_id,
)
)
self.logger.info(f'Moved focus to tab {new_index} after tab closure')
@@ -516,6 +531,51 @@ class BrowserSession(BaseModel):
else:
self.agent_focus = None
+ async def on_AgentFocusChangedEvent(self, event: AgentFocusChangedEvent) -> None:
+ """Handle agent focus change - update focus and clear cache."""
+ self.logger.debug(f'🔄 AgentFocusChangedEvent received: tab_index={event.tab_index}, url={event.url}')
+
+ # Clear cached DOM state since focus changed
+ self.logger.debug('🔄 Clearing DOM cache...')
+ if self._dom_watchdog:
+ self._dom_watchdog.clear_cache()
+ self.logger.debug('🔄 Cleared DOM cache after focus change')
+
+ # Clear cached browser state
+ self.logger.debug('🔄 Clearing cached browser state...')
+ self._cached_browser_state_summary = None
+ self._cached_selector_map.clear()
+ self.logger.debug('🔄 Cached browser state cleared')
+
+ # Update agent focus if tab_index is provided
+ if event.tab_index is not None:
+ self.logger.debug(f'🔄 Getting all pages for tab_index={event.tab_index}...')
+ targets = await self._cdp_get_all_pages()
+ self.logger.debug(f'🔄 Got {len(targets)} targets')
+ if 0 <= event.tab_index < len(targets):
+ target_id = targets[event.tab_index]['targetId']
+ self.logger.debug(f'🔄 Getting CDP session for target {target_id}...')
+ self.agent_focus = await self.get_or_create_cdp_session(target_id, focus=True)
+ self.logger.debug(f'🔄 Updated agent focus to tab {event.tab_index} (target {target_id})')
+
+ # Test that the browser is responsive by evaluating a simple expression
+ if self.agent_focus:
+ self.logger.debug('🔄 Testing browser responsiveness...')
+ try:
+ test_result = await asyncio.wait_for(self.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={'expression': '1 + 1', 'returnByValue': True},
+ session_id=self.agent_focus.session_id
+ ), timeout=2.0)
+ if test_result.get('result', {}).get('value') == 2:
+ self.logger.debug('🔄 ✅ Browser is responsive after focus change')
+ else:
+ raise Exception('❌ Failed to execute test JS expression with Page.evaluate')
+ except Exception as e:
+ self.logger.error(f'🔄 ❌ Page appears crashed after focus change: {e}')
+ raise
+
+ self.logger.debug('🔄 AgentFocusChangedEvent handler completed successfully')
+
async def on_FileDownloadedEvent(self, event: FileDownloadedEvent) -> None:
"""Track downloaded files during this session."""
self.logger.debug(f'FileDownloadedEvent received: {event.file_name} at {event.path}')
@@ -565,14 +625,16 @@ class BrowserSession(BaseModel):
return self._cdp_client_root
async def get_or_create_cdp_session(
- self, target_id: str | None = None, focus: bool = True, new_socket: bool | None = None
+ self, target_id: str | None = None, focus: bool = True, new_socket: bool | None = None, domains: list[str] | 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.
+ target_id: Target ID to get session for. If None, uses current agent focus.
+ If 'main', finds any about:blank target to use.
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. If None (default), creates new socket for new targets only.
+ domains: List of CDP domains to enable (e.g., ['Browser', 'Storage']). If None, enables default domains.
Returns:
CDPSession for the specified target.
@@ -581,54 +643,56 @@ class BrowserSession(BaseModel):
assert self._cdp_client_root is not None, 'Root 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'
- # If no target_id specified, use the current target_id
- if target_id is None:
+ target_id = target_id or self.agent_focus.target_id
+
+ if target_id == 'main' and self.agent_focus.target_id:
target_id = self.agent_focus.target_id
+ elif target_id == 'main' and not self.agent_focus.target_id:
+ all_targets = await self._cdp_client_root.send.Target.getTargets()
+ spare_bg_target = [target for target in all_targets.get('targetInfos', []) if target.get('type') == 'page' and target.get('url') == 'about:blank']
+ if spare_bg_target:
+ target_id = spare_bg_target[0]['targetId']
+
+ if target_id is None:
+ assert self._cdp_client_root is not None, 'Root CDP client not initialized - browser may not be connected yet'
+ result = await self._cdp_client_root.send.Target.createTarget(params={'url': 'about:blank'})
+ target_id = result['targetId']
+ self.logger.debug(f"[get_or_create_cdp_session] Created new target {target_id} for 'main' request with domains={domains}")
+
+ assert target_id is not None, 'No target_id provided and no about:blank tab or open page found'
- # Check if we already have a session for this target in the pool
- if target_id in self._cdp_session_pool:
- session = self._cdp_session_pool[target_id]
- if focus and self.agent_focus.target_id != target_id:
- self.logger.debug(
- f'[get_or_create_cdp_session] Switching agent focus from {self.agent_focus.target_id} to {target_id}'
+ if new_socket:
+ session = await CDPSession.for_target(
+ self._cdp_client_root,
+ target_id=target_id,
+ new_socket=new_socket,
+ cdp_url=self.cdp_url,
+ domains=domains,
+ )
+ else:
+ session = self._cdp_session_pool.get(target_id)
+ if session is None:
+ session = await CDPSession.for_target(
+ self._cdp_client_root,
+ target_id=target_id,
+ new_socket=True,
+ cdp_url=self.cdp_url,
+ domains=domains,
)
- self.agent_focus = session
- else:
- self.logger.debug(f'[get_or_create_cdp_session] Reusing existing session for {target_id} (focus={focus})')
- return session
-
- # If it's the current focus target, return that session
- if self.agent_focus.target_id == target_id:
- self._cdp_session_pool[target_id] = self.agent_focus
- return self.agent_focus
-
- # Create new session for this target
- # 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=should_use_new_socket,
- cdp_url=self.cdp_url if should_use_new_socket else None,
- )
- self._cdp_session_pool[target_id] = session
+ self._cdp_session_pool[target_id] = session
+
+ assert session is not None, 'Failed to create CDP session for target_id'
- # Only change agent focus if requested
if focus:
- self.logger.debug(
- f'[get_or_create_cdp_session] Switching agent focus from {self.agent_focus.target_id} to {target_id}'
- )
self.agent_focus = session
+ self._cdp_session_pool[target_id] = session
else:
- self.logger.debug(
- f'[get_or_create_cdp_session] Created session for {target_id} without changing focus (still on {self.agent_focus.target_id})'
- )
+ if not new_socket:
+ self._cdp_session_pool[target_id] = session
return session
+
@property
def current_target_id(self) -> str | None:
return self.agent_focus.target_id if self.agent_focus else None
@@ -674,7 +738,7 @@ class BrowserSession(BaseModel):
# The handler returns the BrowserStateSummary directly
result = await event.event_result(raise_if_none=True, raise_if_any=True)
- assert result is not None
+ assert result is not None and result.dom_state is not None
return result
async def attach_all_watchdogs(self) -> None:
@@ -685,21 +749,23 @@ class BrowserSession(BaseModel):
return
from browser_use.browser.aboutblank_watchdog import AboutBlankWatchdog
- from browser_use.browser.crash_watchdog import CrashWatchdog
+ # from browser_use.browser.crash_watchdog import CrashWatchdog
from browser_use.browser.default_action_watchdog import DefaultActionWatchdog
from browser_use.browser.dom_watchdog import DOMWatchdog
from browser_use.browser.downloads_watchdog import DownloadsWatchdog
from browser_use.browser.local_browser_watchdog import LocalBrowserWatchdog
+ from browser_use.browser.permissions_watchdog import PermissionsWatchdog
+ from browser_use.browser.popups_watchdog import PopupsWatchdog
from browser_use.browser.screenshot_watchdog import ScreenshotWatchdog
from browser_use.browser.security_watchdog import SecurityWatchdog
from browser_use.browser.storage_state_watchdog import StorageStateWatchdog
# Initialize CrashWatchdog
- CrashWatchdog.model_rebuild()
- self._crash_watchdog = CrashWatchdog(event_bus=self.event_bus, browser_session=self)
+ # CrashWatchdog.model_rebuild()
+ # self._crash_watchdog = CrashWatchdog(event_bus=self.event_bus, browser_session=self)
# self.event_bus.on(BrowserConnectedEvent, self._crash_watchdog.on_BrowserConnectedEvent)
# self.event_bus.on(BrowserStoppedEvent, self._crash_watchdog.on_BrowserStoppedEvent)
- await self._crash_watchdog.attach_to_session()
+ # await self._crash_watchdog.attach_to_session()
# Initialize DownloadsWatchdog
DownloadsWatchdog.model_rebuild()
@@ -746,6 +812,19 @@ class BrowserSession(BaseModel):
# self.event_bus.on(TabClosedEvent, self._aboutblank_watchdog.on_TabClosedEvent)
await self._aboutblank_watchdog.attach_to_session()
+ # Initialize PopupsWatchdog
+ PopupsWatchdog.model_rebuild()
+ self._popups_watchdog = PopupsWatchdog(event_bus=self.event_bus, browser_session=self)
+ # self.event_bus.on(TabCreatedEvent, self._popups_watchdog.on_TabCreatedEvent)
+ # self.event_bus.on(DialogCloseEvent, self._popups_watchdog.on_DialogCloseEvent)
+ await self._popups_watchdog.attach_to_session()
+
+ # Initialize PermissionsWatchdog
+ PermissionsWatchdog.model_rebuild()
+ self._permissions_watchdog = PermissionsWatchdog(event_bus=self.event_bus, browser_session=self)
+ # self.event_bus.on(BrowserConnectedEvent, self._permissions_watchdog.on_BrowserConnectedEvent)
+ await self._permissions_watchdog.attach_to_session()
+
# Initialize DefaultActionWatchdog
DefaultActionWatchdog.model_rebuild()
self._default_action_watchdog = DefaultActionWatchdog(event_bus=self.event_bus, browser_session=self)
@@ -836,6 +915,7 @@ class BrowserSession(BaseModel):
# Collect all targets that need redirection
redirected_targets = []
+ redirect_sessions = {} # Store sessions created for redirection to potentially reuse
for target in page_targets:
target_url = target.get('url', '')
if is_new_tab_page(target_url) and target_url != 'about:blank':
@@ -843,13 +923,15 @@ class BrowserSession(BaseModel):
target_id = target['targetId']
self.logger.info(f'🔄 Redirecting {target_url} to about:blank for target {target_id}')
try:
- # Create a temporary CDP session for this target
- temp_session = await CDPSession.for_target(self._cdp_client_root, target_id)
+ # Create a CDP session for redirection (minimal domains to avoid duplicate event handlers)
+ # Only enable Page domain for navigation, avoid duplicate event handlers
+ redirect_session = await CDPSession.for_target(self._cdp_client_root, target_id, domains=['Page'])
# Navigate to about:blank
- await temp_session.cdp_client.send.Page.navigate(
- params={'url': 'about:blank'}, session_id=temp_session.session_id
+ await redirect_session.cdp_client.send.Page.navigate(
+ params={'url': 'about:blank'}, session_id=redirect_session.session_id
)
redirected_targets.append(target_id)
+ redirect_sessions[target_id] = redirect_session # Store for potential reuse
# Update the target's URL in our list for later use
target['url'] = 'about:blank'
# Small delay to ensure navigation completes
@@ -868,17 +950,26 @@ class BrowserSession(BaseModel):
self.logger.info(f'📄 Created new blank page with target ID: {target_id}')
else:
# Use the first available page
- target_id = page_targets[0]['targetId']
+ target_id = [page for page in page_targets if page.get('type') == 'page'][0]['targetId']
self.logger.info(f'📄 Using existing page with target ID: {target_id}')
# Store the current page target ID and add to pool
- # 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
+ # Reuse redirect session if available, otherwise create new one
+ if target_id in redirect_sessions:
+ self.logger.debug(f'Reusing redirect session for target {target_id}')
+ self.agent_focus = redirect_sessions[target_id]
+ else:
+ # 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)
+ if self.agent_focus:
+ self._cdp_session_pool[target_id] = self.agent_focus
# Verify the session is working
try:
- assert self.agent_focus.title != 'Unknown title'
+ if self.agent_focus:
+ assert self.agent_focus.title != 'Unknown title'
+ else:
+ raise RuntimeError('Failed to create CDP session')
except Exception as e:
self.logger.warning(f'Failed to create CDP session: {e}')
raise
@@ -888,7 +979,7 @@ class BrowserSession(BaseModel):
for idx, target in enumerate(page_targets):
target_url = target.get('url', '')
self.logger.debug(f'Dispatching TabCreatedEvent for initial tab {idx}: {target_url}')
- self.event_bus.dispatch(TabCreatedEvent(tab_index=idx, url=target_url))
+ self.event_bus.dispatch(TabCreatedEvent(tab_index=idx, url=target_url, target_id=target['targetId']))
# Dispatch initial focus event
if page_targets:
@@ -1070,11 +1161,6 @@ class BrowserSession(BaseModel):
and element.attributes.get('type', '').lower() == 'file'
)
- def clear_dom_cache(self) -> None:
- """Clear cached DOM state to force rebuild on next access."""
- if self._dom_watchdog:
- self._dom_watchdog.clear_cache()
-
async def get_selector_map(self) -> dict[int, EnhancedDOMTreeNode]:
"""Get the current selector map from cached state or DOM watchdog.
@@ -1193,9 +1279,14 @@ class BrowserSession(BaseModel):
)
]
- async def _cdp_create_new_page(self, url: str = 'about:blank') -> str:
+ async def _cdp_create_new_page(self, url: str = 'about:blank', background: bool=False, new_window: bool=False) -> str:
"""Create a new page/tab using CDP Target.createTarget. Returns target ID."""
- result = await self.cdp_client.send.Target.createTarget(params={'url': url, 'newWindow': False, 'background': False})
+ # Use the root CDP client to create tabs at the browser level
+ if self._cdp_client_root:
+ result = await self._cdp_client_root.send.Target.createTarget(params={'url': url, 'newWindow': new_window, 'background': background})
+ else:
+ # Fallback to using cdp_client if root is not available
+ result = await self.cdp_client.send.Target.createTarget(params={'url': url, 'newWindow': new_window, 'background': background})
return result['targetId']
async def _cdp_close_page(self, target_id: str) -> None:
@@ -1408,9 +1499,8 @@ class BrowserSession(BaseModel):
if cdp_session:
target_sessions[target_id] = cdp_session.session_id
- try:
- # Try to get frame tree (not all target types support this)
try:
+ # Try to get frame tree (not all target types support this)
frame_tree_result = await cdp_session.cdp_client.send.Page.getFrameTree(session_id=cdp_session.session_id)
# Process the frame tree recursively
@@ -1484,13 +1574,9 @@ class BrowserSession(BaseModel):
# Process the entire frame tree
process_frame_tree(frame_tree_result.get('frameTree', {}))
- except Exception:
+ except Exception as e:
# Target doesn't support Page domain or has no frames
- pass
-
- except Exception:
- # Error processing this target
- pass
+ self.logger.debug(f'Failed to get frame tree for target {target_id}: {e}')
# Second pass: populate backend node IDs and parent target IDs
# Only do this if cross-origin support is enabled
@@ -1621,6 +1707,8 @@ _watchdog_modules = [
'browser_use.browser.storage_state_watchdog.StorageStateWatchdog',
'browser_use.browser.security_watchdog.SecurityWatchdog',
'browser_use.browser.aboutblank_watchdog.AboutBlankWatchdog',
+ 'browser_use.browser.popups_watchdog.PopupsWatchdog',
+ 'browser_use.browser.permissions_watchdog.PermissionsWatchdog',
'browser_use.browser.default_action_watchdog.DefaultActionWatchdog',
'browser_use.browser.dom_watchdog.DOMWatchdog',
'browser_use.browser.screenshot_watchdog.ScreenshotWatchdog',
diff --git a/browser_use/browser/storage_state_watchdog.py b/browser_use/browser/storage_state_watchdog.py
index f8b950a0b..902e93c19 100644
--- a/browser_use/browser/storage_state_watchdog.py
+++ b/browser_use/browser/storage_state_watchdog.py
@@ -12,7 +12,6 @@ from pydantic import Field, PrivateAttr
from browser_use.browser.events import (
BrowserConnectedEvent,
- BrowserStopEvent,
LoadStorageStateEvent,
SaveStorageStateEvent,
StorageStateLoadedEvent,
@@ -27,7 +26,6 @@ class StorageStateWatchdog(BaseWatchdog):
# Event contracts
LISTENS_TO: ClassVar[list[type[BaseEvent]]] = [
BrowserConnectedEvent,
- BrowserStopEvent,
SaveStorageStateEvent,
LoadStorageStateEvent,
]
@@ -55,21 +53,6 @@ class StorageStateWatchdog(BaseWatchdog):
# Automatically load storage state after browser start
self.event_bus.dispatch(LoadStorageStateEvent())
- async def on_BrowserStopEvent(self, event: BrowserStopEvent) -> None:
- """Stop monitoring and save state when browser stop is requested."""
- self.logger.info('[StorageStateWatchdog] Browser stop requested, saving final storage_state.json')
-
- # Save storage state before stopping and wait for completion
- # self.logger.info('[StorageStateWatchdog] Dispatching SaveStorageStateEvent...')
- save_event = self.event_bus.dispatch(SaveStorageStateEvent())
- # self.logger.info('[StorageStateWatchdog] Waiting for save event to complete...')
- await save_event
- # self.logger.info('[StorageStateWatchdog] Save event completed, stopping monitoring...')
-
- # Stop monitoring
- await self._stop_monitoring()
- # self.logger.info('[StorageStateWatchdog] Browser stop handling complete')
- # No cleanup needed - browser context is managed by session
async def on_SaveStorageStateEvent(self, event: SaveStorageStateEvent) -> None:
"""Handle storage state save request."""
@@ -178,7 +161,8 @@ class StorageStateWatchdog(BaseWatchdog):
# Check if CDP client is available
try:
# Try to get a CDP session with a new socket for isolation during shutdown
- await self.browser_session.get_or_create_cdp_session(new_socket=True)
+ # Use 'main' to get any about:blank target, only enable Storage domain to avoid duplicate Page events
+ await self.browser_session.get_or_create_cdp_session(target_id='main', new_socket=True, domains=['Storage'])
except (AssertionError, RuntimeError, Exception):
self.logger.debug('[StorageStateWatchdog] CDP client already disconnected, skipping save')
return
diff --git a/browser_use/browser/views.py b/browser_use/browser/views.py
index f508d0093..700369738 100644
--- a/browser_use/browser/views.py
+++ b/browser_use/browser/views.py
@@ -4,6 +4,7 @@ from typing import Any
from pydantic import BaseModel
from browser_use.dom.views import DOMInteractedElement, SerializedDOMState
+from bubus import BaseEvent
# Known placeholder image data for about:blank pages - a 4x4 white PNG
PLACEHOLDER_4PX_SCREENSHOT = (
@@ -113,6 +114,22 @@ class BrowserStateHistory:
class BrowserError(Exception):
"""Base class for all browser errors"""
+ message: str
+ details: dict[str, Any] | None = None
+ while_handling_event: BaseEvent[Any] | None = None
+
+ def __init__(self, message: str, details: dict[str, Any] | None = None, event: BaseEvent[Any] | None = None):
+ self.message = message
+ super().__init__(message)
+ self.details = details
+ self.while_handling_event = event
+
+ def __str__(self) -> str:
+ if self.details:
+ return f'{self.message} ({self.details}) during: {self.while_handling_event}'
+ else:
+ return f'{self.message} (while handling event: {self.while_handling_event})'
+
class URLNotAllowedError(BrowserError):
"""Error raised when a URL is not allowed"""
diff --git a/browser_use/cli.py b/browser_use/cli.py
index 7b24d0494..deba5dbbf 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -475,12 +475,12 @@ class BrowserUseApp(App):
log_handler.setLevel('RESULT')
log_handler.setFormatter(BrowserUseFormatter('%(message)s'))
else:
- log_handler.setFormatter(BrowserUseFormatter('%(message)s'))
+ log_handler.setFormatter(BrowserUseFormatter('%(levelname)-8s [%(name)s] %(message)s'))
# Configure root logger - Replace ALL handlers, not just stdout handlers
root = logging.getLogger()
- # Clear all existing handlers and add only our richlog handler
+ # Clear all existing handlers to prevent output to stdout/stderr
root.handlers = []
root.addHandler(log_handler)
@@ -514,7 +514,7 @@ class BrowserUseApp(App):
logger.handlers = [log_handler]
logger.setLevel(root.level)
- # Silence third-party loggers
+ # Silence third-party loggers but keep them using our handler
for logger_name in [
'WDM',
'httpx',
@@ -529,11 +529,14 @@ class BrowserUseApp(App):
'PIL.PngImagePlugin',
'trafilatura.htmlprocessing',
'trafilatura',
+ 'groq',
+ 'portalocker',
+ 'portalocker.utils',
]:
third_party = logging.getLogger(logger_name)
third_party.setLevel(logging.ERROR)
third_party.propagate = False
- third_party.handlers = [] # Clear any existing handlers
+ third_party.handlers = [log_handler] # Use our handler to prevent stdout/stderr leakage
def on_mount(self) -> None:
"""Set up components when app is mounted."""
@@ -1064,76 +1067,258 @@ class BrowserUseApp(App):
def update_browser_panel(self) -> None:
"""Update browser information panel with details about the browser."""
- try:
- browser_info = self.query_one('#browser-info', RichLog)
- browser_info.clear()
-
- # Try to use the agent's browser session if available
- browser_session = self.browser_session
- if hasattr(self, 'agent') and self.agent and hasattr(self.agent, 'browser_session'):
- browser_session = self.agent.browser_session
-
- if browser_session:
- # Get browser state info
- cdp_url = browser_session.cdp_url or 'Not connected'
- is_local = browser_session.is_local
- browser_type = 'Local' if is_local else 'Remote'
-
- # Format CDP URL for display
- if cdp_url != 'Not connected':
- port = cdp_url.split(':')[-1].split('/')[0] if ':' in cdp_url else 'unknown'
- display_url = f'Port {port}'
- else:
- display_url = cdp_url
+ browser_info = self.query_one('#browser-info', RichLog)
+ browser_info.clear()
- browser_info.write('[bold]🌐 Browser[/bold]')
- browser_info.write(f'Type: {browser_type}')
- browser_info.write(f'CDP: {display_url}')
- else:
- browser_info.write('[dim]No browser session[/dim]')
- except Exception as e:
- logging.debug(f'Error updating browser panel: {e}')
+ # Try to use the agent's browser session if available
+ browser_session = self.browser_session
+ if hasattr(self, 'agent') and self.agent and hasattr(self.agent, 'browser_session'):
+ browser_session = self.agent.browser_session
- def update_model_panel(self) -> None:
- """Update model information panel."""
- try:
- model_info = self.query_one('#model-info', RichLog)
- model_info.clear()
+ if browser_session:
+ try:
+ # Check if browser session has a CDP client
+ if not hasattr(browser_session, 'cdp_client') or browser_session.cdp_client is None:
+ browser_info.write('[yellow]Browser session created, waiting for browser to launch...[/]')
+ return
+
+ # Update our reference if we're using the agent's session
+ if browser_session != self.browser_session:
+ self.browser_session = browser_session
+
+ # Get basic browser info from browser_profile
+ browser_type = 'Chromium'
+ headless = browser_session.browser_profile.headless
+
+ # Determine connection type based on config
+ connection_type = 'playwright' # Default
+ if browser_session.cdp_url:
+ connection_type = 'CDP'
+ elif browser_session.browser_profile.executable_path:
+ connection_type = 'user-provided'
+
+ # Get window size details from browser_profile
+ window_width = None
+ window_height = None
+ if browser_session.browser_profile.viewport:
+ window_width = browser_session.browser_profile.viewport.get('width')
+ window_height = browser_session.browser_profile.viewport.get('height')
+
+ # Try to get browser PID
+ browser_pid = 'Unknown'
+ connected = False
+ browser_status = '[red]Disconnected[/]'
- if self.llm:
- model_name = getattr(self.llm, 'model', 'Unknown')
- provider = getattr(self.llm, '__class__', type(self.llm)).__name__.replace('Chat', '')
+ try:
+ # Check if browser PID is available
+ # Check if we have a CDP client
+ if browser_session.cdp_client is not None:
+ connected = True
+ browser_status = '[green]Connected[/]'
+ browser_pid = 'N/A'
+ except Exception as e:
+ browser_pid = f'Error: {str(e)}'
+
+ # Display browser information
+ browser_info.write(f'[bold cyan]Chromium[/] Browser ({browser_status})')
+ browser_info.write(
+ f'Type: [yellow]{connection_type}[/] [{"green" if not headless else "red"}]{" (headless)" if headless else ""}[/]'
+ )
+ browser_info.write(f'PID: [dim]{browser_pid}[/]')
+ browser_info.write(f'CDP Port: {browser_session.cdp_url}')
+
+ if window_width and window_height:
+ browser_info.write(f'Window: [blue]{window_width}[/] × [blue]{window_height}[/]')
+
+ # Include additional information about the browser if needed
+ if connected and hasattr(self, 'agent') and self.agent:
+ try:
+ # Show when the browser was connected
+ timestamp = int(time.time())
+ current_time = time.strftime('%H:%M:%S', time.localtime(timestamp))
+ browser_info.write(f'Last updated: [dim]{current_time}[/]')
+ except Exception:
+ pass
+
+ # Show the agent's current page URL if available
+ if browser_session.agent_focus:
+ current_url = (
+ browser_session.agent_focus.url.replace('https://', '')
+ .replace('http://', '')
+ .replace('www.', '')[:36]
+ + '…'
+ )
+ browser_info.write(f'👁️ [green]{current_url}[/]')
+ except Exception as e:
+ browser_info.write(f'[red]Error updating browser info: {str(e)}[/]')
+ else:
+ browser_info.write('[red]Browser not initialized[/]')
- model_info.write('[bold]🤖 Model[/bold]')
- model_info.write(f'Provider: {provider}')
- model_info.write(f'Model: {model_name}')
+ def update_model_panel(self) -> None:
+ """Update model information panel with details about the LLM."""
+ model_info = self.query_one('#model-info', RichLog)
+ model_info.clear()
+
+ if self.llm:
+ # Get model details
+ model_name = 'Unknown'
+ if hasattr(self.llm, 'model_name'):
+ model_name = self.llm.model_name
+ elif hasattr(self.llm, 'model'):
+ model_name = self.llm.model
+
+ # Show model name
+ if self.agent:
+ temp_str = f'{self.llm.temperature}ºC ' if self.llm.temperature else ''
+ vision_str = '+ vision ' if self.agent.settings.use_vision else ''
+ planner_str = '+ planner' if self.agent.settings.planner_llm else ''
+ model_info.write(
+ f'[white]LLM:[/] [blue]{self.llm.__class__.__name__} [yellow]{model_name}[/] {temp_str}{vision_str}{planner_str}'
+ )
else:
- model_info.write('[dim]No model configured[/dim]')
- except Exception as e:
- logging.debug(f'Error updating model panel: {e}')
+ model_info.write(f'[white]LLM:[/] [blue]{self.llm.__class__.__name__} [yellow]{model_name}[/]')
+
+ # Show token usage statistics if agent exists and has history
+ if self.agent and hasattr(self.agent, 'state') and hasattr(self.agent.state, 'history'):
+ # Calculate tokens per step
+ num_steps = len(self.agent.history.history)
+
+ # Get the last step metadata to show the most recent LLM response time
+ if num_steps > 0 and self.agent.history.history[-1].metadata:
+ last_step = self.agent.history.history[-1]
+ if last_step.metadata:
+ step_duration = last_step.metadata.duration_seconds
+ else:
+ step_duration = 0
+
+ # Show total duration
+ total_duration = self.agent.history.total_duration_seconds()
+ if total_duration > 0:
+ model_info.write(f'[white]Total Duration:[/] [magenta]{total_duration:.2f}s[/]')
+
+ # Calculate response time metrics
+ model_info.write(f'[white]Last Step Duration:[/] [magenta]{step_duration:.2f}s[/]')
+
+ # Add current state information
+ if hasattr(self.agent, 'running'):
+ if getattr(self.agent, 'running', False):
+ model_info.write('[yellow]LLM is thinking[blink]...[/][/]')
+ elif hasattr(self.agent, 'state') and hasattr(self.agent.state, 'paused') and self.agent.state.paused:
+ model_info.write('[orange]LLM paused[/]')
+ else:
+ model_info.write('[red]Model not initialized[/]')
def update_tasks_panel(self) -> None:
- """Update tasks panel with agent state."""
- try:
- tasks_info = self.query_one('#tasks-info', RichLog)
- tasks_info.clear()
-
- if hasattr(self, 'agent') and self.agent:
- # Show current task
- task = getattr(self.agent, 'task', 'No task')
- if len(task) > 100:
- task = task[:97] + '...'
- tasks_info.write('[bold]📋 Current Task[/bold]')
- tasks_info.write(f'{task}')
-
- # Show step counter
- if hasattr(self.agent, 'state'):
- step_count = getattr(self.agent.state, 'n_steps', 0)
- tasks_info.write(f'\nStep: {step_count}')
- else:
- tasks_info.write('[dim]No active task[/dim]')
- except Exception as e:
- logging.debug(f'Error updating tasks panel: {e}')
+ """Update tasks information panel with details about the tasks and steps hierarchy."""
+ tasks_info = self.query_one('#tasks-info', RichLog)
+ tasks_info.clear()
+
+ if self.agent:
+ # Check if agent has tasks
+ task_history = []
+ message_history = []
+
+ # Try to extract tasks by looking at message history
+ if hasattr(self.agent, '_message_manager') and self.agent._message_manager:
+ message_history = self.agent._message_manager.state.history.get_messages()
+
+ # Extract original task(s)
+ original_tasks = []
+ for msg in message_history:
+ if hasattr(msg, 'content'):
+ content = msg.content
+ if isinstance(content, str) and 'Your ultimate task is:' in content:
+ task_text = content.split('"""')[1].strip()
+ original_tasks.append(task_text)
+
+ if original_tasks:
+ tasks_info.write('[bold green]TASK:[/]')
+ for i, task in enumerate(original_tasks, 1):
+ # Only show latest task if multiple task changes occurred
+ if i == len(original_tasks):
+ tasks_info.write(f'[white]{task}[/]')
+ tasks_info.write('')
+
+ # Get current state information
+ current_step = self.agent.state.n_steps if hasattr(self.agent, 'state') else 0
+
+ # Get all agent history items
+ history_items = []
+ if hasattr(self.agent, 'state') and hasattr(self.agent.state, 'history'):
+ history_items = self.agent.history.history
+
+ if history_items:
+ tasks_info.write('[bold yellow]STEPS:[/]')
+
+ for idx, item in enumerate(history_items, 1):
+ # Determine step status
+ step_style = '[green]✓[/]'
+
+ # For the current step, show it as in progress
+ if idx == current_step:
+ step_style = '[yellow]⟳[/]'
+
+ # Check if this step had an error
+ if item.result and any(result.error for result in item.result):
+ step_style = '[red]✗[/]'
+
+ # Show step number
+ tasks_info.write(f'{step_style} Step {idx}/{current_step}')
+
+ # Show goal if available
+ if item.model_output and hasattr(item.model_output, 'current_state'):
+ # Show goal for this step
+ goal = item.model_output.current_state.next_goal
+ if goal:
+ # Take just the first line for display
+ goal_lines = goal.strip().split('\n')
+ goal_summary = goal_lines[0]
+ tasks_info.write(f' [cyan]Goal:[/] {goal_summary}')
+
+ # Show evaluation of previous goal (feedback)
+ eval_prev = item.model_output.current_state.evaluation_previous_goal
+ if eval_prev and idx > 1: # Only show for steps after the first
+ eval_lines = eval_prev.strip().split('\n')
+ eval_summary = eval_lines[0]
+ eval_summary = eval_summary.replace('Success', '✅ ').replace('Failed', '❌ ').strip()
+ tasks_info.write(f' [tan]Evaluation:[/] {eval_summary}')
+
+ # Show actions taken in this step
+ if item.model_output and item.model_output.action:
+ tasks_info.write(' [purple]Actions:[/]')
+ for action_idx, action in enumerate(item.model_output.action, 1):
+ action_type = action.__class__.__name__
+ if hasattr(action, 'model_dump'):
+ # For proper actions, show the action type
+ action_dict = action.model_dump(exclude_unset=True)
+ if action_dict:
+ action_name = list(action_dict.keys())[0]
+ tasks_info.write(f' {action_idx}. [blue]{action_name}[/]')
+
+ # Show results or errors from this step
+ if item.result:
+ for result in item.result:
+ if result.error:
+ error_text = result.error
+ tasks_info.write(f' [red]Error:[/] {error_text}')
+ elif result.extracted_content:
+ content = result.extracted_content
+ tasks_info.write(f' [green]Result:[/] {content}')
+
+ # Add a space between steps for readability
+ tasks_info.write('')
+
+ # If agent is actively running, show a status indicator
+ if hasattr(self.agent, 'running') and getattr(self.agent, 'running', False):
+ tasks_info.write('[yellow]Agent is actively working[blink]...[/][/]')
+ elif hasattr(self.agent, 'state') and hasattr(self.agent.state, 'paused') and self.agent.state.paused:
+ tasks_info.write('[orange]Agent is paused (press Enter to resume)[/]')
+ else:
+ tasks_info.write('[dim]Agent not initialized[/]')
+
+ # Force scroll to bottom
+ tasks_panel = self.query_one('#tasks-panel')
+ tasks_panel.scroll_end(animate=False)
async def run_prompt_mode(prompt: str, ctx: click.Context, debug: bool = False):
@@ -1261,6 +1446,9 @@ async def run_prompt_mode(prompt: str, ctx: click.Context, debug: bool = False):
async def textual_interface(config: dict[str, Any]):
"""Run the Textual interface."""
+ # Prevent browser_use from setting up logging at import time
+ os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
+
logger = logging.getLogger('browser_use.startup')
# Set up logging for Textual UI - prevent any logging to stdout
@@ -1300,6 +1488,14 @@ async def textual_interface(config: dict[str, Any]):
browser_profile=profile,
)
logger.debug('BrowserSession initialized successfully')
+
+ # Set up FIFO logging pipes for streaming logs to UI
+ try:
+ from browser_use.logging_config import setup_log_pipes
+ setup_log_pipes(session_id=browser_session.id)
+ logger.debug(f'FIFO logging pipes set up for session {browser_session.id[-4:]}')
+ except Exception as e:
+ logger.debug(f'Could not set up FIFO logging pipes: {e}')
# Browser version logging not available with CDP implementation
except Exception as e:
@@ -1318,6 +1514,8 @@ async def textual_interface(config: dict[str, Any]):
# Step 4: Get LLM
logger.debug('Getting LLM...')
try:
+ # Ensure setup_logging is not called when importing modules
+ os.environ['BROWSER_USE_SETUP_LOGGING'] = 'false'
llm = get_llm(config)
# Log LLM details
model_name = getattr(llm, 'model_name', None) or getattr(llm, 'model', 'Unknown model')
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index 0895844df..413c34605 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -105,8 +105,13 @@ class Controller(Generic[Context]):
from browser_use.browser.events import SwitchTabEvent
if browser_session.agent_focus and tab.id != browser_session.agent_focus.target_id:
- switch_event = browser_session.event_bus.dispatch(SwitchTabEvent(tab_index=i))
- await switch_event
+ try:
+ switch_event = browser_session.event_bus.dispatch(SwitchTabEvent(tab_index=i))
+ await switch_event
+ await switch_event.event_result(raise_if_none=False)
+ except Exception as e:
+ logger.warning(f'Failed to switch to existing Google tab: {e}, will use new tab')
+ continue
use_new_tab = False
break
@@ -124,8 +129,13 @@ class Controller(Generic[Context]):
from browser_use.browser.events import SwitchTabEvent
if browser_session.agent_focus and tab.id != browser_session.agent_focus.target_id:
- switch_event = browser_session.event_bus.dispatch(SwitchTabEvent(tab_index=i))
- await switch_event
+ try:
+ switch_event = browser_session.event_bus.dispatch(SwitchTabEvent(tab_index=i))
+ await switch_event
+ await switch_event.event_result()
+ except Exception as e:
+ logger.warning(f'Failed to switch to agent-owned tab: {e}, will use new tab')
+ continue
use_new_tab = False
break
@@ -133,19 +143,23 @@ class Controller(Generic[Context]):
logger.debug(f'Could not check for existing tabs: {e}, using new tab')
# Dispatch navigation event
- event = browser_session.event_bus.dispatch(
- NavigateToUrlEvent(
- url=search_url,
- new_tab=use_new_tab,
+ try:
+ event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(
+ url=search_url,
+ new_tab=use_new_tab,
+ )
)
- )
- await event
-
- msg = f'🔍 Searched for "{params.query}" in Google'
- logger.info(msg)
- return ActionResult(
- extracted_content=msg, include_in_memory=True, long_term_memory=f"Searched Google for '{params.query}'"
- )
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f'🔍 Searched for "{params.query}" in Google'
+ logger.info(msg)
+ return ActionResult(
+ extracted_content=msg, include_in_memory=True, long_term_memory=f"Searched Google for '{params.query}'"
+ )
+ except Exception as e:
+ logger.error(f'Failed to search Google: {e}')
+ return ActionResult(error=f'Failed to search Google for "{params.query}": {e}')
@self.registry.action(
'Navigate to URL, set new_tab=True to open in new tab, False to navigate in current tab', param_model=GoToUrlAction
@@ -155,6 +169,7 @@ class Controller(Generic[Context]):
# Dispatch navigation event
event = browser_session.event_bus.dispatch(NavigateToUrlEvent(url=params.url, new_tab=params.new_tab))
await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
if params.new_tab:
memory = f'Opened new tab with URL {params.url}'
@@ -173,7 +188,7 @@ class Controller(Generic[Context]):
# Check if it's specifically a RuntimeError about CDP client
if isinstance(e, RuntimeError) and 'CDP client not initialized' in error_msg:
browser_session.logger.error('❌ Browser connection failed - CDP client not properly initialized')
- raise BrowserError(f'Browser connection error: {error_msg}')
+ return ActionResult(error=f'Browser connection error: {error_msg}')
# Check for network-related errors
elif any(
err in error_msg
@@ -187,22 +202,23 @@ class Controller(Generic[Context]):
):
site_unavailable_msg = f'Site unavailable: {params.url} - {error_msg}'
browser_session.logger.warning(f'⚠️ {site_unavailable_msg}')
- raise BrowserError(site_unavailable_msg)
+ return ActionResult(error=site_unavailable_msg)
else:
- # Re-raise the original error
- raise
+ # Return error in ActionResult instead of re-raising
+ return ActionResult(error=f'Navigation failed: {error_msg}')
@self.registry.action('Go back', param_model=NoParamsAction)
async def go_back(_: NoParamsAction, browser_session: BrowserSession):
try:
event = browser_session.event_bus.dispatch(GoBackEvent())
await event
+ msg = '🔙 Navigated back'
+ logger.info(msg)
+ return ActionResult(extracted_content=msg)
except Exception as e:
logger.error(f'Failed to dispatch GoBackEvent: {type(e).__name__}: {e}')
- raise ValueError(f'Failed to go back: {e}') from e
- msg = '🔙 Navigated back'
- logger.info(msg)
- return ActionResult(extracted_content=msg)
+ error_msg = f'Failed to go back: {e}'
+ return ActionResult(error=error_msg)
@self.registry.action(
'Wait for x seconds default 3 (max 10 seconds). This can be used to wait until the page is fully loaded.'
@@ -231,14 +247,17 @@ class Controller(Generic[Context]):
# Dispatch click event with node
try:
- await browser_session.event_bus.dispatch(ClickElementEvent(node=node, new_tab=params.new_tab))
+ event = browser_session.event_bus.dispatch(ClickElementEvent(node=node, new_tab=params.new_tab))
+ await event
+ # Wait for handler to complete and get any exception (None is expected on success)
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f'🖱️ Clicked element with index {params.index}'
+ logger.info(msg)
+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)
except Exception as e:
logger.error(f'Failed to dispatch ClickElementEvent: {type(e).__name__}: {e}')
- raise ValueError(f'Failed to click element {params.index}: {e}') from e
-
- msg = f'🖱️ Clicked element with index {params.index}'
- logger.info(msg)
- return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)
+ error_msg = f'Failed to click element {params.index}: {e}'
+ return ActionResult(error=error_msg)
@self.registry.action(
'Click and input text into a input interactive element',
@@ -254,24 +273,40 @@ class Controller(Generic[Context]):
try:
event = browser_session.event_bus.dispatch(TypeTextEvent(node=node, text=params.text))
await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f"Input '{params.text}' into element {params.index}."
+ logger.info(msg)
+ return ActionResult(
+ extracted_content=msg,
+ include_in_memory=True,
+ long_term_memory=f"Input '{params.text}' into element {params.index}.",
+ )
except Exception as e:
# Log the full error for debugging
logger.error(f'Failed to dispatch TypeTextEvent: {type(e).__name__}: {e}')
- # Re-raise with more context
- raise ValueError(f'Failed to input text into element {params.index}: {e}') from e
-
- msg = f"Input '{params.text}' into element {params.index}."
- logger.info(msg)
- return ActionResult(
- extracted_content=msg,
- include_in_memory=True,
- long_term_memory=f"Input '{params.text}' into element {params.index}.",
- )
+ error_msg = f'Failed to input text into element {params.index}: {e}'
+ return ActionResult(error=error_msg)
@self.registry.action('Upload file to interactive element with file path', param_model=UploadFileAction)
- async def upload_file(params: UploadFileAction, browser_session: BrowserSession, available_file_paths: list[str]):
+ async def upload_file_to_element(params: UploadFileAction, browser_session: BrowserSession, available_file_paths: list[str], file_system: FileSystem):
+ # Check if file is in available_file_paths (user-provided or downloaded files)
if params.path not in available_file_paths:
- raise BrowserError(f'File path {params.path} is not available')
+ # Also check if it's a recently downloaded file that might not be in available_file_paths yet
+ downloaded_files = browser_session.downloaded_files
+ if params.path not in downloaded_files:
+ # Finally, check if it's a file in the FileSystem service
+ if file_system and file_system.get_dir():
+ # Check if the file is actually managed by the FileSystem service
+ # The path should be just the filename for FileSystem files
+ file_obj = file_system.get_file(params.path)
+ if file_obj:
+ # File is managed by FileSystem, construct the full path
+ file_system_path = str(file_system.get_dir() / params.path)
+ params = UploadFileAction(index=params.index, path=file_system_path)
+ else:
+ raise BrowserError(f'File path {params.path} is not available. Must be in available_file_paths, downloaded_files, or a file managed by file_system.')
+ else:
+ raise BrowserError(f'File path {params.path} is not available. Must be in available_file_paths or downloaded_files.')
if not os.path.exists(params.path):
raise BrowserError(f'File {params.path} does not exist')
@@ -294,7 +329,7 @@ class Controller(Generic[Context]):
return None
if browser_session.is_file_input(n):
return n
- for child in n.children_nodes:
+ for child in (n.children_nodes or []):
result = find_file_input_in_descendants(child, depth - 1)
if result:
return result
@@ -311,7 +346,7 @@ class Controller(Generic[Context]):
return result
# Check all siblings and their descendants
if current.parent_node:
- for sibling in current.parent_node.children_nodes:
+ for sibling in (current.parent_node.children_nodes or []):
if sibling is current:
continue
if browser_session.is_file_input(sibling):
@@ -340,7 +375,7 @@ class Controller(Generic[Context]):
params={'expression': 'window.scrollY || window.pageYOffset || 0'}, session_id=cdp_session.session_id
)
current_scroll_y = scroll_info.get('result', {}).get('value', 0)
- except:
+ except Exception:
current_scroll_y = 0
# Find all file inputs in the selector map and pick the closest one to scroll position
@@ -351,7 +386,7 @@ class Controller(Generic[Context]):
if browser_session.is_file_input(element):
# Get element's Y position
if element.absolute_position:
- element_y = element.absolute_position.get('y', 0)
+ element_y = element.absolute_position.y
distance = abs(element_y - current_scroll_y)
if distance < min_distance:
min_distance = distance
@@ -366,44 +401,56 @@ class Controller(Generic[Context]):
raise BrowserError(msg)
# Dispatch upload file event with the file input node
- event = browser_session.event_bus.dispatch(UploadFileEvent(node=file_input_node, file_path=params.path))
- await event
-
- msg = f'📁 Successfully uploaded file to index {params.index}'
- logger.info(msg)
- return ActionResult(
- extracted_content=msg,
- include_in_memory=True,
- long_term_memory=f'Uploaded file {params.path} to element {params.index}',
- )
+ try:
+ event = browser_session.event_bus.dispatch(UploadFileEvent(node=file_input_node, file_path=params.path))
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f'📁 Successfully uploaded file to index {params.index}'
+ logger.info(msg)
+ return ActionResult(
+ extracted_content=msg,
+ include_in_memory=True,
+ long_term_memory=f'Uploaded file {params.path} to element {params.index}',
+ )
+ except Exception as e:
+ logger.error(f'Failed to upload file: {e}')
+ raise BrowserError(f'Failed to upload file: {e}')
# Tab Management Actions
@self.registry.action('Switch tab', param_model=SwitchTabAction)
async def switch_tab(params: SwitchTabAction, browser_session: BrowserSession):
# Dispatch switch tab event
- event = browser_session.event_bus.dispatch(SwitchTabEvent(tab_index=params.page_id))
- await event
-
- msg = f'🔄 Switched to tab #{params.page_id}'
- logger.info(msg)
- return ActionResult(
- extracted_content=msg, include_in_memory=True, long_term_memory=f'Switched to tab {params.page_id}'
- )
+ try:
+ event = browser_session.event_bus.dispatch(SwitchTabEvent(tab_index=params.page_id))
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f'🔄 Switched to tab #{params.page_id}'
+ logger.info(msg)
+ return ActionResult(
+ extracted_content=msg, include_in_memory=True, long_term_memory=f'Switched to tab {params.page_id}'
+ )
+ except Exception as e:
+ logger.error(f'Failed to switch tab: {e}')
+ return ActionResult(error=f'Failed to switch to tab {params.page_id}: {e}')
@self.registry.action('Close an existing tab', param_model=CloseTabAction)
async def close_tab(params: CloseTabAction, browser_session: BrowserSession):
# Dispatch close tab event
- event = browser_session.event_bus.dispatch(CloseTabEvent(tab_index=params.page_id))
- await event
-
- msg = f'❌ Closed tab #{params.page_id}'
- logger.info(msg)
- return ActionResult(
- extracted_content=msg,
- include_in_memory=True,
- long_term_memory=f'Closed tab {params.page_id}',
- )
+ try:
+ event = browser_session.event_bus.dispatch(CloseTabEvent(tab_index=params.page_id))
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f'❌ Closed tab #{params.page_id}'
+ logger.info(msg)
+ return ActionResult(
+ extracted_content=msg,
+ include_in_memory=True,
+ long_term_memory=f'Closed tab {params.page_id}',
+ )
+ except Exception as e:
+ logger.error(f'Failed to close tab: {e}')
+ return ActionResult(error=f'Failed to close tab {params.page_id}: {e}')
# Content Actions
@@ -518,43 +565,45 @@ Provide the extracted information in a clear, structured format."""
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 and params.index != 0:
- try:
- node = await browser_session.get_element_by_index(params.index)
- if node is None:
- # Element not found - return error
- raise ValueError(f'Element index {params.index} not found in DOM')
- except Exception as e:
- # Error getting element - return error
- raise ValueError(f'Failed to get element {params.index}: {e}') from e
-
- # Dispatch scroll event with node - the complex logic is handled in the event handler
- # Convert pages to pixels (assuming 800px per page as standard viewport height)
- pixels = int(params.num_pages * 800)
try:
+ # 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 and params.index != 0:
+ try:
+ node = await browser_session.get_element_by_index(params.index)
+ if node is None:
+ # Element not found - return error
+ raise ValueError(f'Element index {params.index} not found in DOM')
+ except Exception as e:
+ # Error getting element - return error
+ raise ValueError(f'Failed to get element {params.index}: {e}') from e
+
+ # Dispatch scroll event with node - the complex logic is handled in the event handler
+ # Convert pages to pixels (assuming 800px per page as standard viewport height)
+ pixels = int(params.num_pages * 800)
event = browser_session.event_bus.dispatch(
ScrollEvent(direction='down' if params.down else 'up', amount=pixels, node=node)
)
await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ direction = 'down' if params.down else 'up'
+
+ # 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'
+ else:
+ long_term_memory = f'Scrolled {direction} {target} by {params.num_pages} pages'
+
+ msg = f'🔍 {long_term_memory}'
+ logger.info(msg)
+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=long_term_memory)
except Exception as e:
logger.error(f'Failed to dispatch ScrollEvent: {type(e).__name__}: {e}')
- raise ValueError(f'Failed to scroll: {e}') from e
-
- direction = 'down' if params.down else 'up'
- # 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'
- else:
- long_term_memory = f'Scrolled {direction} {target} by {params.num_pages} pages'
-
- msg = f'🔍 {long_term_memory}'
- logger.info(msg)
- return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=long_term_memory)
+ error_msg = f'Failed to scroll: {e}'
+ return ActionResult(error=error_msg)
@self.registry.action(
'Send strings of special keys to use Playwright page.keyboard.press - examples include Escape, Backspace, Insert, PageDown, Delete, Enter, or Shortcuts such as `Control+o`, `Control+Shift+T`',
@@ -565,13 +614,14 @@ Provide the extracted information in a clear, structured format."""
try:
event = browser_session.event_bus.dispatch(SendKeysEvent(keys=params.keys))
await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f'⌨️ Sent keys: {params.keys}'
+ logger.info(msg)
+ return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Sent keys: {params.keys}')
except Exception as e:
logger.error(f'Failed to dispatch SendKeysEvent: {type(e).__name__}: {e}')
- raise ValueError(f'Failed to send keys: {e}') from e
-
- msg = f'⌨️ Sent keys: {params.keys}'
- logger.info(msg)
- return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Sent keys: {params.keys}')
+ error_msg = f'Failed to send keys: {e}'
+ return ActionResult(error=error_msg)
@self.registry.action(
description='Scroll to a text in the current page',
@@ -579,15 +629,15 @@ Provide the extracted information in a clear, structured format."""
async def scroll_to_text(text: str, browser_session: BrowserSession): # type: ignore
# Dispatch scroll to text event
event = browser_session.event_bus.dispatch(ScrollToTextEvent(text=text))
- await event
-
- # Check result to see if text was found
- result = await event.event_result()
- if result and result.get('found'):
+
+ try:
+ # The handler returns None on success or raises an exception if text not found
+ await event.event_result(raise_if_any=True, raise_if_none=False)
msg = f'🔍 Scrolled to text: {text}'
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Scrolled to text: {text}')
- else:
+ except Exception as e:
+ # Text not found
msg = f"Text '{text}' not found or not visible on page"
logger.info(msg)
return ActionResult(
diff --git a/browser_use/logging_config.py b/browser_use/logging_config.py
index 5c4b59a57..831388b71 100644
--- a/browser_use/logging_config.py
+++ b/browser_use/logging_config.py
@@ -1,5 +1,7 @@
import logging
+import os
import sys
+from pathlib import Path
from dotenv import load_dotenv
@@ -119,20 +121,36 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
browser_use_logger.addHandler(console)
browser_use_logger.setLevel(log_level)
- # Configure CDP logging to match browser_use level
- # websockets.client emits the logs that cdp_use transforms to appear as cdp_use.client
- cdp_loggers = [
- 'websockets.client', # Controls emission of transformed cdp_use.client logs
- 'cdp_use',
- 'cdp_use.client',
- 'cdp_use.cdp',
- 'cdp_use.cdp.registry',
- ]
- for logger_name in cdp_loggers:
- cdp_logger = logging.getLogger(logger_name)
- cdp_logger.setLevel(log_level) # Same level as browser_use
- cdp_logger.addHandler(console) # Same handler as browser_use
- cdp_logger.propagate = False
+ # Configure bubus logger to allow INFO level logs
+ bubus_logger = logging.getLogger('bubus')
+ bubus_logger.propagate = False # Don't propagate to root logger
+ bubus_logger.addHandler(console)
+ bubus_logger.setLevel(logging.INFO if log_type == 'result' else log_level)
+
+ # Configure CDP logging using cdp_use's setup function
+ # This enables the formatted CDP output at the same level as browser_use
+ try:
+ from cdp_use.logging import setup_cdp_logging # type: ignore
+ # Use the same stream and level as browser_use
+ setup_cdp_logging(
+ level=log_level,
+ stream=stream or sys.stdout,
+ format_string='%(levelname)-8s [%(name)s] %(message)s' if log_type != 'result' else '%(message)s'
+ )
+ except ImportError:
+ # If cdp_use doesn't have the new logging module, fall back to manual config
+ cdp_loggers = [
+ 'websockets.client',
+ 'cdp_use',
+ 'cdp_use.client',
+ 'cdp_use.cdp',
+ 'cdp_use.cdp.registry',
+ ]
+ for logger_name in cdp_loggers:
+ cdp_logger = logging.getLogger(logger_name)
+ cdp_logger.setLevel(log_level)
+ cdp_logger.addHandler(console)
+ cdp_logger.propagate = False
logger = logging.getLogger('browser_use')
# logger.info('BrowserUse logging setup complete with level %s', log_type)
@@ -165,3 +183,97 @@ def setup_logging(stream=None, log_level=None, force_setup=False):
third_party.propagate = False
return logger
+
+
+class FIFOHandler(logging.Handler):
+ """Non-blocking handler that writes to a named pipe."""
+
+ def __init__(self, fifo_path: str):
+ super().__init__()
+ self.fifo_path = fifo_path
+ Path(fifo_path).parent.mkdir(parents=True, exist_ok=True)
+
+ # Create FIFO if it doesn't exist
+ if not os.path.exists(fifo_path):
+ os.mkfifo(fifo_path)
+
+ # Don't open the FIFO yet - will open on first write
+ self.fd = None
+
+ def emit(self, record):
+ try:
+ # Open FIFO on first write if not already open
+ if self.fd is None:
+ try:
+ self.fd = os.open(self.fifo_path, os.O_WRONLY | os.O_NONBLOCK)
+ except OSError:
+ # No reader connected yet, skip this message
+ return
+
+ msg = f"{self.format(record)}\n".encode()
+ os.write(self.fd, msg)
+ except (OSError, BrokenPipeError):
+ # Reader disconnected, close and reset
+ if self.fd is not None:
+ try:
+ os.close(self.fd)
+ except:
+ pass
+ self.fd = None
+
+ def close(self):
+ if hasattr(self, 'fd') and self.fd is not None:
+ try:
+ os.close(self.fd)
+ except:
+ pass
+ super().close()
+
+
+def setup_log_pipes(session_id: str, base_dir: str = None):
+ """Setup named pipes for log streaming.
+
+ Usage:
+ # In browser-use:
+ setup_log_pipes(session_id="abc123")
+
+ # In consumer process:
+ tail -f {temp_dir}/buagent.c123/agent.pipe
+ """
+ import tempfile
+
+ if base_dir is None:
+ base_dir = tempfile.gettempdir()
+
+ suffix = session_id[-4:]
+ pipe_dir = Path(base_dir) / f"buagent.{suffix}"
+
+ # Agent logs
+ agent_handler = FIFOHandler(str(pipe_dir / "agent.pipe"))
+ agent_handler.setLevel(logging.DEBUG)
+ agent_handler.setFormatter(logging.Formatter('%(levelname)-8s [%(name)s] %(message)s'))
+ for name in ['browser_use.agent', 'browser_use.controller']:
+ logger = logging.getLogger(name)
+ logger.addHandler(agent_handler)
+ logger.setLevel(logging.DEBUG)
+ logger.propagate = True
+
+ # CDP logs
+ cdp_handler = FIFOHandler(str(pipe_dir / "cdp.pipe"))
+ cdp_handler.setLevel(logging.DEBUG)
+ cdp_handler.setFormatter(logging.Formatter('%(levelname)-8s [%(name)s] %(message)s'))
+ for name in ['websockets.client', 'cdp_use.client']:
+ logger = logging.getLogger(name)
+ logger.addHandler(cdp_handler)
+ logger.setLevel(logging.DEBUG)
+ logger.propagate = True
+
+ # Event logs
+ event_handler = FIFOHandler(str(pipe_dir / "events.pipe"))
+ event_handler.setLevel(logging.INFO)
+ event_handler.setFormatter(logging.Formatter('%(levelname)-8s [%(name)s] %(message)s'))
+ for name in ['bubus', 'browser_use.browser.session']:
+ logger = logging.getLogger(name)
+ logger.addHandler(event_handler)
+ logger.setLevel(logging.INFO) # Enable INFO for event bus
+ logger.propagate = True
diff --git a/examples/custom-functions/cua.py b/examples/custom-functions/cua.py
index 1719917ac..415e8701c 100644
--- a/examples/custom-functions/cua.py
+++ b/examples/custom-functions/cua.py
@@ -43,14 +43,17 @@ class OpenAICUAAction(BaseModel):
description: str = Field(..., description='Description of your next goal')
-async def handle_model_action(page, action) -> ActionResult:
+async def handle_model_action(browser_session: BrowserSession, action) -> ActionResult:
"""
Given a computer action (e.g., click, double_click, scroll, etc.),
- execute the corresponding operation on the Playwright page.
+ execute the corresponding operation using CDP.
"""
action_type = action.type
ERROR_MSG: str = 'Could not execute the CUA action.'
+ if not browser_session.agent_focus:
+ return ActionResult(error='No active browser session')
+
try:
match action_type:
case 'click':
@@ -60,7 +63,27 @@ async def handle_model_action(page, action) -> ActionResult:
# Not handling things like middle click, etc.
if button != 'left' and button != 'right':
button = 'left'
- await page.mouse.click(x, y, button=button)
+
+ # Use CDP to click
+ await browser_session.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mousePressed',
+ 'x': x,
+ 'y': y,
+ 'button': button,
+ 'clickCount': 1,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
+ await browser_session.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mouseReleased',
+ 'x': x,
+ 'y': y,
+ 'button': button,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
msg = f'Clicked at ({x}, {y}) with button {button}'
return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)
@@ -68,8 +91,24 @@ async def handle_model_action(page, action) -> ActionResult:
x, y = action.x, action.y
scroll_x, scroll_y = action.scroll_x, action.scroll_y
print(f'Action: scroll at ({x}, {y}) with offsets (scroll_x={scroll_x}, scroll_y={scroll_y})')
- await page.mouse.move(x, y)
- await page.evaluate(f'window.scrollBy({scroll_x}, {scroll_y})')
+
+ # Move mouse to position first
+ await browser_session.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mouseMoved',
+ 'x': x,
+ 'y': y,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
+
+ # Execute scroll using JavaScript
+ await browser_session.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f'window.scrollBy({scroll_x}, {scroll_y})',
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
msg = f'Scrolled at ({x}, {y}) with offsets (scroll_x={scroll_x}, scroll_y={scroll_y})'
return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)
@@ -78,19 +117,43 @@ async def handle_model_action(page, action) -> ActionResult:
for k in keys:
print(f"Action: keypress '{k}'")
# A simple mapping for common keys; expand as needed.
+ key_code = k
if k.lower() == 'enter':
- await page.keyboard.press('Enter')
+ key_code = 'Enter'
elif k.lower() == 'space':
- await page.keyboard.press(' ')
- else:
- await page.keyboard.press(k)
+ key_code = 'Space'
+
+ # Use CDP to send key
+ await browser_session.agent_focus.cdp_client.send.Input.dispatchKeyEvent(
+ params={
+ 'type': 'keyDown',
+ 'key': key_code,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
+ await browser_session.agent_focus.cdp_client.send.Input.dispatchKeyEvent(
+ params={
+ 'type': 'keyUp',
+ 'key': key_code,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
msg = f'Pressed keys: {keys}'
return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)
case 'type':
text = action.text
print(f'Action: type text: {text}')
- await page.keyboard.type(text)
+
+ # Type text character by character
+ for char in text:
+ await browser_session.agent_focus.cdp_client.send.Input.dispatchKeyEvent(
+ params={
+ 'type': 'char',
+ 'text': char,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
msg = f'Typed text: {text}'
return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)
@@ -202,7 +265,7 @@ async def openai_cua_fallback(params: OpenAICUAAction, browser_session: BrowserS
action = computer_call.action
print(f'🎬 Executing CUA action: {action.type} - {action}')
- action_result = await handle_model_action(page, action)
+ action_result = await handle_model_action(browser_session, action)
await asyncio.sleep(0.1)
print('✅ CUA action completed successfully')
diff --git a/examples/custom-functions/extract_pdf_content.py b/examples/custom-functions/extract_pdf_content.py
index 0dc451f34..bae621f70 100755
--- a/examples/custom-functions/extract_pdf_content.py
+++ b/examples/custom-functions/extract_pdf_content.py
@@ -21,7 +21,7 @@ from pydantic import BaseModel, Field
from browser_use import Agent, Controller
from browser_use.agent.views import ActionResult
-from browser_use.browser.context import BrowserContext
+from browser_use.browser import BrowserSession
from browser_use.llm import ChatOpenAI
if not os.getenv('OPENAI_API_KEY'):
@@ -43,7 +43,7 @@ class PdfExtractParams(BaseModel):
'Extract PDF Text',
param_model=PdfExtractParams,
)
-def extract_mistral_ocr(params: PdfExtractParams, browser: BrowserContext) -> ActionResult:
+def extract_mistral_ocr(params: PdfExtractParams, browser: BrowserSession) -> ActionResult:
"""
Process a PDF URL using Mistral OCR API and return the OCR response.
diff --git a/examples/custom-functions/file_upload.py b/examples/custom-functions/file_upload.py
index cdbb84a22..139d59f1b 100644
--- a/examples/custom-functions/file_upload.py
+++ b/examples/custom-functions/file_upload.py
@@ -52,7 +52,7 @@ async def upload_file(index: int, path: str, browser_session: BrowserSession, av
# Dispatch the upload file event
event = browser_session.event_bus.dispatch(
- UploadFileEvent(element_node=dom_element, file_path=path)
+ UploadFileEvent(node=dom_element, file_path=path)
)
await event
diff --git a/examples/custom-functions/solve_amazon_captcha.py b/examples/custom-functions/solve_amazon_captcha.py
index cf73e1919..f7156c3fd 100644
--- a/examples/custom-functions/solve_amazon_captcha.py
+++ b/examples/custom-functions/solve_amazon_captcha.py
@@ -48,11 +48,21 @@ controller = Controller()
],
)
async def solve_amazon_captcha(browser_session: BrowserSession):
- page = await browser_session.get_current_page()
-
- # Find the captcha image and extract its src
- captcha_img = page.locator('img[src*="amazon.com/captcha"]')
- link = await captcha_img.get_attribute('src')
+ if not browser_session.agent_focus:
+ raise ValueError('No active browser session')
+
+ # Find the captcha image and extract its src using CDP
+ result = await browser_session.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ const img = document.querySelector('img[src*="amazon.com/captcha"]');
+ img ? img.src : null;
+ """,
+ 'returnByValue': True,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
+ link = result.get('result', {}).get('value')
if not link:
raise ValueError('Could not find captcha image on the page')
@@ -62,8 +72,31 @@ async def solve_amazon_captcha(browser_session: BrowserSession):
if not solution or solution == 'Not solved':
raise ValueError('Captcha could not be solved')
- await page.locator('#captchacharacters').fill(solution)
- await page.locator('button[type="submit"]').click()
+ # Fill the captcha solution using CDP
+ await browser_session.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f"""
+ const input = document.querySelector('#captchacharacters');
+ if (input) {{
+ input.value = '{solution}';
+ input.dispatchEvent(new Event('input', {{ bubbles: true }}));
+ input.dispatchEvent(new Event('change', {{ bubbles: true }}));
+ }}
+ """,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
+
+ # Click submit button using CDP
+ await browser_session.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ const button = document.querySelector('button[type="submit"]');
+ if (button) button.click();
+ """,
+ },
+ session_id=browser_session.agent_focus.session_id,
+ )
return ActionResult(extracted_content=solution)
diff --git a/examples/features/multiple_tasks.py b/examples/features/multiple_tasks.py
index 9ccacf188..1fade4e12 100644
--- a/examples/features/multiple_tasks.py
+++ b/examples/features/multiple_tasks.py
@@ -10,8 +10,7 @@ load_dotenv()
from browser_use import Agent
-from browser_use.browser import BrowserSession
-from browser_use.browser.types import async_playwright
+from browser_use.browser import BrowserSession, BrowserProfile
from browser_use.llm import ChatGoogle
api_key = os.getenv('GOOGLE_API_KEY')
@@ -23,23 +22,26 @@ llm = ChatGoogle(model='gemini-2.0-flash', api_key=api_key)
async def main():
- async with async_playwright() as p:
- browser = await p.chromium.launch(
- headless=False,
- )
-
- context = await browser.new_context(
- viewport={'width': 1502, 'height': 853},
- ignore_https_errors=True,
- )
-
- agent = Agent(
- browser_session=BrowserSession(
- browser_context=context,
- ),
- task='Go to https://browser-use.com/',
- llm=llm,
- )
+ # Create browser profile with settings
+ browser_profile = BrowserProfile(
+ headless=False,
+ viewport={'width': 1502, 'height': 853},
+ ignore_https_errors=True,
+ )
+
+ # Create browser session
+ browser_session = BrowserSession(
+ browser_profile=browser_profile,
+ )
+
+ # Start the browser
+ await browser_session.start()
+
+ agent = Agent(
+ browser_session=browser_session,
+ task='Go to https://browser-use.com/',
+ llm=llm,
+ )
try:
result = await agent.run()
@@ -73,8 +75,8 @@ async def main():
continue
finally:
- await context.close()
- await browser.close()
+ # Stop the browser session
+ await browser_session.stop()
if __name__ == '__main__':
diff --git a/examples/features/result_processing.py b/examples/features/result_processing.py
index d4d9aa3da..a8020d469 100644
--- a/examples/features/result_processing.py
+++ b/examples/features/result_processing.py
@@ -18,14 +18,16 @@ llm = ChatOpenAI(model='gpt-4.1')
async def main():
- async with BrowserSession(
+ browser_session = BrowserSession(
browser_profile=BrowserProfile(
headless=False,
traces_dir='./tmp/result_processing',
window_size={'width': 1280, 'height': 1000},
user_data_dir='~/.config/browseruse/profiles/default',
)
- ) as browser_session:
+ )
+ await browser_session.start()
+ try:
agent = Agent(
task="go to google.com and type 'OpenAI' click search and give me the first url",
llm=llm,
@@ -45,6 +47,8 @@ async def main():
print('\nThoughts:')
pprint(history.model_thoughts(), indent=4)
+ finally:
+ await browser_session.stop()
if __name__ == '__main__':
diff --git a/examples/features/save_trace.py b/examples/features/save_trace.py
index 3e9caa100..7dbe82375 100644
--- a/examples/features/save_trace.py
+++ b/examples/features/save_trace.py
@@ -23,13 +23,16 @@ async def main():
)
)
- async with browser_session:
+ await browser_session.start()
+ try:
agent = Agent(
task='Go to hackernews, then go to apple.com and return all titles of open tabs',
llm=llm,
browser_session=browser_session,
)
await agent.run()
+ finally:
+ await browser_session.stop()
asyncio.run(main())
diff --git a/examples/use-cases/find_and_apply_to_jobs.py b/examples/use-cases/find_and_apply_to_jobs.py
index 06bc8ad59..7852d9440 100644
--- a/examples/use-cases/find_and_apply_to_jobs.py
+++ b/examples/use-cases/find_and_apply_to_jobs.py
@@ -80,17 +80,18 @@ def read_cv():
)
async def upload_cv(index: int, browser_session: BrowserSession):
path = str(CV.absolute())
- file_upload_dom_el = await browser_session.find_file_upload_element_by_index(index)
-
- if file_upload_dom_el is None:
- logger.info(f'No file upload element found at index {index}')
- return ActionResult(error=f'No file upload element found at index {index}')
-
- file_upload_el = await browser_session.get_locate_element(file_upload_dom_el)
-
- if file_upload_el is None:
- logger.info(f'No file upload element found at index {index}')
- return ActionResult(error=f'No file upload element found at index {index}')
+
+ # Get the element by index
+ dom_element = await browser_session.get_element_by_index(index)
+
+ if dom_element is None:
+ logger.info(f'No element found at index {index}')
+ return ActionResult(error=f'No element found at index {index}')
+
+ # Check if it's a file input element
+ if not browser_session.is_file_input(dom_element):
+ logger.info(f'Element at index {index} is not a file upload element')
+ return ActionResult(error=f'Element at index {index} is not a file upload element')
try:
await file_upload_el.set_input_files(path)
diff --git a/examples/use-cases/google_sheets.py b/examples/use-cases/google_sheets.py
index f0100da68..cebc3a63b 100644
--- a/examples/use-cases/google_sheets.py
+++ b/examples/use-cases/google_sheets.py
@@ -43,7 +43,8 @@ async def main():
),
)
- async with browser_session:
+ await browser_session.start()
+ try:
model = ChatOpenAI(model='gpt-4.1')
# eraser = Agent(
@@ -76,8 +77,10 @@ async def main():
controller=controller,
)
await researcher.run()
+ finally:
+ await browser_session.stop()
- # improvised_continuer = Agent(
+ # improvised_continuer = Agent(
# task="""
# Read the Google Sheet https://docs.google.com/spreadsheets/d/1INaIcfpYXlMRWO__de61SHFCaqt1lfHlcvtXZPItlpI/edit
# Add 3 more rows to the bottom continuing the existing pattern, make sure any data you add is sourced correctly.
diff --git a/examples/use-cases/play_chess.py b/examples/use-cases/play_chess.py
index 2ebc464da..6253c2dfc 100755
--- a/examples/use-cases/play_chess.py
+++ b/examples/use-cases/play_chess.py
@@ -22,7 +22,7 @@ from pydantic import BaseModel, Field
from browser_use import Agent, Controller
from browser_use.agent.views import ActionResult
-from browser_use.browser.context import BrowserContext
+from browser_use.browser import BrowserSession
from browser_use.llm import ChatOpenAI
if not os.getenv('OPENAI_API_KEY'):
@@ -97,10 +97,21 @@ def pixels_to_algebraic(x_px: float, y_px: float, square_size: float) -> str:
raise ValueError(f'Pixel coordinates out of bounds: ({x_px}, {y_px})')
-async def calculate_square_size(page) -> float | None:
+async def calculate_square_size(browser: BrowserSession) -> float | None:
"""Dynamically calculates the size of a chess square in pixels."""
try:
- board_html = await page.locator('cg-board').inner_html(timeout=3000)
+ if not browser.agent_focus:
+ return None
+
+ # Get board HTML using CDP
+ result = await browser.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': "document.querySelector('cg-board') ? document.querySelector('cg-board').innerHTML : null",
+ 'returnByValue': True,
+ },
+ session_id=browser.agent_focus.session_id,
+ )
+ board_html = result.get('result', {}).get('value')
soup = BeautifulSoup(board_html, 'html.parser')
pieces = soup.find_all('piece')
if not pieces:
@@ -154,17 +165,26 @@ def create_fen_board(board_state: dict) -> str:
return fen
-async def get_current_board_info(page) -> tuple[str | None, float | None]:
+async def get_current_board_info(browser: BrowserSession) -> tuple[str | None, float | None]:
"""Reads the current board HTML and returns FEN string and square size."""
board_state = {}
board_html = ''
square_size = None
try:
- board_locator = page.locator('cg-board')
- await board_locator.wait_for(state='visible', timeout=3000)
- board_html = await board_locator.inner_html()
- square_size = await calculate_square_size(page)
+ if not browser.agent_focus:
+ return None, None
+
+ # Get board HTML using CDP
+ result = await browser.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': "document.querySelector('cg-board') ? document.querySelector('cg-board').innerHTML : null",
+ 'returnByValue': True,
+ },
+ session_id=browser.agent_focus.session_id,
+ )
+ board_html = result.get('result', {}).get('value')
+ square_size = await calculate_square_size(browser)
except Exception as e:
logger.error(f'Error (get_info): Could not read cg-board: {e}')
return None, None
@@ -202,10 +222,13 @@ async def get_current_board_info(page) -> tuple[str | None, float | None]:
@controller.registry.action(
'Read Chess Board',
)
-async def read_board(browser: BrowserContext):
+async def read_board(browser: BrowserSession):
"""Reads the board, returns FEN and legal moves in SAN (+/#), and the last move by opponent if possible."""
- page = await browser.get_current_page()
- full_fen, _ = await get_current_board_info(page)
+ # Get the current page's CDP session
+ if not browser.agent_focus:
+ return ActionResult(extracted_content='No active page to read board from.')
+
+ full_fen, _ = await get_current_board_info(browser)
if not full_fen:
return ActionResult(extracted_content='Could not read board state.')
@@ -214,7 +237,15 @@ async def read_board(browser: BrowserContext):
last_move_san = None
try:
- move_list_html = await page.locator('l4x').inner_html(timeout=3000)
+ if browser.agent_focus:
+ result = await browser.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': "document.querySelector('l4x') ? document.querySelector('l4x').innerHTML : null",
+ 'returnByValue': True,
+ },
+ session_id=browser.agent_focus.session_id,
+ )
+ move_list_html = result.get('result', {}).get('value') or ''
soup = BeautifulSoup(move_list_html, 'html.parser')
move_tags = soup.find_all('kwdb')
moves = [tag.get_text(strip=True) for tag in move_tags]
@@ -254,14 +285,13 @@ async def read_board(browser: BrowserContext):
'Play Chess Move',
param_model=PlayMoveParams,
)
-async def play_move(params: PlayMoveParams, browser: BrowserContext):
+async def play_move(params: PlayMoveParams, browser: BrowserSession):
"""Plays a chess move given in SAN by converting it to UCI and clicking."""
san_move = params.move.strip()
- page = await browser.get_current_page()
uci_move = ''
try:
- current_fen, square_size = await get_current_board_info(page)
+ current_fen, square_size = await get_current_board_info(browser)
if not current_fen or square_size is None:
return ActionResult(extracted_content='Failed to get current FEN or square size to play move.')
@@ -287,8 +317,9 @@ async def play_move(params: PlayMoveParams, browser: BrowserContext):
return ActionResult(extracted_content=f"Could not convert UCI '{uci_move}' to coordinates: {e}")
try:
- board_locator = page.locator('cg-board')
- await board_locator.wait_for(state='visible', timeout=3000)
+ if not browser.agent_focus:
+ return ActionResult(extracted_content='No active page to play move on.')
+
click_offset = square_size / 2
start_click_x = start_x + click_offset
start_click_y = start_y + click_offset
@@ -296,9 +327,71 @@ async def play_move(params: PlayMoveParams, browser: BrowserContext):
end_click_y = end_y + click_offset
logger.debug(f"DEBUG: Playing SAN '{san_move}' (UCI: {uci_move}).")
- await board_locator.click(position={'x': start_click_x, 'y': start_click_y}, timeout=3000)
+
+ # Get board element bounds first
+ result = await browser.agent_focus.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ const board = document.querySelector('cg-board');
+ if (board) {
+ const rect = board.getBoundingClientRect();
+ {x: rect.left, y: rect.top, width: rect.width, height: rect.height};
+ } else {
+ null;
+ }
+ """,
+ 'returnByValue': True,
+ },
+ session_id=browser.agent_focus.session_id,
+ )
+ board_rect = result.get('result', {}).get('value')
+
+ if not board_rect:
+ return ActionResult(extracted_content='Could not find chess board element.')
+
+ # Click start position
+ await browser.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mousePressed',
+ 'x': board_rect['x'] + start_click_x,
+ 'y': board_rect['y'] + start_click_y,
+ 'button': 'left',
+ 'clickCount': 1,
+ },
+ session_id=browser.agent_focus.session_id,
+ )
+ await browser.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mouseReleased',
+ 'x': board_rect['x'] + start_click_x,
+ 'y': board_rect['y'] + start_click_y,
+ 'button': 'left',
+ },
+ session_id=browser.agent_focus.session_id,
+ )
+
await asyncio.sleep(0.5)
- await board_locator.click(position={'x': end_click_x, 'y': end_click_y}, timeout=3000)
+
+ # Click end position
+ await browser.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mousePressed',
+ 'x': board_rect['x'] + end_click_x,
+ 'y': board_rect['y'] + end_click_y,
+ 'button': 'left',
+ 'clickCount': 1,
+ },
+ session_id=browser.agent_focus.session_id,
+ )
+ await browser.agent_focus.cdp_client.send.Input.dispatchMouseEvent(
+ params={
+ 'type': 'mouseReleased',
+ 'x': board_rect['x'] + end_click_x,
+ 'y': board_rect['y'] + end_click_y,
+ 'button': 'left',
+ },
+ session_id=browser.agent_focus.session_id,
+ )
await asyncio.sleep(0.5)
return ActionResult(extracted_content=f'Played move {san_move}.', include_in_memory=True)
diff --git a/tests/ci/test_action_ClickElementEvent.py b/tests/ci/test_browser_event_ClickElementEvent.py
similarity index 50%
rename from tests/ci/test_action_ClickElementEvent.py
rename to tests/ci/test_browser_event_ClickElementEvent.py
index 4dd694505..e06befa1e 100644
--- a/tests/ci/test_action_ClickElementEvent.py
+++ b/tests/ci/test_browser_event_ClickElementEvent.py
@@ -1,4 +1,6 @@
import asyncio
+import tempfile
+from pathlib import Path
import pytest
from pytest_httpserver import HTTPServer
@@ -10,6 +12,7 @@ from browser_use.controller.service import Controller
from browser_use.controller.views import (
ClickElementAction,
GoToUrlAction,
+ UploadFileAction,
)
@@ -235,7 +238,7 @@ class TestClickElementEvent:
# Get the link element (assuming it will be at index 0)
# First get the browser state to see what elements are available
- state = await browser_session.get_browser_state_with_recovery()
+ state = await browser_session.get_browser_state_summary()
# Find the link element in the selector map
link_index = None
@@ -264,9 +267,22 @@ class TestClickElementEvent:
final_tab_count = len(tabs)
assert final_tab_count == initial_tab_count + 1, f'Expected {initial_tab_count + 1} tabs, got {final_tab_count}'
- # Verify we switched to the new tab and it has the correct URL
+ # Verify we're still on the original tab (not switched) - matches browser Cmd/Ctrl+click behavior
current_url = await browser_session.get_current_page_url()
- assert f'{base_url}/page1' in current_url
+ assert f'{base_url}/newTab' in current_url, f'Should still be on original tab, but got {current_url}'
+
+ # Wait for the new tab to finish navigating to the target URL
+ # New tabs initially open at the current URL then navigate to the target
+ max_wait = 5
+ for _ in range(max_wait):
+ await asyncio.sleep(0.5)
+ tabs = await browser_session.get_tabs()
+ new_tab = tabs[-1] # Last tab is the newly opened one
+ if f'{base_url}/page1' in new_tab.url:
+ break
+
+ # Verify the new tab has the correct URL
+ assert f'{base_url}/page1' in new_tab.url, f'New tab should have page1 URL, but got {new_tab.url}'
async def test_click_element_normal_vs_new_tab(self, controller, browser_session, base_url, http_server):
"""Test that click_element_by_index behaves differently with new_tab=False vs new_tab=True."""
@@ -303,7 +319,7 @@ class TestClickElementEvent:
initial_tab_count = len(tabs)
# Get browser state and find link elements
- state = await browser_session.get_browser_state_with_recovery()
+ state = await browser_session.get_browser_state_summary()
link_indices = []
for index, element in state.dom_state.selector_map.items():
if hasattr(element, 'tag_name') and element.tag_name == 'a':
@@ -711,3 +727,624 @@ class TestClickElementEvent:
assert 'select' in result.error.lower() and 'dropdown' in result.error.lower(), (
f'Error message should mention select/dropdown, got: {result.error}'
)
+
+ async def test_click_triggers_alert_popup(self, browser_session, base_url, http_server):
+ """Test that clicking a button triggers an alert dialog that is auto-accepted."""
+ from browser_use.browser.events import (
+ NavigateToUrlEvent,
+ BrowserStateRequestEvent,
+ ClickElementEvent,
+ DialogOpenedEvent
+ )
+
+ # Add route with alert dialog
+ http_server.expect_request('/alert_test').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Alert Test</title>
+ </head>
+ <body>
+ <h1>Alert Dialog Test</h1>
+ <button id="alertButton" onclick="alert('This is an alert!'); document.getElementById('result').textContent = 'Alert shown';">
+ Show Alert
+ </button>
+ <div id="result">No popup shown</div>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to the alert test page using events
+ nav_event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(url=f'{base_url}/alert_test')
+ )
+ await nav_event
+ await asyncio.sleep(0.5)
+
+ # Get the browser state to find clickable elements
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ browser_state = await state_event.event_result(raise_if_none=True, raise_if_any=True)
+
+ # Find the alert button
+ alert_button = None
+ for element in browser_state.dom_state.selector_map.values():
+ if element.attributes and element.attributes.get('id') == 'alertButton':
+ alert_button = element
+ break
+
+ assert alert_button is not None, 'Could not find alert button'
+
+ # Expect the DialogOpenedEvent
+ dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
+
+ # Click the alert button using ClickElementEvent
+ click_event = browser_session.event_bus.dispatch(
+ ClickElementEvent(node=alert_button)
+ )
+ await click_event
+
+ # Wait for and verify DialogOpenedEvent was dispatched
+ dialog_event = await asyncio.wait_for(dialog_event_future, timeout=2.0)
+ assert dialog_event.dialog_type == 'alert'
+ assert 'This is an alert!' in dialog_event.message
+
+ # Verify the page updated after alert was accepted
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ result_js = await cdp_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') == 'Alert shown'
+
+ async def test_click_triggers_confirm_popup(self, browser_session, base_url, http_server):
+ """Test that clicking a button triggers a confirm dialog that is auto-accepted."""
+ from browser_use.browser.events import (
+ NavigateToUrlEvent,
+ BrowserStateRequestEvent,
+ ClickElementEvent,
+ DialogOpenedEvent
+ )
+
+ # Add route with confirm dialog
+ http_server.expect_request('/confirm_test').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Confirm Test</title>
+ </head>
+ <body>
+ <h1>Confirm Dialog Test</h1>
+ <button id="confirmButton" onclick="if(confirm('Are you sure?')) { document.getElementById('result').textContent = 'Confirmed'; } else { document.getElementById('result').textContent = 'Cancelled'; }">
+ Show Confirm
+ </button>
+ <div id="result">No popup shown</div>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to the confirm test page
+ nav_event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(url=f'{base_url}/confirm_test')
+ )
+ await nav_event
+ await asyncio.sleep(0.5)
+
+ # Get the browser state
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ browser_state = await state_event.event_result(raise_if_none=True, raise_if_any=True)
+
+ # Find the confirm button
+ confirm_button = None
+ for element in browser_state.dom_state.selector_map.values():
+ if element.attributes and element.attributes.get('id') == 'confirmButton':
+ confirm_button = element
+ break
+
+ assert confirm_button is not None, 'Could not find confirm button'
+
+ # Expect the DialogOpenedEvent
+ dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
+
+ # Click the confirm button
+ click_event = browser_session.event_bus.dispatch(
+ ClickElementEvent(node=confirm_button)
+ )
+ await click_event
+
+ # Wait for and verify DialogOpenedEvent was dispatched
+ dialog_event = await asyncio.wait_for(dialog_event_future, timeout=2.0)
+ assert dialog_event.dialog_type == 'confirm'
+ assert 'Are you sure?' in dialog_event.message
+
+ # Verify the page updated after confirm was accepted (auto-accepts with True)
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ result_js = await cdp_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') == 'Confirmed'
+
+ async def test_page_usable_after_popup_confirm(self, browser_session, base_url, http_server):
+ """Test that the page remains usable after handling confirm dialogs."""
+ from browser_use.browser.events import (
+ NavigateToUrlEvent,
+ BrowserStateRequestEvent,
+ ClickElementEvent,
+ DialogOpenedEvent
+ )
+
+ # Add route with confirm dialog and navigation
+ http_server.expect_request('/popup_nav_test').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Popup Navigation Test</title>
+ </head>
+ <body>
+ <h1>Popup and Navigation Test</h1>
+ <button id="confirmButton" onclick="if(confirm('Continue to navigation?')) { document.getElementById('result').textContent = 'Ready to navigate'; }">
+ Show Confirm
+ </button>
+ <a href="/page1" id="navLink">Navigate to Page 1</a>
+ <div id="result">No popup shown</div>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to the test page
+ nav_event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(url=f'{base_url}/popup_nav_test')
+ )
+ await nav_event
+ await asyncio.sleep(0.5)
+
+ # Get browser state
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ browser_state = await state_event
+
+ # Find and click the confirm button
+ confirm_button = None
+ for element in browser_state.dom_state.selector_map.values():
+ if element.attributes and element.attributes.get('id') == 'confirmButton':
+ confirm_button = element
+ break
+
+ assert confirm_button is not None, 'Could not find confirm button'
+
+ # Expect dialog event
+ dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
+
+ # Click confirm button
+ click_event = browser_session.event_bus.dispatch(
+ ClickElementEvent(node=confirm_button)
+ )
+ await click_event
+
+ # Wait for dialog event
+ dialog_event = await asyncio.wait_for(dialog_event_future, timeout=2.0)
+ assert dialog_event.dialog_type == 'confirm'
+
+ # Verify page was updated
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ result_js = await cdp_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') == 'Ready to navigate'
+
+ # Refresh browser state after handling dialog
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ browser_state = await state_event
+
+ # Find and click navigation link to verify page is still usable
+ nav_link = None
+ for element in browser_state.dom_state.selector_map.values():
+ if element.attributes and element.attributes.get('id') == 'navLink':
+ nav_link = element
+ break
+
+ assert nav_link is not None, 'Could not find navigation link'
+
+ # Click the navigation link
+ click_event = browser_session.event_bus.dispatch(
+ ClickElementEvent(node=nav_link)
+ )
+ await click_event
+ await asyncio.sleep(1)
+
+ # Verify navigation succeeded
+ current_url = await browser_session.get_current_page_url()
+ assert f'{base_url}/page1' in current_url, f'Navigation failed, current URL: {current_url}'
+
+ # Verify browser is still responsive
+ current_title = await browser_session.get_current_page_title()
+ assert 'Test Page 1' in current_title, f'Page title incorrect: {current_title}'
+
+ async def test_click_triggers_onbeforeunload_popup(self, browser_session, base_url, http_server):
+ """Test that navigating away from a page with onbeforeunload triggers a dialog."""
+ from browser_use.browser.events import (
+ NavigateToUrlEvent,
+ BrowserStateRequestEvent,
+ ClickElementEvent,
+ DialogOpenedEvent
+ )
+
+ # Add route with onbeforeunload handler
+ http_server.expect_request('/beforeunload_test').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>BeforeUnload Test</title>
+ <script>
+ window.onbeforeunload = function(e) {
+ e.preventDefault();
+ e.returnValue = 'You have unsaved changes!';
+ return 'You have unsaved changes!';
+ };
+ </script>
+ </head>
+ <body>
+ <h1>BeforeUnload Test</h1>
+ <p>This page has unsaved changes.</p>
+ <a href="/page1" id="navLink">Navigate Away</a>
+ <div id="result">Page loaded</div>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to the beforeunload test page
+ nav_event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(url=f'{base_url}/beforeunload_test')
+ )
+ await nav_event
+ await asyncio.sleep(0.5)
+
+ # Get browser state
+ state_event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ browser_state = await state_event
+
+ # Find the navigation link
+ nav_link = None
+ for element in browser_state.dom_state.selector_map.values():
+ if element.attributes and element.attributes.get('id') == 'navLink':
+ nav_link = element
+ break
+
+ assert nav_link is not None, 'Could not find navigation link'
+
+ # Expect the DialogOpenedEvent for beforeunload
+ dialog_event_future = browser_session.event_bus.expect(DialogOpenedEvent)
+
+ # Click the navigation link - should trigger beforeunload popup
+ click_event = browser_session.event_bus.dispatch(
+ ClickElementEvent(node=nav_link)
+ )
+ await click_event
+
+ # Wait for and verify DialogOpenedEvent was dispatched
+ dialog_event = await asyncio.wait_for(dialog_event_future, timeout=2.0)
+ assert dialog_event.dialog_type == 'beforeunload'
+ # Note: beforeunload messages are often browser-controlled and may not match our custom message
+
+ # Wait a bit for navigation to complete after dialog is auto-accepted
+ await asyncio.sleep(1)
+
+ # Verify navigation succeeded after beforeunload was accepted
+ current_url = await browser_session.get_current_page_url()
+ assert f'{base_url}/page1' in current_url, f'Navigation should have succeeded after beforeunload was accepted, current URL: {current_url}'
+
+ async def test_file_upload_click_and_verify(self, controller, browser_session, base_url, http_server):
+ """Test that clicking a file upload element and uploading a file works correctly."""
+ # Create a temporary test file
+ import tempfile as temp_module
+ with temp_module.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file:
+ temp_file.write('Test file content for upload')
+ temp_file_path = temp_file.name
+
+ try:
+ # Add route for file upload test page
+ http_server.expect_request('/fileupload').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>File Upload Test</title>
+ <style>
+ .upload-section {
+ margin: 20px;
+ padding: 20px;
+ border: 2px dashed #ccc;
+ }
+ #fileInfo {
+ margin-top: 20px;
+ padding: 10px;
+ border: 1px solid #ddd;
+ min-height: 50px;
+ }
+ .upload-label {
+ display: inline-block;
+ padding: 10px 20px;
+ background-color: #4CAF50;
+ color: white;
+ cursor: pointer;
+ border-radius: 4px;
+ }
+ .upload-label:hover {
+ background-color: #45a049;
+ }
+ input[type="file"] {
+ /* Hide the default file input */
+ display: none;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>File Upload Test</h1>
+ <div class="upload-section">
+ <p>Click the button below to select a file:</p>
+ <label for="fileInput" class="upload-label">Choose File</label>
+ <input type="file" id="fileInput" name="fileInput" />
+ <div id="fileInfo">
+ <p id="fileName">No file selected</p>
+ <p id="fileSize"></p>
+ <p id="fileType"></p>
+ </div>
+ </div>
+
+ <script>
+ document.getElementById('fileInput').addEventListener('change', function(e) {
+ const file = e.target.files[0];
+ if (file) {
+ document.getElementById('fileName').textContent = 'File name: ' + file.name;
+ document.getElementById('fileSize').textContent = 'File size: ' + file.size + ' bytes';
+ document.getElementById('fileType').textContent = 'File type: ' + (file.type || 'unknown');
+ } else {
+ document.getElementById('fileName').textContent = 'No file selected';
+ document.getElementById('fileSize').textContent = '';
+ document.getElementById('fileType').textContent = '';
+ }
+ });
+ </script>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to the file upload test page
+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/fileupload', 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)
+
+ # Wait for the page to load
+ await asyncio.sleep(0.5)
+
+ # Initialize the DOM state to populate the selector map
+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
+
+ # Get the selector map
+ selector_map = await browser_session.get_selector_map()
+
+ # Find the label element that triggers the file input
+ label_index = None
+ for idx, element in selector_map.items():
+ if element.tag_name.lower() == 'label' and 'upload-label' in str(element.attributes.get('class', '')):
+ label_index = idx
+ break
+
+ assert label_index is not None, 'Could not find file upload label element'
+
+ # Create action model for file upload
+ class UploadFileActionModel(ActionModel):
+ upload_file_to_element: UploadFileAction | None = None
+
+ # Create a temporary FileSystem for the test
+ import tempfile
+ from browser_use.filesystem.file_system import FileSystem
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ file_system = FileSystem(base_dir=temp_dir)
+
+ # Upload the file using the label index (should find the associated file input)
+ result = await controller.act(
+ UploadFileActionModel(upload_file_to_element=UploadFileAction(index=label_index, path=temp_file_path)),
+ browser_session,
+ available_file_paths=[temp_file_path], # Pass the file path as available
+ file_system=file_system, # Pass the required file_system parameter
+ )
+
+ # Verify the upload action succeeded
+ assert result.error is None, f'File upload failed: {result.error}'
+ assert result.extracted_content is not None
+ assert 'Successfully uploaded file' in result.extracted_content
+
+ # Wait a moment for the JavaScript to process the file
+ await asyncio.sleep(0.5)
+
+ # Verify the file was actually selected using CDP Runtime.evaluate
+ cdp_session = await browser_session.get_or_create_cdp_session()
+
+ # Check if the file input has a file selected
+ file_check_js = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ (() => {
+ const input = document.getElementById('fileInput');
+ if (!input || !input.files || input.files.length === 0) {
+ return { hasFile: false };
+ }
+ const file = input.files[0];
+ return {
+ hasFile: true,
+ fileName: file.name,
+ fileSize: file.size,
+ fileType: file.type || 'text/plain'
+ };
+ })()
+ """,
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+
+ file_info = file_check_js.get('result', {}).get('value', {})
+
+ # Verify file was selected
+ assert file_info.get('hasFile') is True, 'File was not properly selected in the input element'
+ assert file_info.get('fileName', '').endswith('.txt'), f"Expected .txt file, got: {file_info.get('fileName')}"
+ assert file_info.get('fileSize', 0) > 0, 'File size should be greater than 0'
+
+ # Also verify the UI was updated (the file info div)
+ ui_check_js = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ (() => {
+ const fileName = document.getElementById('fileName').textContent;
+ const fileSize = document.getElementById('fileSize').textContent;
+ return {
+ fileNameText: fileName,
+ fileSizeText: fileSize,
+ hasFileInfo: !fileName.includes('No file selected')
+ };
+ })()
+ """,
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+
+ ui_info = ui_check_js.get('result', {}).get('value', {})
+
+ # Verify UI was updated
+ assert ui_info.get('hasFileInfo') is True, 'UI was not updated with file information'
+ assert '.txt' in ui_info.get('fileNameText', ''), f"File name not shown in UI: {ui_info.get('fileNameText')}"
+ assert 'bytes' in ui_info.get('fileSizeText', ''), f"File size not shown in UI: {ui_info.get('fileSizeText')}"
+
+ finally:
+ # Clean up the temporary file
+ Path(temp_file_path).unlink(missing_ok=True)
+
+ async def test_file_upload_path_validation(self, controller, browser_session, base_url, http_server):
+ """Test that file upload validates paths correctly with available_file_paths, downloaded_files, and FileSystem."""
+ import tempfile
+ from pathlib import Path
+ from browser_use.browser.views import BrowserError
+ from browser_use.filesystem.file_system import FileSystem
+ from browser_use.controller.views import UploadFileAction
+
+ # Create a temporary test file that's NOT in available_file_paths
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file:
+ temp_file.write('Test file content')
+ test_file_path = temp_file.name
+
+ try:
+ # Set up test page with file input
+ http_server.expect_request('/upload-test').respond_with_data(
+ """
+ <html>
+ <body>
+ <h1>File Upload Test</h1>
+ <input type="file" id="fileInput" />
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to the test page
+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/upload-test', 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 browser state to populate selector map
+ from browser_use.browser.events import BrowserStateRequestEvent
+ event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ state = await event
+
+ # Test 1: Try to upload a file that's not in available_file_paths - should fail
+ class UploadActionModel(ActionModel):
+ upload_file_to_element: UploadFileAction | None = None
+
+ upload_action = UploadActionModel(upload_file_to_element=UploadFileAction(index=1, path=test_file_path))
+
+ # Create a temporary FileSystem for all tests
+ with tempfile.TemporaryDirectory() as temp_dir:
+ file_system = FileSystem(base_dir=temp_dir)
+
+ try:
+ # This should fail because the file is not in available_file_paths
+ result = await controller.act(
+ upload_action,
+ browser_session,
+ available_file_paths=[], # Empty available_file_paths
+ file_system=file_system
+ )
+ assert result.error is not None, "Upload should have failed for file not in available_file_paths"
+ assert "not available" in result.error, f"Error message should mention file not available: {result.error}"
+ except BrowserError as e:
+ assert "not available" in str(e), f"Error should mention file not available: {e}"
+
+ # Test 2: Add file to available_file_paths - should succeed
+ result = await controller.act(
+ upload_action,
+ browser_session,
+ available_file_paths=[test_file_path], # File is now in available_file_paths
+ file_system=file_system
+ )
+ assert result.error is None, f"Upload should have succeeded with file in available_file_paths: {result.error}"
+
+ # Test 3: Test with FileSystem integration - write a test file to the FileSystem
+ await file_system.write_file('test.txt', 'FileSystem test content')
+ fs_file_path = str(file_system.get_dir() / 'test.txt')
+
+ # Try to upload using just the filename (should check FileSystem)
+ upload_action_fs = UploadActionModel(
+ upload_file_to_element=UploadFileAction(index=1, path='test.txt')
+ )
+
+ result = await controller.act(
+ upload_action_fs,
+ browser_session,
+ available_file_paths=[], # Empty available_file_paths
+ file_system=file_system # But FileSystem is provided
+ )
+ assert result.error is None, f"Upload should have succeeded with file in FileSystem: {result.error}"
+
+ # Test 4: Simulate a downloaded file
+ # Manually add a file to browser_session._downloaded_files to simulate a download
+ browser_session._downloaded_files.append(test_file_path)
+
+ # Try uploading with the file only in downloaded_files
+ result = await controller.act(
+ upload_action,
+ browser_session,
+ available_file_paths=[], # Empty available_file_paths, but file is in downloaded_files
+ file_system=file_system
+ )
+ assert result.error is None, f"Upload should have succeeded with file in downloaded_files: {result.error}"
+
+ finally:
+ # Clean up the temporary file
+ Path(test_file_path).unlink(missing_ok=True)
diff --git a/tests/ci/test_action_NavigateToUrlEvent.py b/tests/ci/test_browser_event_NavigateToUrlEvent.py
similarity index 100%
rename from tests/ci/test_action_NavigateToUrlEvent.py
rename to tests/ci/test_browser_event_NavigateToUrlEvent.py
diff --git a/tests/ci/test_action_ScrollEvent.py b/tests/ci/test_browser_event_ScrollEvent.py
similarity index 100%
rename from tests/ci/test_action_ScrollEvent.py
rename to tests/ci/test_browser_event_ScrollEvent.py
diff --git a/tests/ci/test_action_TypeTextEvent.py b/tests/ci/test_browser_event_TypeTextEvent.py
similarity index 100%
rename from tests/ci/test_action_TypeTextEvent.py
rename to tests/ci/test_browser_event_TypeTextEvent.py
diff --git a/tests/ci/test_browser_event_TypeTextEvent2.py b/tests/ci/test_browser_event_TypeTextEvent2.py
new file mode 100644
index 000000000..5aa786276
--- /dev/null
+++ b/tests/ci/test_browser_event_TypeTextEvent2.py
@@ -0,0 +1,682 @@
+import asyncio
+
+import pytest
+from pytest_httpserver import HTTPServer
+
+from browser_use.agent.views import ActionModel, ActionResult
+from browser_use.browser import BrowserSession
+from browser_use.browser.profile import BrowserProfile
+from browser_use.controller.service import Controller
+from browser_use.controller.views import (
+ GoToUrlAction,
+ InputTextAction,
+)
+
+
[email protected](scope='session')
+def http_server():
+ """Create and provide a test HTTP server that serves static content."""
+ server = HTTPServer()
+ server.start()
+
+ # Add routes for common test pages
+ server.expect_request('/').respond_with_data(
+ '<html><head><title>Test Home Page</title></head><body><h1>Test Home Page</h1><p>Welcome to the test site</p></body></html>',
+ content_type='text/html',
+ )
+
+ server.expect_request('/form').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Form Test Page</title>
+ </head>
+ <body>
+ <h1>Test Form</h1>
+ <form>
+ <input type="text" id="name" name="name" placeholder="Enter name">
+ <input type="email" id="email" name="email" placeholder="Enter email">
+ <textarea id="message" name="message" placeholder="Enter message"></textarea>
+ <input type="password" id="password" name="password" placeholder="Enter password">
+ <button type="submit">Submit</button>
+ </form>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ yield server
+ server.stop()
+
+
[email protected](scope='session')
+def base_url(http_server):
+ """Return the base URL for the test HTTP server."""
+ return f'http://{http_server.host}:{http_server.port}'
+
+
[email protected](scope='module')
+async def browser_session():
+ """Create and provide a Browser instance with security disabled."""
+ profile = BrowserProfile(headless=True, disable_security=True, cross_origin_iframes=False)
+ session = BrowserSession(browser_profile=profile)
+ await session.start()
+ yield session
+ await session.kill()
+
+
+def controller():
+ """Create and provide a Controller instance."""
+ return Controller()
+
+
+class TestTypeTextEvent:
+ """Test TypeTextEvent and input_text action functionality."""
+
+ async def test_input_text_action(self, controller, browser_session, base_url, http_server):
+ """Test that InputTextAction correctly inputs text into form fields."""
+ # Set up search form endpoint for this test
+ http_server.expect_request('/searchform').respond_with_data(
+ """
+ <html>
+ <head><title>Search Form</title></head>
+ <body>
+ <h1>Search Form</h1>
+ <form action="/search" method="get">
+ <input type="text" id="searchbox" name="q" placeholder="Search...">
+ <button type="submit">Search</button>
+ </form>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to a page with a form
+ goto_action = {'go_to_url': GoToUrlAction(url=f'{base_url}/searchform', new_tab=False)}
+
+ class GoToUrlActionModel(ActionModel):
+ go_to_url: GoToUrlAction | None = None
+
+ await controller.act(GoToUrlActionModel(**goto_action), browser_session)
+
+ # Wait for page to load
+ await asyncio.sleep(0.5)
+
+ # Get the search input field index
+ selector_map = await browser_session.get_selector_map()
+
+ # Find the search input field - this requires examining the DOM
+ # We'll mock this part since we can't rely on specific element indices
+ # In a real test, you would get the actual index from the selector map
+
+ # For demonstration, we'll just use a hard-coded mock value
+ # and check that the controller processes the action correctly
+ mock_input_index = 1 # This would normally be determined dynamically
+
+ # Create input text action
+ input_action = {'input_text': InputTextAction(index=mock_input_index, text='Python programming')}
+
+ class InputTextActionModel(ActionModel):
+ input_text: InputTextAction | None = None
+
+ # The actual input might fail if the page structure changes or in headless mode
+ # So we'll just verify the controller correctly processes the action
+ result = await controller.act(InputTextActionModel(**input_action), browser_session)
+
+ # Verify the result is an ActionResult
+ assert isinstance(result, ActionResult)
+
+ # Check if the action succeeded or failed
+ if result.error is None:
+ # Action succeeded, verify the extracted_content
+ assert result.extracted_content is not None
+ assert 'Input' in result.extracted_content
+ else:
+ # Action failed, verify the error message contains expected text
+ assert 'Element index' in result.error or 'does not exist' in result.error or 'Failed to input text' in result.error
+
+ async def test_type_text_event_directly(self, browser_session, base_url):
+ """Test TypeTextEvent directly through the event bus."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Navigate to a page with input fields
+ await browser_session._cdp_navigate(f'{base_url}/form')
+ await asyncio.sleep(0.5)
+
+ # Get the DOM state to find input elements
+ state = await browser_session.get_browser_state_summary()
+
+ # Find an input field
+ input_node = None
+ for node in state.dom_state.selector_map.values():
+ if node.tag_name == 'input' and node.attributes.get('type') == 'text':
+ input_node = node
+ break
+
+ if input_node:
+ # Test typing text into the input field
+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=input_node, text='Hello World', clear_existing=True))
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify the text was actually typed
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f'document.getElementById("{input_node.attributes.get("id")}").value',
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ assert value_check.get('result', {}).get('value') == 'Hello World'
+
+ async def test_type_text_clear_existing(self, browser_session, base_url):
+ """Test TypeTextEvent with clear_existing flag."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Navigate to form page
+ await browser_session._cdp_navigate(f'{base_url}/form')
+ await asyncio.sleep(0.5)
+
+ # Get DOM state
+ state = await browser_session.get_browser_state_summary()
+
+ # Find email input field
+ email_node = None
+ for node in state.dom_state.selector_map.values():
+ if node.tag_name == 'input' and node.attributes.get('type') == 'email':
+ email_node = node
+ break
+
+ if email_node:
+ # First, type some text without clearing
+ event = browser_session.event_bus.dispatch(
+ TypeTextEvent(node=email_node, text='[email protected]', clear_existing=False)
+ )
+ await asyncio.wait_for(event, timeout=3.0)
+
+ # Now type new text with clearing
+ event = browser_session.event_bus.dispatch(
+ TypeTextEvent(node=email_node, text='[email protected]', clear_existing=True)
+ )
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify only the second text is in the field
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f'document.getElementById("{email_node.attributes.get("id")}").value',
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ assert value_check.get('result', {}).get('value') == '[email protected]'
+
+ async def test_type_text_textarea(self, browser_session, base_url):
+ """Test typing text into a textarea element."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Navigate to form page
+ await browser_session._cdp_navigate(f'{base_url}/form')
+ await asyncio.sleep(0.5)
+
+ # Get DOM state
+ state = await browser_session.get_browser_state_summary()
+
+ # Find textarea element
+ textarea_node = None
+ for node in state.dom_state.selector_map.values():
+ if node.tag_name == 'textarea':
+ textarea_node = node
+ break
+
+ if textarea_node:
+ # Type multiline text
+ multiline_text = 'Line 1\nLine 2\nLine 3'
+ event = browser_session.event_bus.dispatch(
+ TypeTextEvent(node=textarea_node, text=multiline_text, clear_existing=True)
+ )
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify the multiline text was typed
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f'document.getElementById("{textarea_node.attributes.get("id")}").value',
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ assert value_check.get('result', {}).get('value') == multiline_text
+
+ async def test_type_text_password_field(self, browser_session, base_url):
+ """Test typing into a password field."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Navigate to form page
+ await browser_session._cdp_navigate(f'{base_url}/form')
+ await asyncio.sleep(0.5)
+
+ # Get DOM state
+ state = await browser_session.get_browser_state_summary()
+
+ # Find password input field
+ password_node = None
+ for node in state.dom_state.selector_map.values():
+ if node.tag_name == 'input' and node.attributes.get('type') == 'password':
+ password_node = node
+ break
+
+ if password_node:
+ # Type password text
+ password_text = 'SecureP@ssw0rd!'
+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=password_node, text=password_text, clear_existing=True))
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify the password was typed (value is present but masked visually)
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f'document.getElementById("{password_node.attributes.get("id")}").value',
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ assert value_check.get('result', {}).get('value') == password_text
+
+ async def test_type_text_readonly_field(self, browser_session, base_url, http_server):
+ """Test typing into a readonly field should handle gracefully."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Add page with readonly input
+ http_server.expect_request('/readonly').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head><title>Readonly Test</title></head>
+ <body>
+ <h1>Readonly Field Test</h1>
+ <input type="text" id="readonly-input" value="Cannot change this" readonly>
+ <input type="text" id="normal-input" placeholder="Can change this">
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to page
+ await browser_session._cdp_navigate(f'{base_url}/readonly')
+ await asyncio.sleep(0.5)
+
+ # Get DOM state
+ state = await browser_session.get_browser_state_summary()
+
+ # Find readonly input field
+ readonly_node = None
+ for node in state.dom_state.selector_map.values():
+ if node.tag_name == 'input' and node.attributes.get('id') == 'readonly-input':
+ readonly_node = node
+ break
+
+ if readonly_node:
+ # Try to type into readonly field
+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=readonly_node, text='New text', clear_existing=True))
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+
+ # The operation should complete (CDP allows typing into readonly fields)
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # But the value should remain unchanged due to readonly attribute
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.getElementById("readonly-input").value', 'returnByValue': True},
+ session_id=cdp_session.session_id,
+ )
+ # Readonly fields keep their original value
+ assert value_check.get('result', {}).get('value') == 'Cannot change this'
+
+ async def test_type_text_special_characters(self, browser_session, base_url):
+ """Test typing text with special characters."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Navigate to form page
+ await browser_session._cdp_navigate(f'{base_url}/form')
+ await asyncio.sleep(0.5)
+
+ # Get DOM state
+ state = await browser_session.get_browser_state_summary()
+
+ # Find text input field
+ text_node = None
+ for node in state.dom_state.selector_map.values():
+ if node.tag_name == 'input' and node.attributes.get('type') == 'text':
+ text_node = node
+ break
+
+ if text_node:
+ # Type text with special characters
+ special_text = 'Test @#$%^&*()_+-={}[]|\\:";<>?,./~`'
+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text=special_text, clear_existing=True))
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify the special characters were typed correctly
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f'document.getElementById("{text_node.attributes.get("id")}").value',
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ assert value_check.get('result', {}).get('value') == special_text
+
+ async def test_type_text_empty_string(self, browser_session, base_url):
+ """Test clearing a field by typing empty string with clear_existing=True."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Navigate to form page
+ await browser_session._cdp_navigate(f'{base_url}/form')
+ await asyncio.sleep(0.5)
+
+ # Get DOM state
+ state = await browser_session.get_browser_state_summary()
+
+ # Find text input field
+ text_node = None
+ for node in state.dom_state.selector_map.values():
+ if node.tag_name == 'input' and node.attributes.get('type') == 'text':
+ text_node = node
+ break
+
+ if text_node:
+ # First type some text
+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text='Initial text', clear_existing=True))
+ await asyncio.wait_for(event, timeout=3.0)
+
+ # Now clear it by typing empty string with clear_existing=True
+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=text_node, text='', clear_existing=True))
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify the field is now empty
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ value_check = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': f'document.getElementById("{text_node.attributes.get("id")}").value',
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ assert value_check.get('result', {}).get('value') == ''
+
+ async def test_type_text_index_zero_whole_page(self, browser_session, base_url, http_server):
+ """Test typing with index 0 types into the page (goes to whatever has focus)."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Create a page with an input that auto-focuses
+ http_server.expect_request('/autofocus-form').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head><title>Autofocus Form</title></head>
+ <body>
+ <h1>Form with Autofocus</h1>
+ <input type="text" id="first-input" placeholder="First input">
+ <input type="text" id="autofocus-input" placeholder="Has autofocus" autofocus>
+ <input type="text" id="third-input" placeholder="Third input">
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to page with autofocus
+ await browser_session._cdp_navigate(f'{base_url}/autofocus-form')
+ await asyncio.sleep(0.5)
+
+ # Get DOM state
+ state = await browser_session.get_browser_state_summary()
+
+ # Create a node with index 0 - this should type to the page (whatever has focus)
+ from browser_use.dom.views import EnhancedDOMTreeNode
+
+ mock_node = EnhancedDOMTreeNode(
+ element_index=0,
+ node_id=0,
+ backend_node_id=0,
+ session_id='',
+ frame_id='',
+ target_id='',
+ node_type=1,
+ node_name='body',
+ node_value='',
+ attributes={},
+ is_scrollable=False,
+ is_visible=True,
+ absolute_position=None,
+ content_document=None,
+ shadow_root_type=None,
+ shadow_roots=None,
+ parent_node=None,
+ children_nodes=[],
+ ax_node=None,
+ snapshot_node=None,
+ )
+
+ # Type text with index 0 - should type to whatever has focus (the autofocus input)
+ event = browser_session.event_bus.dispatch(TypeTextEvent(node=mock_node, text='Hello Page', clear_existing=False))
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify the text went into the autofocus input
+ cdp_session = await browser_session.get_or_create_cdp_session()
+
+ # Check that the autofocus input has the text
+ autofocus_value = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.getElementById("autofocus-input").value', 'returnByValue': True},
+ session_id=cdp_session.session_id,
+ )
+ assert autofocus_value.get('result', {}).get('value') == 'Hello Page', 'Text should have gone to the autofocus input'
+
+ # Check other inputs are empty
+ first_value = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.getElementById("first-input").value', 'returnByValue': True},
+ session_id=cdp_session.session_id,
+ )
+ assert first_value.get('result', {}).get('value') == '', 'First input should be empty'
+
+ third_value = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.getElementById("third-input").value', 'returnByValue': True},
+ session_id=cdp_session.session_id,
+ )
+ assert third_value.get('result', {}).get('value') == '', 'Third input should be empty'
+
+ async def test_type_text_nonexistent_element(self, browser_session, base_url):
+ """Test that typing into a non-existent element falls back to typing to the page."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Navigate to form page
+ await browser_session._cdp_navigate(f'{base_url}/form')
+ await asyncio.sleep(0.5)
+
+ # Focus the first input manually to have a known focused element
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ await browser_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.getElementById("name").focus()', 'returnByValue': True},
+ session_id=cdp_session.session_id,
+ )
+
+ # Create a node with a non-existent index
+ from browser_use.dom.views import EnhancedDOMTreeNode
+
+ nonexistent_node = EnhancedDOMTreeNode(
+ element_index=99999,
+ node_id=99999,
+ backend_node_id=99999, # Non-existent backend node
+ session_id='',
+ frame_id='',
+ target_id='',
+ node_type=1,
+ node_name='input',
+ node_value='',
+ attributes={'id': 'nonexistent'},
+ is_scrollable=False,
+ is_visible=True,
+ absolute_position=None,
+ content_document=None,
+ shadow_root_type=None,
+ shadow_roots=None,
+ parent_node=None,
+ children_nodes=[],
+ ax_node=None,
+ snapshot_node=None,
+ )
+
+ # Try to type into non-existent element - should fall back to typing to the page
+ event = browser_session.event_bus.dispatch(
+ TypeTextEvent(node=nonexistent_node, text='Fallback text', clear_existing=False)
+ )
+
+ # This should complete (might succeed by falling back to page typing)
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+
+ # Check if the text was typed to the focused element as a fallback
+ name_value = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.getElementById("name").value', 'returnByValue': True},
+ session_id=cdp_session.session_id,
+ )
+
+ # Either it failed (and returned an error) or it fell back to typing to the page
+ success = event_result.get('success', False)
+ if success:
+ # If it succeeded, it should have typed to the focused element
+ assert name_value.get('result', {}).get('value') == 'Fallback text', (
+ 'Text should have been typed to the focused element as fallback'
+ )
+ else:
+ # Or it might have failed entirely
+ assert event_result.get('error') is not None, "Should have an error if it didn't fall back"
+
+ async def test_type_text_iframe_input(self, browser_session, base_url, http_server):
+ """Test typing text into an input field inside an iframe."""
+ from browser_use.browser.events import TypeTextEvent
+
+ # Add iframe content page
+ http_server.expect_request('/iframe-form').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Iframe Form</title>
+ </head>
+ <body>
+ <h2>Iframe Form</h2>
+ <input type="text" id="iframe-input" name="iframe-field" placeholder="Type here in iframe">
+ <div id="iframe-result"></div>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Add main page with iframe
+ http_server.expect_request('/page-with-form-iframe').respond_with_data(
+ f"""
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Page with Form Iframe</title>
+ </head>
+ <body>
+ <h1>Main Page</h1>
+ <p>This page contains an iframe with a form:</p>
+ <iframe id="form-iframe" src="{base_url}/iframe-form" style="width: 100%; height: 300px; border: 2px solid #333;"></iframe>
+ <div>
+ <input type="text" id="main-input" placeholder="Main page input">
+ </div>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to page with iframe
+ await browser_session._cdp_navigate(f'{base_url}/page-with-form-iframe')
+ await asyncio.sleep(1.0) # Give iframe time to load
+
+ # Get DOM state to find the iframe input
+ state = await browser_session.get_browser_state_summary()
+
+ # Find the input inside the iframe (it should be in the DOM if same-origin)
+ iframe_input_node = None
+ for node in state.dom_state.selector_map.values():
+ # Look for the iframe input by its id
+ if node.tag_name == 'input' and node.attributes.get('id') == 'iframe-input':
+ iframe_input_node = node
+ break
+
+ if iframe_input_node:
+ # Type text into the iframe input
+ event = browser_session.event_bus.dispatch(
+ TypeTextEvent(node=iframe_input_node, text='Text in iframe', clear_existing=True)
+ )
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
+
+ # Verify the text was typed into the iframe input
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ iframe_value = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ (() => {
+ const iframe = document.getElementById('form-iframe');
+ if (iframe && iframe.contentDocument) {
+ const input = iframe.contentDocument.getElementById('iframe-input');
+ return input ? input.value : null;
+ }
+ return null;
+ })()
+ """,
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ assert iframe_value.get('result', {}).get('value') == 'Text in iframe'
+
+ # Verify the main page input is still empty
+ main_value = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.getElementById("main-input").value', 'returnByValue': True},
+ session_id=cdp_session.session_id,
+ )
+ assert main_value.get('result', {}).get('value') == '', 'Main page input should still be empty'
+ else:
+ # If cross-origin iframes are disabled, we won't see the iframe content
+ pytest.skip('Iframe input not found - likely due to cross_origin_iframes=False')
diff --git a/tests/ci/test_aboutblank_watchdog.py b/tests/ci/test_browser_watchdog_aboutblank.py
similarity index 100%
rename from tests/ci/test_aboutblank_watchdog.py
rename to tests/ci/test_browser_watchdog_aboutblank.py
diff --git a/tests/ci/test_watchdog.py b/tests/ci/test_browser_watchdog_base.py
similarity index 100%
rename from tests/ci/test_watchdog.py
rename to tests/ci/test_browser_watchdog_base.py
diff --git a/tests/ci/test_crash_watchdog.py b/tests/ci/test_browser_watchdog_crash.py
similarity index 100%
rename from tests/ci/test_crash_watchdog.py
rename to tests/ci/test_browser_watchdog_crash.py
diff --git a/tests/ci/test_downloads_watchdog.py b/tests/ci/test_browser_watchdog_downloads.py
similarity index 100%
rename from tests/ci/test_downloads_watchdog.py
rename to tests/ci/test_browser_watchdog_downloads.py
diff --git a/tests/ci/test_browser_watchdog_permissions.py b/tests/ci/test_browser_watchdog_permissions.py
new file mode 100644
index 000000000..d6b909358
--- /dev/null
+++ b/tests/ci/test_browser_watchdog_permissions.py
@@ -0,0 +1,189 @@
+"""Test that verifies permissions watchdog is working correctly."""
+
+import asyncio
+import pytest
+from browser_use import Agent
+from browser_use.browser.session import BrowserSession
+from browser_use.browser.profile import BrowserProfile
+from tests.ci.conftest import create_mock_llm
+
+
+async def test_permissions_are_granted_on_connect(httpserver, caplog):
+ """Test that permissions are granted when browser connects."""
+ # Set up test HTML that requests geolocation
+ httpserver.expect_request("/geo").respond_with_data(
+ """
+ <html>
+ <body>
+ <h1>Geolocation Test</h1>
+ <button id="get-location" onclick="getLocation()">Get Location</button>
+ <div id="result"></div>
+ <script>
+ function getLocation() {
+ if (navigator.geolocation) {
+ navigator.geolocation.getCurrentPosition(
+ position => {
+ document.getElementById('result').innerText =
+ `Lat: ${position.coords.latitude}, Lon: ${position.coords.longitude}`;
+ },
+ error => {
+ document.getElementById('result').innerText = `Error: ${error.message}`;
+ }
+ );
+ } else {
+ document.getElementById('result').innerText = 'Geolocation not supported';
+ }
+ }
+ </script>
+ </body>
+ </html>
+ """
+ )
+
+ # Create a profile with geolocation permissions
+ profile = BrowserProfile(
+ permissions=['geolocation'],
+ headless=True
+ )
+
+ # Create a mock LLM that will navigate and click the button
+ mock_llm = create_mock_llm([
+ """
+ {
+ "thinking": "Navigating to the geolocation test page",
+ "evaluation_previous_goal": "Starting",
+ "memory": "Need to navigate",
+ "next_goal": "Navigate to geo page",
+ "action": [
+ {
+ "go_to_url": {
+ "url": "%s"
+ }
+ }
+ ]
+ }
+ """ % httpserver.url_for('/geo'),
+ """
+ {
+ "thinking": "Clicking the get location button",
+ "evaluation_previous_goal": "Navigated successfully",
+ "memory": "On geo page",
+ "next_goal": "Click get location button",
+ "action": [
+ {
+ "click_element": {
+ "index": 0
+ }
+ }
+ ]
+ }
+ """
+ ])
+
+ # Create browser and agent
+ browser = BrowserSession(browser_profile=profile)
+ agent = Agent(task="Test geolocation permissions", browser=browser, llm=mock_llm)
+
+ # Run task
+ await agent.run(max_steps=3)
+
+ # Check that permissions were granted
+ grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Browser.grantPermissions" in record.message]
+ assert len(grant_logs) > 0, "Expected Browser.grantPermissions to be called"
+
+ # Check that the correct permission was granted
+ geo_permission_logs = [record for record in caplog.records if "geolocation" in record.message and ("granting" in record.message.lower() or "granted" in record.message.lower())]
+ assert len(geo_permission_logs) > 0, "Expected geolocation permission to be granted"
+
+ # Clean up
+ await browser.stop()
+
+
+async def test_multiple_permissions_are_granted(caplog):
+ """Test that multiple permissions are granted correctly."""
+ # Create a profile with multiple permissions
+ # Use CDP permission names directly
+ profile = BrowserProfile(
+ permissions=['geolocation', 'clipboardReadWrite', 'notifications'],
+ headless=True
+ )
+
+ # Create browser
+ browser = BrowserSession(browser_profile=profile)
+
+ # Start browser (this will trigger PermissionsWatchdog)
+ await browser.start()
+
+ # Give it time to initialize
+ await asyncio.sleep(0.5)
+
+ # Check that permissions were granted
+ grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Successfully granted permissions" in record.message]
+ assert len(grant_logs) > 0, "Expected permissions to be granted"
+
+ # Check that all permissions were mentioned
+ permissions_to_check = ['geolocation', 'clipboardReadWrite', 'notifications']
+ for perm in permissions_to_check:
+ perm_logs = [record for record in caplog.records if perm in record.message]
+ assert len(perm_logs) > 0, f"Expected {perm} permission to be granted"
+
+ # Clean up
+ await browser.stop()
+
+
+async def test_no_permissions_when_empty_list(caplog):
+ """Test that no permissions are granted when list is empty."""
+ # Create a profile with no permissions
+ profile = BrowserProfile(
+ permissions=[],
+ headless=True
+ )
+
+ # Create browser
+ browser = BrowserSession(browser_profile=profile)
+
+ # Start browser
+ await browser.start()
+
+ # Give it time to initialize
+ await asyncio.sleep(0.5)
+
+ # Check that permissions were not granted
+ grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Successfully granted permissions" in record.message]
+ assert len(grant_logs) == 0, "Expected no permissions to be granted when list is empty"
+
+ # Clean up
+ await browser.stop()
+
+
+async def test_permissions_watchdog_handles_invalid_permissions(caplog):
+ """Test that invalid permissions are handled gracefully."""
+ # Create a profile with an invalid permission
+ profile = BrowserProfile(
+ permissions=['geolocation', 'invalid-permission', 'clipboardReadWrite'],
+ headless=True
+ )
+
+ # Create browser
+ browser = BrowserSession(browser_profile=profile)
+
+ # Start browser (should not crash despite invalid permission)
+ await browser.start()
+
+ # Give it time to initialize
+ await asyncio.sleep(0.5)
+
+ # Check that permissions were still granted (despite invalid permission)
+ grant_logs = [record for record in caplog.records if "Granting browser permissions" in record.message or "Successfully granted permissions" in record.message]
+ assert len(grant_logs) > 0, "Expected permissions to be granted"
+
+ # Check for warning about invalid permission
+ warning_logs = [record for record in caplog.records if "invalid-permission" in record.message or "Unknown permission" in record.message]
+ # This is OK if there's no warning - CDP might just ignore it
+
+ # Clean up
+ await browser.stop()
\ No newline at end of file
diff --git a/tests/ci/test_security_watchdog.py b/tests/ci/test_browser_watchdog_security.py
similarity index 100%
rename from tests/ci/test_security_watchdog.py
rename to tests/ci/test_browser_watchdog_security.py
diff --git a/tests/ci/test_storage_state_watchdog.py b/tests/ci/test_browser_watchdog_storage_state.py
similarity index 100%
rename from tests/ci/test_storage_state_watchdog.py
rename to tests/ci/test_browser_watchdog_storage_state.py
diff --git a/tests/ci/test_cdp_new_tab_session.py b/tests/ci/test_cdp_new_tab_session.py
new file mode 100644
index 000000000..d8b40656e
--- /dev/null
+++ b/tests/ci/test_cdp_new_tab_session.py
@@ -0,0 +1,132 @@
+"""Test CDP session handling when creating new tabs."""
+
+import asyncio
+
+import pytest
+from browser_use.browser.events import NavigateToUrlEvent, TabCreatedEvent
+from browser_use.browser.session import BrowserSession
+from browser_use.browser.profile import BrowserProfile
+
+
+async def httpserver_url(httpserver):
+ """Create a local HTTP server for testing."""
+ httpserver.expect_request("/").respond_with_data(
+ """
+ <html>
+ <head><title>Test Page</title></head>
+ <body>
+ <h1>Test Page</h1>
+ <p>This is a test page</p>
+ </body>
+ </html>
+ """,
+ content_type="text/html",
+ )
+ return httpserver.url_for("/")
+
+
+async def test_new_tab_cdp_session_attachment(httpserver_url):
+ """Test that CDP session is properly attached when creating new tabs."""
+ browser = BrowserSession(browser_profile=BrowserProfile(
+ headless=True,
+ viewport={'width': 800, 'height': 600}
+ ))
+
+ tab_created_events = []
+
+ # Track TabCreatedEvent to verify it's dispatched correctly
+ browser.event_bus.on(TabCreatedEvent, lambda event: tab_created_events.append(event))
+
+ try:
+ await browser.start()
+
+ # Navigate to initial page
+ nav_event = browser.event_bus.dispatch(NavigateToUrlEvent(url=httpserver_url))
+ await nav_event
+
+ # Clear any initial tab created events
+ tab_created_events.clear()
+
+ # Now create a new tab - this should trigger the CDP error if not fixed
+ new_tab_event = browser.event_bus.dispatch(NavigateToUrlEvent(
+ url=httpserver_url,
+ new_tab=True
+ ))
+ await new_tab_event
+
+ # Wait a bit for all events to process
+ await asyncio.sleep(1)
+
+ # Verify that TabCreatedEvent was dispatched
+ assert len(tab_created_events) == 1, f"Expected 1 TabCreatedEvent, got {len(tab_created_events)}"
+ assert tab_created_events[0].url == httpserver_url
+
+ # Verify we have 2 tabs now
+ tabs = await browser.get_tabs()
+ assert len(tabs) == 2, f"Expected 2 tabs, got {len(tabs)}"
+
+ # Verify the CDP session is attached to the new tab
+ assert browser.agent_focus is not None
+ assert browser.agent_focus.target_id is not None
+
+ # Try to execute a CDP command on the new tab to verify it works
+ cdp_session = await browser.get_or_create_cdp_session()
+ result = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.title'},
+ session_id=cdp_session.session_id
+ )
+ assert result['result']['value'] == 'Test Page'
+
+ # Get browser state to verify DOM can be built on new tab
+ from browser_use.browser.events import BrowserStateRequestEvent
+ state_event = browser.event_bus.dispatch(BrowserStateRequestEvent())
+ state = await state_event.event_result()
+
+ # Verify state was retrieved without errors
+ assert state is not None
+ assert state.dom_state is not None
+
+ finally:
+ await browser.stop()
+
+
+async def test_multiple_new_tabs_cdp_session(httpserver_url):
+ """Test creating multiple new tabs in succession."""
+ browser = BrowserSession(browser_profile=BrowserProfile(
+ headless=True,
+ viewport={'width': 800, 'height': 600}
+ ))
+
+ try:
+ await browser.start()
+
+ # Navigate to initial page
+ nav_event = browser.event_bus.dispatch(NavigateToUrlEvent(url=httpserver_url))
+ await nav_event
+
+ # Create multiple new tabs quickly
+ for i in range(3):
+ new_tab_event = browser.event_bus.dispatch(NavigateToUrlEvent(
+ url=f"{httpserver_url}?tab={i}",
+ new_tab=True
+ ))
+ await new_tab_event
+
+ # Wait for events to process
+ await asyncio.sleep(1)
+
+ # Verify we have 4 tabs total (1 initial + 3 new)
+ tabs = await browser.get_tabs()
+ assert len(tabs) == 4, f"Expected 4 tabs, got {len(tabs)}"
+
+ # Verify CDP commands work on the current tab
+ cdp_session = await browser.get_or_create_cdp_session()
+ result = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'window.location.href'},
+ session_id=cdp_session.session_id
+ )
+ assert 'tab=2' in result['result']['value'], "Should be on the last created tab"
+
+ finally:
+ await browser.stop()
diff --git a/tests/ci/test_download_upload_full_circle.py b/tests/ci/test_download_upload_full_circle.py
new file mode 100644
index 000000000..d6c0dde82
--- /dev/null
+++ b/tests/ci/test_download_upload_full_circle.py
@@ -0,0 +1,360 @@
+"""Test full circle: download a file and then upload it back, verifying hash matches"""
+
+import asyncio
+import hashlib
+import tempfile
+from pathlib import Path
+
+import pytest
+from pytest_httpserver import HTTPServer
+
+from browser_use.agent.views import ActionModel, ActionResult
+from browser_use.browser import BrowserSession
+from browser_use.browser.events import FileDownloadedEvent, BrowserStateRequestEvent
+from browser_use.browser.profile import BrowserProfile
+from browser_use.controller.service import Controller
+from browser_use.controller.views import GoToUrlAction, ClickElementAction, UploadFileAction
+from browser_use.filesystem.file_system import FileSystem
+
+
[email protected](scope='function')
+def download_upload_server():
+ """Create a test HTTP server with download and upload endpoints."""
+ server = HTTPServer()
+ server.start()
+
+ # Test file content and hash
+ test_content = b'This is a test file for download-upload verification. Random: 12345'
+ test_hash = hashlib.sha256(test_content).hexdigest()
+
+ # Store uploaded files data
+ uploaded_files = []
+
+ # Add download endpoint
+ server.expect_request('/download/test-file.txt').respond_with_data(
+ test_content,
+ content_type='text/plain',
+ headers={'Content-Disposition': 'attachment; filename="test-file.txt"'}
+ )
+
+ # Add upload page
+ upload_page_html = """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Upload Test Page</title>
+ </head>
+ <body>
+ <h1>File Upload Test</h1>
+ <form id="uploadForm" action="/upload" method="POST" enctype="multipart/form-data">
+ <input type="file" id="fileInput" name="file" />
+ <button type="submit" id="submitButton">Upload File</button>
+ </form>
+ <div id="result"></div>
+
+ <script>
+ document.getElementById('uploadForm').addEventListener('submit', async (e) => {
+ e.preventDefault();
+ const formData = new FormData(e.target);
+ const file = formData.get('file');
+
+ if (file) {
+ // Read file content
+ const content = await file.text();
+
+ // Calculate SHA256 hash
+ const encoder = new TextEncoder();
+ const data = encoder.encode(content);
+ const hashBuffer = await crypto.subtle.digest('SHA-256', data);
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
+ const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
+
+ // Display result
+ document.getElementById('result').innerHTML = `
+ <p>File uploaded successfully!</p>
+ <p>Filename: <span id="uploadedFileName">${file.name}</span></p>
+ <p>Size: <span id="uploadedFileSize">${file.size}</span> bytes</p>
+ <p>SHA256: <span id="uploadedFileHash">${hashHex}</span></p>
+ `;
+
+ // Send to server for verification
+ fetch('/upload', {
+ method: 'POST',
+ body: formData
+ });
+ }
+ });
+ </script>
+ </body>
+ </html>
+ """
+
+ server.expect_request('/upload-page').respond_with_data(
+ upload_page_html,
+ content_type='text/html'
+ )
+
+ # Handle upload POST request (for server-side verification)
+ def handle_upload(request):
+ # Store uploaded file info for verification
+ if request.files and 'file' in request.files:
+ file_data = request.files['file'][0]
+ uploaded_files.append({
+ 'filename': file_data['filename'],
+ 'content': file_data['body'],
+ 'hash': hashlib.sha256(file_data['body']).hexdigest()
+ })
+ return request.make_response({'status': 'ok'})
+
+ server.expect_request('/upload', method='POST').respond_with_handler(handle_upload)
+
+ # Add download page with link
+ download_page_html = f"""
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Download Test Page</title>
+ </head>
+ <body>
+ <h1>File Download Test</h1>
+ <a id="downloadLink" href="/download/test-file.txt">Download Test File</a>
+ <p>Original file SHA256: <span id="originalHash">{test_hash}</span></p>
+ </body>
+ </html>
+ """
+
+ server.expect_request('/download-page').respond_with_data(
+ download_page_html,
+ content_type='text/html'
+ )
+
+ server.test_hash = test_hash
+ server.uploaded_files = uploaded_files
+
+ yield server
+ server.stop()
+
+
+class TestDownloadUploadFullCircle:
+ """Test full circle: download a file and then upload it back"""
+
+ async def test_download_then_upload_with_hash_verification(self, download_upload_server):
+ """Download a file, then upload it to another page, verify hash matches"""
+
+ # Create temporary directory for downloads
+ with tempfile.TemporaryDirectory() as tmpdir:
+ downloads_path = Path(tmpdir) / 'downloads'
+ downloads_path.mkdir()
+
+ # Create browser session with downloads enabled
+ browser_session = BrowserSession(
+ browser_profile=BrowserProfile(
+ headless=True,
+ downloads_path=str(downloads_path),
+ user_data_dir=None,
+ )
+ )
+
+ await browser_session.start()
+
+ # Create controller and file system
+ controller = Controller()
+ file_system = FileSystem(base_dir=tmpdir)
+
+ try:
+ base_url = f'http://{download_upload_server.host}:{download_upload_server.port}'
+
+ # Step 1: Navigate to download page
+ class GoToUrlActionModel(ActionModel):
+ go_to_url: GoToUrlAction | None = None
+
+ result = await controller.act(
+ GoToUrlActionModel(go_to_url=GoToUrlAction(url=f'{base_url}/download-page', new_tab=False)),
+ browser_session
+ )
+ assert result.error is None, f"Navigation to download page failed: {result.error}"
+
+ await asyncio.sleep(0.5)
+
+ # Get browser state to find download link
+ event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ state_result = await event.event_result()
+
+ # Find download link
+ download_link_index = None
+ for idx, element in state_result.dom_state.selector_map.items():
+ if element.attributes and element.attributes.get('id') == 'downloadLink':
+ download_link_index = idx
+ break
+
+ assert download_link_index is not None, "Download link not found"
+
+ # Step 2: Click download link and wait for download
+ class ClickActionModel(ActionModel):
+ click_element_by_index: ClickElementAction | None = None
+
+ # Click the download link
+ result = await controller.act(
+ ClickActionModel(click_element_by_index=ClickElementAction(index=download_link_index)),
+ browser_session
+ )
+ assert result.error is None, f"Click on download link failed: {result.error}"
+
+ # Wait for the download event
+ try:
+ download_event = await browser_session.event_bus.expect(FileDownloadedEvent, timeout=10.0)
+ downloaded_file_path = download_event.path
+ except asyncio.TimeoutError:
+ pytest.fail("Download did not complete within timeout")
+
+ assert downloaded_file_path is not None, "Downloaded file path is None"
+ assert Path(downloaded_file_path).exists(), f"Downloaded file does not exist: {downloaded_file_path}"
+
+ # Verify download is tracked by browser_session
+ assert downloaded_file_path in browser_session.downloaded_files, \
+ f"Downloaded file not tracked by browser_session: {downloaded_file_path}"
+
+ # Calculate hash of downloaded file
+ downloaded_content = Path(downloaded_file_path).read_bytes()
+ downloaded_hash = hashlib.sha256(downloaded_content).hexdigest()
+
+ print(f"✅ File downloaded: {downloaded_file_path}")
+ print(f" Original hash: {download_upload_server.test_hash}")
+ print(f" Downloaded hash: {downloaded_hash}")
+ assert downloaded_hash == download_upload_server.test_hash, "Downloaded file hash doesn't match original"
+
+ # Step 3: Navigate to upload page in a new tab
+ print(f"\n🔄 Opening upload page in new tab: {base_url}/upload-page")
+
+ # Debug: Check how many tabs we have before navigation
+ tabs_before = await browser_session.get_tabs()
+ print(f"📑 Tabs before navigation: {len(tabs_before)} tabs")
+ for i, tab in enumerate(tabs_before):
+ print(f" Tab {i}: {tab.url}")
+ result = await controller.act(
+ GoToUrlActionModel(go_to_url=GoToUrlAction(url=f'{base_url}/upload-page', new_tab=True)),
+ browser_session
+ )
+ assert result.error is None, f"Navigation to upload page failed: {result.error}"
+ print(f"✅ Navigation result: {result.extracted_content}")
+
+ # The new tab should be automatically focused after opening
+ await asyncio.sleep(2.0) # Give more time for the new tab to load and focus
+
+ # Debug: Get all tabs
+ tabs = await browser_session.get_tabs()
+ print(f"\n📑 All tabs after opening upload page:")
+ for i, tab in enumerate(tabs):
+ print(f" Tab {i}: {tab.url} - {tab.title}")
+
+ # Get browser state to find file input
+ event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ state_result = await event.event_result()
+
+ # Debug: print page URL and title
+ print(f"\n🔍 Getting DOM state:")
+ print(f" Current page URL: {state_result.url}")
+ print(f" Current page title: {state_result.title}")
+
+ # Find file input
+ file_input_index = None
+ input_elements = []
+ for idx, element in state_result.dom_state.selector_map.items():
+ if element.tag_name and element.tag_name.lower() == 'input':
+ input_elements.append((idx, element.attributes))
+ if element.attributes and element.attributes.get('type') == 'file':
+ file_input_index = idx
+ break
+
+ print(f"Found {len(input_elements)} input elements: {input_elements}")
+ assert file_input_index is not None, "File input not found"
+
+ # Step 4: Upload the downloaded file
+ class UploadActionModel(ActionModel):
+ upload_file_to_element: UploadFileAction | None = None
+
+ # The downloaded file should be automatically available for upload
+ result = await controller.act(
+ UploadActionModel(upload_file_to_element=UploadFileAction(
+ index=file_input_index,
+ path=downloaded_file_path
+ )),
+ browser_session,
+ available_file_paths=[], # Empty, but file is in downloaded_files
+ file_system=file_system
+ )
+ assert result.error is None, f"File upload failed: {result.error}"
+
+ # Step 4b: Click the submit button to trigger the form submission
+ # Get browser state to find submit button
+ event = browser_session.event_bus.dispatch(BrowserStateRequestEvent())
+ state_result = await event.event_result()
+
+ # Find submit button
+ submit_button_index = None
+ for idx, element in state_result.dom_state.selector_map.items():
+ if element.tag_name and element.tag_name.lower() == 'button' and element.attributes and element.attributes.get('id') == 'submitButton':
+ submit_button_index = idx
+ break
+
+ assert submit_button_index is not None, "Submit button not found"
+
+ # Click the submit button
+ result = await controller.act(
+ ClickActionModel(click_element_by_index=ClickElementAction(index=submit_button_index)),
+ browser_session
+ )
+ assert result.error is None, f"Click on submit button failed: {result.error}"
+
+ # Wait for JavaScript to process the upload
+ await asyncio.sleep(1.0)
+
+ # Step 5: Verify upload via JavaScript (client-side hash)
+ cdp_session = await browser_session.get_or_create_cdp_session()
+
+ # Get uploaded file details from the page
+ upload_verification = await browser_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ (() => {
+ const fileName = document.getElementById('uploadedFileName')?.textContent;
+ const fileSize = document.getElementById('uploadedFileSize')?.textContent;
+ const fileHash = document.getElementById('uploadedFileHash')?.textContent;
+
+ return {
+ fileName: fileName || null,
+ fileSize: fileSize || null,
+ fileHash: fileHash || null,
+ hasResult: !!document.getElementById('result').textContent.includes('successfully')
+ };
+ })()
+ """,
+ 'returnByValue': True
+ },
+ session_id=cdp_session.session_id
+ )
+
+ upload_info = upload_verification.get('result', {}).get('value', {})
+
+ # Verify upload was successful
+ assert upload_info.get('hasResult') is True, "Upload result not displayed"
+ assert upload_info.get('fileName') == 'test-file.txt', f"Uploaded filename mismatch: {upload_info.get('fileName')}"
+ assert upload_info.get('fileHash') == download_upload_server.test_hash, \
+ f"Uploaded file hash mismatch. Expected: {download_upload_server.test_hash}, Got: {upload_info.get('fileHash')}"
+
+ print(f"✅ File uploaded successfully!")
+ print(f" Uploaded filename: {upload_info.get('fileName')}")
+ print(f" Uploaded hash: {upload_info.get('fileHash')}")
+ print(f" Hash matches original: {upload_info.get('fileHash') == download_upload_server.test_hash}")
+
+ # Step 6: Verify server-side upload (if needed)
+ if download_upload_server.uploaded_files:
+ server_file = download_upload_server.uploaded_files[0]
+ assert server_file['hash'] == download_upload_server.test_hash, \
+ f"Server-side hash mismatch. Expected: {download_upload_server.test_hash}, Got: {server_file['hash']}"
+ print(f"✅ Server-side verification passed!")
+
+ print("\n🎉 Full circle test passed: Download → Upload with hash verification!")
+
+ finally:
+ await browser_session.stop()
\ No newline at end of file
diff --git a/tests/ci/test_multi_websocket.py b/tests/ci/test_multi_websocket.py
new file mode 100644
index 000000000..fe733c390
--- /dev/null
+++ b/tests/ci/test_multi_websocket.py
@@ -0,0 +1,215 @@
+"""Test multi-WebSocket CDP connections."""
+
+import asyncio
+import logging
+
+import pytest
+from pytest_httpserver import HTTPServer
+
+from browser_use.browser.session import BrowserSession
+
+# Set up logging
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(__name__)
+
+
+def test_html(httpserver: HTTPServer):
+ """Set up test HTML pages."""
+ # Main page HTML
+ main_html = """
+ <!DOCTYPE html>
+ <html>
+ <head><title>Main Page</title></head>
+ <body>
+ <h1 id="main-title">Main Page</h1>
+ <button id="test-button">Click Me</button>
+ <a href="/page2.html" target="_blank">Open Page 2</a>
+ </body>
+ </html>
+ """
+
+ # Second page HTML
+ page2_html = """
+ <!DOCTYPE html>
+ <html>
+ <head><title>Page 2</title></head>
+ <body>
+ <h1 id="page2-title">Page 2</h1>
+ <input id="test-input" type="text" placeholder="Enter text">
+ </body>
+ </html>
+ """
+
+ httpserver.expect_request("/").respond_with_data(main_html, content_type="text/html")
+ httpserver.expect_request("/page2.html").respond_with_data(page2_html, content_type="text/html")
+
+ return httpserver.url_for("/")
+
+
+async def test_shared_websocket_default(test_html):
+ """Test that default behavior uses shared WebSocket."""
+ browser_session = BrowserSession()
+
+ try:
+ await browser_session.start()
+
+ # Navigate to test page
+ await browser_session._cdp_navigate(test_html)
+
+ # Create new tab
+ target_id = await browser_session._cdp_create_new_page(test_html + "page2.html")
+
+ # Get sessions for both targets (explicitly request shared WebSocket)
+ session1 = browser_session.agent_focus
+ session2 = await browser_session.get_or_create_cdp_session(target_id, focus=False, new_socket=False)
+
+ # Both should use the same CDP client
+ assert session1.cdp_client is session2.cdp_client
+ assert not session1.owns_cdp_client
+ assert not session2.owns_cdp_client
+
+ logger.info("✅ Default behavior: Both sessions share the same WebSocket connection")
+
+ finally:
+ await browser_session.kill()
+
+
+async def test_dedicated_websocket_per_target(test_html):
+ """Test that new_socket=True creates separate WebSocket connections."""
+ browser_session = BrowserSession(headless=True, user_data_dir=None)
+
+ try:
+ await browser_session.start()
+
+ # Navigate to test page
+ await browser_session._cdp_navigate(test_html)
+
+ # Create new tab
+ target_id = await browser_session._cdp_create_new_page(test_html + "page2.html")
+
+ # Get session for first target (uses shared WebSocket)
+ session1 = browser_session.agent_focus
+
+ # Get session for second target with dedicated WebSocket
+ session2 = await browser_session.get_or_create_cdp_session(
+ target_id,
+ focus=False,
+ new_socket=True
+ )
+
+ # Sessions should use different CDP clients
+ assert session1.cdp_client is not session2.cdp_client
+ assert not session1.owns_cdp_client
+ assert session2.owns_cdp_client
+
+ # Both sessions should work independently
+ # Test session1
+ result1 = await session1.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.title'},
+ session_id=session1.session_id
+ )
+ assert result1['result']['value'] == 'Main Page'
+
+ # Test session2
+ result2 = await session2.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.title'},
+ session_id=session2.session_id
+ )
+ assert result2['result']['value'] == 'Page 2'
+
+ logger.info("✅ Multi-WebSocket: Each target has its own WebSocket connection")
+
+ finally:
+ await browser_session.kill()
+
+
+async def test_websocket_cleanup(test_html):
+ """Test that WebSocket connections are properly cleaned up."""
+ browser_session = BrowserSession()
+
+ try:
+ await browser_session.start()
+
+ # Create multiple tabs with dedicated WebSockets
+ target_ids = []
+ for i in range(3):
+ target_id = await browser_session._cdp_create_new_page(f"{test_html}#tab{i}")
+ target_ids.append(target_id)
+
+ # Create session with dedicated WebSocket
+ session = await browser_session.get_or_create_cdp_session(
+ target_id,
+ focus=False,
+ new_socket=True
+ )
+ assert session.owns_cdp_client
+
+ # Should have 3 sessions in the pool (plus the initial one)
+ assert len(browser_session._cdp_session_pool) >= 3
+
+ # Reset should clean up all WebSocket connections
+ await browser_session.reset()
+
+ # Pool should be empty
+ assert len(browser_session._cdp_session_pool) == 0
+
+ logger.info("✅ Cleanup: All WebSocket connections properly disconnected")
+
+ finally:
+ await browser_session.kill()
+
+
+async def test_mixed_websocket_sessions(test_html):
+ """Test mixing shared and dedicated WebSocket sessions."""
+ browser_session = BrowserSession()
+
+ try:
+ await browser_session.start()
+
+ # Create multiple tabs
+ target_ids = []
+ for i in range(4):
+ target_id = await browser_session._cdp_create_new_page(f"{test_html}#tab{i}")
+ target_ids.append(target_id)
+
+ # Create sessions with mixed WebSocket types
+ session_shared1 = await browser_session.get_or_create_cdp_session(
+ target_ids[0], focus=False, new_socket=False
+ )
+ session_dedicated1 = await browser_session.get_or_create_cdp_session(
+ target_ids[1], focus=False, new_socket=True
+ )
+ session_shared2 = await browser_session.get_or_create_cdp_session(
+ target_ids[2], focus=False, new_socket=False
+ )
+ session_dedicated2 = await browser_session.get_or_create_cdp_session(
+ target_ids[3], focus=False, new_socket=True
+ )
+
+ # Verify ownership
+ assert not session_shared1.owns_cdp_client
+ assert session_dedicated1.owns_cdp_client
+ assert not session_shared2.owns_cdp_client
+ assert session_dedicated2.owns_cdp_client
+
+ # Shared sessions should use the same client
+ assert session_shared1.cdp_client is session_shared2.cdp_client
+
+ # Dedicated sessions should use different clients
+ assert session_dedicated1.cdp_client is not session_dedicated2.cdp_client
+ assert session_dedicated1.cdp_client is not session_shared1.cdp_client
+
+ logger.info("✅ Mixed mode: Shared and dedicated WebSockets work together")
+
+ finally:
+ await browser_session.kill()
+
+
+if __name__ == "__main__":
+ # Run with: python -m pytest tests/ci/test_multi_websocket.py -v
+ pytest.main([__file__, "-v", "-s"])
diff --git a/tests/ci/test_search_google_tab_focus.py b/tests/ci/test_search_google_tab_focus.py
new file mode 100644
index 000000000..6ea1db1b0
--- /dev/null
+++ b/tests/ci/test_search_google_tab_focus.py
@@ -0,0 +1,147 @@
+"""Test that search_google properly switches focus to the new tab."""
+
+import asyncio
+
+import pytest
+from bubus import EventBus
+
+from browser_use.browser import BrowserSession
+from browser_use.browser.events import BrowserStateRequestEvent, NavigateToUrlEvent
+from browser_use.controller.service import Controller
+
+
+async def browser_session():
+ """Create a browser session for testing."""
+ session = BrowserSession()
+ await session.start()
+ yield session
+ await session.close()
+
+
+async def test_search_google_creates_and_focuses_new_tab(browser_session: BrowserSession):
+ """Test that search_google creates a new tab and properly switches focus to it."""
+ # Create controller to get the search_google action
+ controller = Controller()
+
+ # Get initial browser state
+ initial_state_event = browser_session.event_bus.dispatch(
+ BrowserStateRequestEvent(include_screenshot=False)
+ )
+ initial_state = await initial_state_event
+ initial_url = initial_state.url
+ initial_tabs_count = len(initial_state.tabs)
+
+ # Execute search_google action
+ action_result = await controller.registry.execute_action(
+ action_name='search_google',
+ params={'query': 'test search'},
+ browser_session=browser_session,
+ )
+
+ # Small delay to ensure navigation completes
+ await asyncio.sleep(1)
+
+ # Get browser state after search
+ state_event = browser_session.event_bus.dispatch(
+ BrowserStateRequestEvent(include_screenshot=False)
+ )
+ state_after = await state_event
+
+ # Verify a new tab was created
+ assert len(state_after.tabs) == initial_tabs_count + 1, f"Expected {initial_tabs_count + 1} tabs, got {len(state_after.tabs)}"
+
+ # Verify the current URL is Google search, not about:blank
+ assert 'google.com/search' in state_after.url, f"Expected Google search URL, got {state_after.url}"
+ assert state_after.url != initial_url, f"URL didn't change from {initial_url}"
+ assert 'about:blank' not in state_after.url, "Agent is still on about:blank after search_google"
+
+ # Verify the search query is in the URL
+ assert 'test+search' in state_after.url or 'test%20search' in state_after.url, f"Query not found in URL: {state_after.url}"
+
+ print(f"✅ Test passed! Agent correctly focused on Google tab: {state_after.url}")
+
+
+async def test_navigate_with_new_tab_focuses_properly(browser_session: BrowserSession):
+ """Test that NavigateToUrlEvent with new_tab=True properly switches focus."""
+ # Get initial state
+ initial_state_event = browser_session.event_bus.dispatch(
+ BrowserStateRequestEvent(include_screenshot=False)
+ )
+ initial_state = await initial_state_event
+ initial_tabs_count = len(initial_state.tabs)
+
+ # Navigate to a URL in a new tab
+ nav_event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(url='https://example.com', new_tab=True)
+ )
+ await nav_event
+
+ # Small delay to ensure navigation completes
+ await asyncio.sleep(1)
+
+ # Get browser state after navigation
+ state_event = browser_session.event_bus.dispatch(
+ BrowserStateRequestEvent(include_screenshot=False)
+ )
+ state_after = await state_event
+
+ # Verify a new tab was created
+ assert len(state_after.tabs) == initial_tabs_count + 1, f"Expected {initial_tabs_count + 1} tabs, got {len(state_after.tabs)}"
+
+ # Verify the current URL is the navigated URL
+ assert 'example.com' in state_after.url, f"Expected example.com URL, got {state_after.url}"
+ assert 'about:blank' not in state_after.url, "Agent is still on about:blank after new tab navigation"
+
+ print(f"✅ Test passed! Agent correctly focused on new tab: {state_after.url}")
+
+
+async def test_multiple_new_tabs_focus_on_latest(browser_session: BrowserSession):
+ """Test that creating multiple new tabs focuses on the most recent one."""
+ # Navigate to first new tab
+ nav1_event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(url='https://example.com', new_tab=True)
+ )
+ await nav1_event
+ await asyncio.sleep(0.5)
+
+ # Navigate to second new tab
+ nav2_event = browser_session.event_bus.dispatch(
+ NavigateToUrlEvent(url='https://github.com', new_tab=True)
+ )
+ await nav2_event
+ await asyncio.sleep(0.5)
+
+ # Get browser state
+ state_event = browser_session.event_bus.dispatch(
+ BrowserStateRequestEvent(include_screenshot=False)
+ )
+ state = await state_event
+
+ # Should be focused on the most recent tab (github.com)
+ assert 'github.com' in state.url, f"Expected github.com URL, got {state.url}"
+ assert len(state.tabs) >= 3, f"Expected at least 3 tabs, got {len(state.tabs)}"
+
+ print(f"✅ Test passed! Agent correctly focused on latest tab: {state.url}")
+
+
+if __name__ == "__main__":
+ # Run tests directly
+ async def run_all_tests():
+ session = BrowserSession()
+ await session.start()
+ try:
+ print("Running test_search_google_creates_and_focuses_new_tab...")
+ await test_search_google_creates_and_focuses_new_tab(session)
+
+ print("\nRunning test_navigate_with_new_tab_focuses_properly...")
+ await test_navigate_with_new_tab_focuses_properly(session)
+
+ print("\nRunning test_multiple_new_tabs_focus_on_latest...")
+ await test_multiple_new_tabs_focus_on_latest(session)
+
+ print("\n✅ All tests passed!")
+ finally:
+ await session.close()
+
+ asyncio.run(run_all_tests())
\ No newline at end of file