Skip to content

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

dont click on file inputs or dropdowns

Nick Sweeting77e18cf79c1a
diff --git a/browser_use/agent/prompts.py b/browser_use/agent/prompts.py
index 46a682fb8..374dcd7c2 100644
--- a/browser_use/agent/prompts.py
+++ b/browser_use/agent/prompts.py
@@ -92,6 +92,7 @@ class AgentMessagePrompt:
available_file_paths: list[str] | None = None,
screenshots: list[str] | None = None,
vision_detail_level: Literal['auto', 'low', 'high'] = 'auto',
+ include_recent_events: bool = False,
):
self.browser_state: 'BrowserStateSummary' = browser_state_summary
self.file_system: 'FileSystem | None' = file_system
@@ -106,6 +107,7 @@ class AgentMessagePrompt:
self.available_file_paths: list[str] | None = available_file_paths
self.screenshots = screenshots or []
self.vision_detail_level = vision_detail_level
+ self.include_recent_events = include_recent_events
assert self.browser_state
@observe_debug(ignore_input=True, ignore_output=True, name='_get_browser_state_description')
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index f2469dae3..56e85a930 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -184,7 +184,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
llm_timeout: int = 60,
step_timeout: int = 180,
preload: bool = False,
- recent_events: bool = False,
+ include_recent_events: bool = False,
**kwargs,
):
if not isinstance(llm, BaseChatModel):
@@ -233,7 +233,7 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self.task = task
self.llm = llm
self.preload = preload
- self.recent_events = recent_events
+ self.include_recent_events = include_recent_events
self.controller = (
controller if controller is not None else Controller(display_files_in_done_text=display_files_in_done_text)
)
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.py
index 0a2e63eb7..12fb00396 100644
--- a/browser_use/browser/default_action_watchdog.py
+++ b/browser_use/browser/default_action_watchdog.py
@@ -197,6 +197,16 @@ class DefaultActionWatchdog(BaseWatchdog):
"""
try:
+ # Check if element is a file input or select dropdown - these should not be clicked
+ tag_name = element_node.tag_name.lower() if element_node.tag_name else ''
+ element_type = element_node.attributes.get('type', '').lower() if element_node.attributes else ''
+
+ if tag_name == 'select':
+ raise Exception('Cannot click on <select> elements. Use select_dropdown_option action instead.')
+
+ if tag_name == 'input' and element_type == 'file':
+ raise Exception('Cannot click on file input elements. File uploads must be handled programmatically.')
+
# Get CDP client
cdp_session = await self.browser_session.get_or_create_cdp_session()
@@ -427,7 +437,7 @@ class DefaultActionWatchdog(BaseWatchdog):
session_id=session_id,
)
await asyncio.sleep(0.5)
- # Navigation is handled by NavigationWatchdog via events
+ # Navigation is handled by BrowserSession via events
return None
except Exception as js_e:
self.logger.error(f'CDP JavaScript click also failed: {js_e}')
@@ -649,7 +659,7 @@ class DefaultActionWatchdog(BaseWatchdog):
# Wait for navigation
await asyncio.sleep(0.5)
- # Navigation is handled by NavigationWatchdog via events
+ # Navigation is handled by BrowserSession via events
self.logger.info(f'πŸ”™ Navigated back to {entries[current_index - 1]["url"]}')
except Exception as e:
@@ -682,7 +692,7 @@ class DefaultActionWatchdog(BaseWatchdog):
# Wait for navigation
await asyncio.sleep(0.5)
- # Navigation is handled by NavigationWatchdog via events
+ # Navigation is handled by BrowserSession via events
self.logger.info(f'πŸ”œ Navigated forward to {entries[current_index + 1]["url"]}')
except Exception as e:
@@ -710,7 +720,7 @@ class DefaultActionWatchdog(BaseWatchdog):
if self.browser_session._dom_watchdog:
self.browser_session._dom_watchdog.clear_cache()
- # Navigation is handled by NavigationWatchdog via events
+ # Navigation is handled by BrowserSession via events
self.logger.info('πŸ”„ Target refreshed')
except Exception as e:
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 2f4d2a0b7..603528c58 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -2,11 +2,10 @@
import asyncio
import logging
+import os
from pathlib import Path
from typing import Any, Self, cast
-# Silence cdp_use logging - it's too verbose at DEBUG level
-# We need to patch the logger in the cdp_use.client module directly
import cdp_use.client
import httpx
from bubus import EventBus
@@ -15,13 +14,17 @@ from cdp_use.cdp.network import Cookie
from pydantic import BaseModel, ConfigDict, Field, PrivateAttr
from uuid_extensions import uuid7str
-cdp_use.client.logger.setLevel(logging.ERROR)
-
-# Also configure all cdp_use loggers
-for logger_name in ['cdp_use', 'cdp_use.client', 'cdp_use.cdp', 'cdp_use.cdp.registry']:
- cdp_logger = logging.getLogger(logger_name)
- cdp_logger.setLevel(logging.ERROR)
- cdp_logger.propagate = False # Don't propagate to root logger
+# Note: CDP logging level is controlled by the CLI when needed
+# By default we silence it unless explicitly enabled
+if os.environ.get('BROWSER_USE_CDP_DEBUG') != 'true':
+ # Silence cdp_use logging - it's too verbose at DEBUG level
+ cdp_use.client.logger.setLevel(logging.ERROR)
+
+ # Also configure all cdp_use loggers
+ for logger_name in ['cdp_use', 'cdp_use.client', 'cdp_use.cdp', 'cdp_use.cdp.registry']:
+ cdp_logger = logging.getLogger(logger_name)
+ cdp_logger.setLevel(logging.ERROR)
+ cdp_logger.propagate = False # Don't propagate to root logger
from browser_use.browser.events import (
AgentFocusChangedEvent,
@@ -83,18 +86,18 @@ class CDPSession(BaseModel):
owns_cdp_client: bool = False
@classmethod
- async def for_target(cls, cdp_client: CDPClient, target_id: str, create_own_connection: 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):
"""Create a CDP session for a target.
Args:
cdp_client: Existing CDP client to use (or just for reference if creating own)
target_id: Target ID to attach to
- create_own_connection: If True, create a dedicated WebSocket connection for this target
- cdp_url: CDP URL (required if create_own_connection is True)
+ new_socket: If True, create a dedicated WebSocket connection for this target
+ cdp_url: CDP URL (required if new_socket is True)
"""
- if create_own_connection:
+ if new_socket:
if not cdp_url:
- raise ValueError('cdp_url required when create_own_connection=True')
+ raise ValueError('cdp_url required when new_socket=True')
# Create a new CDP client with its own WebSocket connection
import logging
logger = logging.getLogger(f'browser_use.CDPSession.{target_id[-4:]}')
@@ -245,6 +248,12 @@ class BrowserSession(BaseModel):
# TODO: clear the event bus queue here, implement this helper
# await self.event_bus.wait_for_idle(timeout=5.0)
# await self.event_bus.clear()
+
+ # Disconnect sessions that own their WebSocket connections
+ for session in self._cdp_session_pool.values():
+ if hasattr(session, 'disconnect'):
+ await session.disconnect()
+ self._cdp_session_pool.clear()
self._cdp_client_root = None # type: ignore
self._cached_browser_state_summary = None
@@ -534,12 +543,13 @@ class BrowserSession(BaseModel):
assert self._cdp_client_root is not None, 'CDP client not initialized - browser may not be connected yet'
return self._cdp_client_root
- async def get_or_create_cdp_session(self, target_id: str | None = None, focus: bool = True) -> CDPSession:
+ async def get_or_create_cdp_session(self, target_id: str | None = None, focus: bool = True, new_socket: bool = False) -> CDPSession:
"""Get or create a CDP session for a target.
Args:
target_id: Target ID to get session for. If None, uses current agent focus.
focus: If True, switches agent focus to this target. If False, just returns session without changing focus.
+ new_socket: If True, create a dedicated WebSocket connection for this target.
Returns:
CDPSession for the specified target.
@@ -570,8 +580,13 @@ class BrowserSession(BaseModel):
return self.agent_focus
# Create new session for this target
- self.logger.debug(f'[get_or_create_cdp_session] Creating new CDP session for target {target_id}')
- session = await CDPSession.for_target(self._cdp_client_root, target_id)
+ self.logger.debug(f'[get_or_create_cdp_session] Creating new CDP session for target {target_id} (new_socket={new_socket})')
+ session = await CDPSession.for_target(
+ self._cdp_client_root,
+ target_id,
+ new_socket=new_socket,
+ cdp_url=self.cdp_url if new_socket else None
+ )
self._cdp_session_pool[target_id] = session
# Only change agent focus if requested
diff --git a/browser_use/cli.py b/browser_use/cli.py
index 99d4a3a98..b4c0fe2dd 100644
--- a/browser_use/cli.py
+++ b/browser_use/cli.py
@@ -688,6 +688,13 @@ class BrowserUseApp(App):
def setup_cdp_logger(self) -> None:
"""Setup CDP message logger."""
+ # Enable CDP debug logging
+ os.environ['BROWSER_USE_CDP_DEBUG'] = 'true'
+
+ # Need to re-import cdp_use.client to apply the new setting
+ import cdp_use.client
+ cdp_use.client.logger.setLevel(logging.DEBUG)
+
# Get the CDP log widget
cdp_log = self.query_one('#cdp-log', RichLog)
@@ -719,7 +726,7 @@ class BrowserUseApp(App):
cdp_handler.setLevel(logging.DEBUG)
# Configure cdp_use loggers to use our handler
- for logger_name in ['cdp_use', 'cdp_use.client', 'cdp_use.cdp']:
+ for logger_name in ['cdp_use', 'cdp_use.client', 'cdp_use.cdp', 'cdp_use.cdp.registry']:
cdp_logger = logging.getLogger(logger_name)
cdp_logger.setLevel(logging.DEBUG)
cdp_logger.handlers = [cdp_handler] # Replace existing handlers
@@ -759,9 +766,9 @@ class BrowserUseApp(App):
# Update our browser_session reference to point to the agent's
if hasattr(self.agent, 'browser_session'):
self.browser_session = self.agent.browser_session
- # Set up event bus listener and CDP logger now that we have the browser session
- self.setup_event_bus_listener()
- self.setup_cdp_logger()
+ # Set up event bus listener if not already set up
+ if self._event_bus_handler_id is None:
+ self.setup_event_bus_listener()
else:
self.agent.add_new_task(task)
@@ -929,24 +936,15 @@ class BrowserUseApp(App):
with Container(id='three-column-container'):
# Column 1: Main output
with VerticalScroll(id='main-output-column'):
- rich_log = RichLog(highlight=True, markup=True, id='main-output-log', wrap=True, auto_scroll=True)
- rich_log.write('[bold blue]Agent Output[/]')
- rich_log.write('-' * 20)
- yield rich_log
+ yield RichLog(highlight=True, markup=True, id='main-output-log', wrap=True, auto_scroll=True)
# Column 2: Event bus events
with VerticalScroll(id='events-column'):
- events_log = RichLog(highlight=True, markup=True, id='events-log', wrap=True, auto_scroll=True)
- events_log.write('[bold yellow]Browser Events[/]')
- events_log.write('-' * 20)
- yield events_log
+ yield RichLog(highlight=True, markup=True, id='events-log', wrap=True, auto_scroll=True)
# Column 3: CDP messages
with VerticalScroll(id='cdp-column'):
- cdp_log = RichLog(highlight=True, markup=True, id='cdp-log', wrap=True, auto_scroll=True)
- cdp_log.write('[bold cyan]CDP Messages[/]')
- cdp_log.write('-' * 20)
- yield cdp_log
+ yield RichLog(highlight=True, markup=True, id='cdp-log', wrap=True, auto_scroll=True)
# Task input container (now at the bottom)
with Container(id='task-input-container'):
diff --git a/tests/ci/test_ClickElementEvent.py b/tests/ci/test_ClickElementEvent.py
index 855de8041..0e1034929 100644
--- a/tests/ci/test_ClickElementEvent.py
+++ b/tests/ci/test_ClickElementEvent.py
@@ -188,10 +188,7 @@ class TestClickElementEvent:
assert f'Clicked element with index {button_index}' in result.extracted_content, (
f'Expected click confirmation in result content, got: {result.extracted_content}'
)
- if button_text:
- assert result.extracted_content is not None and button_text in result.extracted_content, (
- f"Button text '{button_text}' not found in result content: {result.extracted_content}"
- )
+ # Note: The click action doesn't include button text in the result, only the index
# Verify the click actually had an effect on the page using CDP
cdp_session = await browser_session.get_or_create_cdp_session(browser_session.agent_focus)
diff --git a/tests/ci/test_ScrollEvent.py b/tests/ci/test_ScrollEvent.py
index 82fe66814..370c20c7d 100644
--- a/tests/ci/test_ScrollEvent.py
+++ b/tests/ci/test_ScrollEvent.py
@@ -3,12 +3,11 @@ import asyncio
import pytest
from pytest_httpserver import HTTPServer
-from browser_use.agent.views import ActionResult
+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 (
- ActionModel,
GoToUrlAction,
ScrollAction,
)
@@ -166,25 +165,89 @@ class TestScrollActions:
assert event_result is not None
assert event_result.get('success') is True
- async def test_scroll_with_element_node(self, browser_session, base_url):
- """Test scrolling a specific element via node."""
+ async def test_scroll_with_element_node(self, browser_session, base_url, http_server):
+ """Test scrolling a specific element via node parameter."""
from browser_use.browser.events import ScrollEvent
- from browser_use.dom.service import EnhancedDOMTreeNode
- # Navigate to scrollable page
- await browser_session._cdp_navigate(f'{base_url}/scrollable')
+ # Add a page with a scrollable div element
+ http_server.expect_request('/scrollable-div').respond_with_data(
+ """
+ <!DOCTYPE html>
+ <html>
+ <head>
+ <title>Scrollable Div Test</title>
+ <style>
+ body { margin: 0; padding: 20px; }
+ #scrollable-container {
+ width: 400px;
+ height: 300px;
+ overflow-y: scroll;
+ border: 2px solid #333;
+ background: #f0f0f0;
+ }
+ .inner-content {
+ height: 1500px;
+ background: linear-gradient(to bottom, #f0f0f0, #333);
+ padding: 20px;
+ }
+ .marker {
+ padding: 20px;
+ background: #007bff;
+ color: white;
+ margin: 200px 0;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>Page with Scrollable Div</h1>
+ <div id="scrollable-container">
+ <div class="inner-content">
+ <p>Start of scrollable content</p>
+ <div class="marker">Marker 1</div>
+ <div class="marker">Marker 2</div>
+ <div class="marker">Marker 3</div>
+ <p>End of scrollable content</p>
+ </div>
+ </div>
+ <p>Content outside scrollable div</p>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+
+ # Navigate to the page with scrollable div
+ await browser_session._cdp_navigate(f'{base_url}/scrollable-div')
await asyncio.sleep(0.5)
- # Create a mock node with frame_id to test cross-origin handling
- class MockNode:
- def __init__(self):
- self.frame_id = None # No frame_id for simple element
- self.backend_node_id = 1
+ # Get browser state to find the scrollable element
+ state = await browser_session.get_browser_state_summary()
+
+ # Find the scrollable container element in the selector map
+ scrollable_node = None
+ for index, node in state.selector_map.items():
+ # Look for the div with id="scrollable-container"
+ if (hasattr(node, 'attributes') and
+ node.attributes.get('id') == 'scrollable-container'):
+ scrollable_node = node
+ break
+
+ assert scrollable_node is not None, "Could not find scrollable-container element"
- mock_node = MockNode()
+ # Test scrolling the specific element down
+ event = browser_session.event_bus.dispatch(
+ ScrollEvent(direction='down', amount=300, node=scrollable_node)
+ )
+ result = await asyncio.wait_for(event, timeout=3.0)
+ event_result = await result.event_result()
+ assert event_result is not None
+ assert event_result.get('success') is True
- # Test element scroll - should use main session when cross_origin_iframes=False
- event = browser_session.event_bus.dispatch(ScrollEvent(direction='down', amount=300, node=mock_node))
+ # Test scrolling the specific element up
+ event = browser_session.event_bus.dispatch(
+ ScrollEvent(direction='up', amount=150, node=scrollable_node)
+ )
result = await asyncio.wait_for(event, timeout=3.0)
event_result = await result.event_result()
- assert event_result is not None
\ No newline at end of file
+ assert event_result is not None
+ assert event_result.get('success') is True
\ No newline at end of file
diff --git a/tests/ci/test_controller.py b/tests/ci/test_controller.py
index c918f9838..84a15b763 100644
--- a/tests/ci/test_controller.py
+++ b/tests/ci/test_controller.py
@@ -16,7 +16,6 @@ from browser_use.controller.views import (
GoToUrlAction,
InputTextAction,
NoParamsAction,
- ScrollAction,
SearchGoogleAction,
SendKeysAction,
SwitchTabAction,
diff --git a/tests/ci/test_navigation_watchdog.py b/tests/ci/test_security_watchdog.py
similarity index 76%
rename from tests/ci/test_navigation_watchdog.py
rename to tests/ci/test_security_watchdog.py
index cad82a8a2..49c8ad364 100644
--- a/tests/ci/test_navigation_watchdog.py
+++ b/tests/ci/test_security_watchdog.py
@@ -1,4 +1,4 @@
-"""Test NavigationWatchdog functionality."""
+"""Test navigation and security functionality."""
from typing import cast
@@ -18,8 +18,8 @@ from browser_use.browser.session import BrowserSession
@pytest.mark.asyncio
-async def test_navigation_watchdog_tab_created_events():
- """Test that the NavigationWatchdog properly emits TabCreatedEvent when pages are added."""
+async def test_navigation_tab_created_events():
+ """Test that BrowserSession properly emits TabCreatedEvent when pages are added."""
profile = BrowserProfile(headless=True)
session = BrowserSession(browser_profile=profile)
@@ -32,9 +32,9 @@ async def test_navigation_watchdog_tab_created_events():
session.event_bus.dispatch(BrowserStartEvent())
await session.event_bus.expect(BrowserConnectedEvent, timeout=5.0)
- # Verify navigation watchdog was created
- assert hasattr(session, '_navigation_watchdog'), 'NavigationWatchdog should be created'
- assert session._navigation_watchdog is not None, 'NavigationWatchdog should not be None'
+ # Verify security watchdog was created (replaces navigation watchdog)
+ assert hasattr(session, '_security_watchdog'), 'SecurityWatchdog should be created'
+ assert session._security_watchdog is not None, 'SecurityWatchdog should not be None'
# Create first tab - should emit TabCreatedEvent
session.event_bus.dispatch(NavigateToUrlEvent(url='data:text/html,<h1>Tab 1</h1>', new_tab=True))
@@ -67,8 +67,8 @@ async def test_navigation_watchdog_tab_created_events():
@pytest.mark.asyncio
-async def test_navigation_watchdog_security_enforcement():
- """Test that the NavigationWatchdog enforces allowed_domains security."""
+async def test_security_watchdog_enforcement():
+ """Test that the SecurityWatchdog enforces allowed_domains security."""
# Create a profile with restricted domains
profile = BrowserProfile(
headless=True,
@@ -85,32 +85,32 @@ async def test_navigation_watchdog_security_enforcement():
session.event_bus.dispatch(BrowserStartEvent())
await session.event_bus.expect(BrowserConnectedEvent, timeout=5.0)
- # Verify navigation watchdog was created and has security check
- assert hasattr(session, '_navigation_watchdog'), 'NavigationWatchdog should be created'
- assert session._navigation_watchdog is not None, 'NavigationWatchdog should not be None'
+ # Verify security watchdog was created and has security check
+ assert hasattr(session, '_security_watchdog'), 'SecurityWatchdog should be created'
+ assert session._security_watchdog is not None, 'SecurityWatchdog should not be None'
# Test that allowed domains work
allowed_url = 'https://httpbin.org/get'
- allowed = session._navigation_watchdog._is_url_allowed(allowed_url)
+ allowed = session._security_watchdog._is_url_allowed(allowed_url)
assert allowed, f'Should allow {allowed_url}'
# Test that disallowed domains are blocked
disallowed_url = 'https://malicious-site.com/bad'
- disallowed = session._navigation_watchdog._is_url_allowed(disallowed_url)
+ disallowed = session._security_watchdog._is_url_allowed(disallowed_url)
assert not disallowed, f'Should block {disallowed_url}'
# Test internal URLs are always allowed
internal_urls = ['about:blank', 'chrome://newtab/', 'chrome://new-tab-page/']
for url in internal_urls:
- assert session._navigation_watchdog._is_url_allowed(url), f'Should allow internal URL: {url}'
+ assert session._security_watchdog._is_url_allowed(url), f'Should allow internal URL: {url}'
# Test glob patterns work
profile_with_glob = BrowserProfile(headless=True, allowed_domains=['*.github.com'])
- session._navigation_watchdog.browser_session.browser_profile = profile_with_glob
+ session._security_watchdog.browser_session.browser_profile = profile_with_glob
- assert session._navigation_watchdog._is_url_allowed('https://api.github.com/repos'), 'Should allow subdomain'
- assert session._navigation_watchdog._is_url_allowed('https://github.com/user'), 'Should allow main domain'
- assert not session._navigation_watchdog._is_url_allowed('https://evil.com/github.com'), 'Should block non-matching domain'
+ assert session._security_watchdog._is_url_allowed('https://api.github.com/repos'), 'Should allow subdomain'
+ assert session._security_watchdog._is_url_allowed('https://github.com/user'), 'Should allow main domain'
+ assert not session._security_watchdog._is_url_allowed('https://evil.com/github.com'), 'Should block non-matching domain'
finally:
# Stop browser