Skip to content

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

better exception handling from browser events -> LLM

mertunsallb177f3166ea7
diff --git a/browser_use/browser/views.py b/browser_use/browser/views.py
index 355c542ff..dcb5606db 100644
--- a/browser_use/browser/views.py
+++ b/browser_use/browser/views.py
@@ -126,17 +126,42 @@ class BrowserStateHistory:
class BrowserError(Exception):
- """Base class for all browser errors"""
+ """Browser error with structured memory for LLM context management.
+
+ This exception class provides separate memory contexts for browser actions:
+ - short_term_memory: Immediate context shown once to the LLM for the next action
+ - long_term_memory: Persistent error information stored across steps
+ """
message: str
+ short_term_memory: str | None = None
+ long_term_memory: str | None = None
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):
+ def __init__(
+ self,
+ message: str,
+ short_term_memory: str | None = None,
+ long_term_memory: str | None = None,
+ details: dict[str, Any] | None = None,
+ event: BaseEvent[Any] | None = None,
+ ):
+ """Initialize a BrowserError with structured memory contexts.
+
+ Args:
+ message: Technical error message for logging and debugging
+ short_term_memory: Context shown once to LLM (e.g., available actions, options)
+ long_term_memory: Persistent error info stored in agent memory
+ details: Additional metadata for debugging
+ event: The browser event that triggered this error
+ """
self.message = message
- super().__init__(message)
+ self.short_term_memory = short_term_memory
+ self.long_term_memory = long_term_memory or message # Fallback to message if not provided
self.details = details
self.while_handling_event = event
+ super().__init__(message)
def __str__(self) -> str:
if self.details:
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index 476b629a4..9cfc8c19e 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -58,7 +58,8 @@ class DefaultActionWatchdog(BaseWatchdog):
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)
raise BrowserError(
- 'Click triggered a file input element which could not be handled, use the dedicated file upload function instead'
+ message=msg,
+ long_term_memory=msg,
)
# Perform the actual click using internal implementation
@@ -230,13 +231,18 @@ class DefaultActionWatchdog(BaseWatchdog):
self.logger.warning(
f'Cannot click on <select> elements. Use get_dropdown_options(index={element_node.element_index}) action instead.'
)
- raise Exception(
- f'<llm_error_msg>Cannot click on <select> elements. Use get_dropdown_options(index={element_node.element_index}) action instead.</llm_error_msg>'
+ msg = f'Cannot click on <select> elements. Use get_dropdown_options(index={element_node.element_index}) action instead.'
+ self.logger.warning(msg)
+ raise BrowserError(
+ message=msg,
+ long_term_memory=msg,
)
if tag_name == 'input' and element_type == 'file':
- raise Exception(
- f'<llm_error_msg>Cannot click on file input element (index={element_node.element_index}). File uploads must be handled using upload_file_to_element action</llm_error_msg>'
+ msg = f'Cannot click on file input element (index={element_node.element_index}). File uploads must be handled using upload_file_to_element action.'
+ raise BrowserError(
+ message=msg,
+ long_term_memory=msg,
)
# Get CDP client
@@ -531,14 +537,17 @@ class DefaultActionWatchdog(BaseWatchdog):
except URLNotAllowedError as e:
raise e
+ except BrowserError as e:
+ raise e
except Exception as e:
# Extract key element info for error message
element_info = f'<{element_node.tag_name or "unknown"}'
if element_node.element_index:
element_info += f' index={element_node.element_index}'
element_info += '>'
- raise Exception(
- f'<llm_error_msg>Failed to click element {element_info}. The element may not be interactable or visible. {type(e).__name__}: {e}</llm_error_msg>'
+ raise BrowserError(
+ message=f'Failed to click element: {e}',
+ long_term_memory=f'Failed to click element {element_info}. The element may not be interactable or visible.',
)
async def _type_to_page(self, text: str):
@@ -1269,9 +1278,8 @@ class DefaultActionWatchdog(BaseWatchdog):
# Check if it's a file input
if not self.browser_session.is_file_input(element_node):
- raise Exception(
- f'<llm_error_msg>Element {index_for_logging} is not a file input. Use click_element_by_index for non-file input elements.</llm_error_msg>'
- )
+ msg = f'Upload failed - element {index_for_logging} is not a file input.'
+ raise BrowserError(message=msg, long_term_memory=msg)
# Get CDP client and session
cdp_client = self.browser_session.cdp_client
@@ -1408,191 +1416,203 @@ class DefaultActionWatchdog(BaseWatchdog):
except Exception as e:
raise ValueError(f'Failed to resolve node to object: {e}') from e
- try:
- # Use JavaScript to extract dropdown options
- options_script = """
- function() {
- const startElement = this;
+ # Use JavaScript to extract dropdown options
+ options_script = """
+ function() {
+ const startElement = this;
+
+ // Function to check if an element is a dropdown and extract options
+ function checkDropdownElement(element) {
+ // Check if it's a native select element
+ if (element.tagName.toLowerCase() === 'select') {
+ return {
+ type: 'select',
+ options: Array.from(element.options).map((opt, idx) => ({
+ text: opt.text.trim(),
+ value: opt.value,
+ index: idx,
+ selected: opt.selected
+ })),
+ id: element.id || '',
+ name: element.name || '',
+ source: 'target'
+ };
+ }
- // Function to check if an element is a dropdown and extract options
- function checkDropdownElement(element) {
- // Check if it's a native select element
- if (element.tagName.toLowerCase() === 'select') {
- return {
- type: 'select',
- options: Array.from(element.options).map((opt, idx) => ({
- text: opt.text.trim(),
- value: opt.value,
+ // Check if it's an ARIA dropdown/menu
+ const role = element.getAttribute('role');
+ if (role === 'menu' || role === 'listbox' || role === 'combobox') {
+ // Find all menu items/options
+ const menuItems = element.querySelectorAll('[role="menuitem"], [role="option"]');
+ const options = [];
+
+ menuItems.forEach((item, idx) => {
+ const text = item.textContent ? item.textContent.trim() : '';
+ if (text) {
+ options.push({
+ text: text,
+ value: item.getAttribute('data-value') || text,
index: idx,
- selected: opt.selected
- })),
- id: element.id || '',
- name: element.name || '',
- source: 'target'
- };
- }
+ selected: item.getAttribute('aria-selected') === 'true' || item.classList.contains('selected')
+ });
+ }
+ });
- // Check if it's an ARIA dropdown/menu
- const role = element.getAttribute('role');
- if (role === 'menu' || role === 'listbox' || role === 'combobox') {
- // Find all menu items/options
- const menuItems = element.querySelectorAll('[role="menuitem"], [role="option"]');
- const options = [];
-
- menuItems.forEach((item, idx) => {
- const text = item.textContent ? item.textContent.trim() : '';
- if (text) {
- options.push({
- text: text,
- value: item.getAttribute('data-value') || text,
- index: idx,
- selected: item.getAttribute('aria-selected') === 'true' || item.classList.contains('selected')
- });
- }
- });
-
+ return {
+ type: 'aria',
+ options: options,
+ id: element.id || '',
+ name: element.getAttribute('aria-label') || '',
+ source: 'target'
+ };
+ }
+
+ // Check if it's a Semantic UI dropdown or similar
+ if (element.classList.contains('dropdown') || element.classList.contains('ui')) {
+ const menuItems = element.querySelectorAll('.item, .option, [data-value]');
+ const options = [];
+
+ menuItems.forEach((item, idx) => {
+ const text = item.textContent ? item.textContent.trim() : '';
+ if (text) {
+ options.push({
+ text: text,
+ value: item.getAttribute('data-value') || text,
+ index: idx,
+ selected: item.classList.contains('selected') || item.classList.contains('active')
+ });
+ }
+ });
+
+ if (options.length > 0) {
return {
- type: 'aria',
+ type: 'custom',
options: options,
id: element.id || '',
name: element.getAttribute('aria-label') || '',
source: 'target'
};
}
-
- // Check if it's a Semantic UI dropdown or similar
- if (element.classList.contains('dropdown') || element.classList.contains('ui')) {
- const menuItems = element.querySelectorAll('.item, .option, [data-value]');
- const options = [];
-
- menuItems.forEach((item, idx) => {
- const text = item.textContent ? item.textContent.trim() : '';
- if (text) {
- options.push({
- text: text,
- value: item.getAttribute('data-value') || text,
- index: idx,
- selected: item.classList.contains('selected') || item.classList.contains('active')
- });
- }
- });
-
- if (options.length > 0) {
- return {
- type: 'custom',
- options: options,
- id: element.id || '',
- name: element.getAttribute('aria-label') || '',
- source: 'target'
- };
- }
- }
-
- return null;
}
- // Function to recursively search children up to specified depth
- function searchChildrenForDropdowns(element, maxDepth, currentDepth = 0) {
- if (currentDepth >= maxDepth) return null;
-
- // Check all direct children
- for (let child of element.children) {
- // Check if this child is a dropdown
- const result = checkDropdownElement(child);
- if (result) {
- result.source = `child-depth-${currentDepth + 1}`;
- return result;
- }
-
- // Recursively check this child's children
- const childResult = searchChildrenForDropdowns(child, maxDepth, currentDepth + 1);
- if (childResult) {
- return childResult;
- }
+ return null;
+ }
+
+ // Function to recursively search children up to specified depth
+ function searchChildrenForDropdowns(element, maxDepth, currentDepth = 0) {
+ if (currentDepth >= maxDepth) return null;
+
+ // Check all direct children
+ for (let child of element.children) {
+ // Check if this child is a dropdown
+ const result = checkDropdownElement(child);
+ if (result) {
+ result.source = `child-depth-${currentDepth + 1}`;
+ return result;
}
- return null;
- }
-
- // First check the target element itself
- let dropdownResult = checkDropdownElement(startElement);
- if (dropdownResult) {
- return dropdownResult;
- }
-
- // If target element is not a dropdown, search children up to depth 4
- dropdownResult = searchChildrenForDropdowns(startElement, 4);
- if (dropdownResult) {
- return dropdownResult;
+ // Recursively check this child's children
+ const childResult = searchChildrenForDropdowns(child, maxDepth, currentDepth + 1);
+ if (childResult) {
+ return childResult;
+ }
}
- return {
- error: `Element and its children (depth 4) are not recognizable dropdown types (tag: ${startElement.tagName}, role: ${startElement.getAttribute('role')}, classes: ${startElement.className})`
- };
+ return null;
}
- """
-
- result = await cdp_session.cdp_client.send.Runtime.callFunctionOn(
- params={
- 'functionDeclaration': options_script,
- 'objectId': object_id,
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
-
- dropdown_data = result.get('result', {}).get('value', {})
+
+ // First check the target element itself
+ let dropdownResult = checkDropdownElement(startElement);
+ if (dropdownResult) {
+ return dropdownResult;
+ }
+
+ // If target element is not a dropdown, search children up to depth 4
+ dropdownResult = searchChildrenForDropdowns(startElement, 4);
+ if (dropdownResult) {
+ return dropdownResult;
+ }
+
+ return {
+ error: `Element and its children (depth 4) are not recognizable dropdown types (tag: ${startElement.tagName}, role: ${startElement.getAttribute('role')}, classes: ${startElement.className})`
+ };
+ }
+ """
- if dropdown_data.get('error'):
- raise ValueError(dropdown_data['error'])
+ result = await cdp_session.cdp_client.send.Runtime.callFunctionOn(
+ params={
+ 'functionDeclaration': options_script,
+ 'objectId': object_id,
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
- if not dropdown_data.get('options'):
- raise ValueError('No options found in dropdown')
+ dropdown_data = result.get('result', {}).get('value', {})
- # Format options for display
- formatted_options = []
- for opt in dropdown_data['options']:
- # Use JSON encoding to ensure exact string matching
- encoded_text = json.dumps(opt['text'])
- status = ' (selected)' if opt.get('selected') else ''
- formatted_options.append(f'{opt["index"]}: text={encoded_text}, value={json.dumps(opt["value"])}{status}')
+ if dropdown_data.get('error'):
+ raise BrowserError(message=dropdown_data['error'], long_term_memory=dropdown_data['error'])
- dropdown_type = dropdown_data.get('type', 'select')
- element_info = f'Index: {index_for_logging}, Type: {dropdown_type}, ID: {dropdown_data.get("id", "none")}, Name: {dropdown_data.get("name", "none")}'
- source_info = dropdown_data.get('source', 'unknown')
+ if not dropdown_data.get('options'):
+ msg = f'No options found in dropdown at index {index_for_logging}'
+ raise BrowserError(message=msg, long_term_memory=msg)
- if source_info == 'target':
- msg = f'Found {dropdown_type} dropdown ({element_info}):\n' + '\n'.join(formatted_options)
- else:
- msg = f'Found {dropdown_type} dropdown in {source_info} ({element_info}):\n' + '\n'.join(formatted_options)
- msg += f'\n\nUse the exact text or value string (without quotes) in select_dropdown_option(index={index_for_logging}, text=...)'
+ # Format options for display
+ formatted_options = []
+ for opt in dropdown_data['options']:
+ # Use JSON encoding to ensure exact string matching
+ encoded_text = json.dumps(opt['text'])
+ status = ' (selected)' if opt.get('selected') else ''
+ formatted_options.append(f'{opt["index"]}: text={encoded_text}, value={json.dumps(opt["value"])}{status}')
- if source_info == 'target':
- self.logger.info(f'πŸ“‹ Found {len(dropdown_data["options"])} dropdown options for index {index_for_logging}')
- else:
- self.logger.info(
- f'πŸ“‹ Found {len(dropdown_data["options"])} dropdown options for index {index_for_logging} in {source_info}'
- )
+ dropdown_type = dropdown_data.get('type', 'select')
+ element_info = f'Index: {index_for_logging}, Type: {dropdown_type}, ID: {dropdown_data.get("id", "none")}, Name: {dropdown_data.get("name", "none")}'
+ source_info = dropdown_data.get('source', 'unknown')
- # Return the dropdown data as a dict
- return {
- 'type': dropdown_type,
- 'options': json.dumps(dropdown_data['options']), # Convert list to JSON string for dict[str, str] type
- 'element_info': element_info,
- 'source': source_info,
- 'formatted_options': '\n'.join(formatted_options),
- 'message': msg,
- }
+ if source_info == 'target':
+ msg = f'Found {dropdown_type} dropdown ({element_info}):\n' + '\n'.join(formatted_options)
+ else:
+ msg = f'Found {dropdown_type} dropdown in {source_info} ({element_info}):\n' + '\n'.join(formatted_options)
+ msg += f'\n\nUse the exact text or value string (without quotes) in select_dropdown_option(index={index_for_logging}, text=...)'
- except Exception as e:
- error_msg = f'Failed to get dropdown options: {str(e)}'
- self.logger.error(error_msg)
- raise ValueError(error_msg) from e
+ if source_info == 'target':
+ self.logger.info(f'πŸ“‹ Found {len(dropdown_data["options"])} dropdown options for index {index_for_logging}')
+ else:
+ self.logger.info(
+ f'πŸ“‹ Found {len(dropdown_data["options"])} dropdown options for index {index_for_logging} in {source_info}'
+ )
+ # Create structured memory for the response
+ short_term_memory = msg
+ long_term_memory = f'Got dropdown options for index {index_for_logging}'
+
+ # Return the dropdown data as a dict with structured memory
+ return {
+ 'type': dropdown_type,
+ 'options': json.dumps(dropdown_data['options']), # Convert list to JSON string for dict[str, str] type
+ 'element_info': element_info,
+ 'source': source_info,
+ 'formatted_options': '\n'.join(formatted_options),
+ 'message': msg,
+ 'short_term_memory': short_term_memory,
+ 'long_term_memory': long_term_memory,
+ 'element_index': str(index_for_logging),
+ }
+
+ except BrowserError:
+ # Re-raise BrowserError as-is to preserve structured memory
+ raise
+ except TimeoutError:
+ msg = f'Failed to get dropdown options for index {index_for_logging} due to timeout.'
+ self.logger.error(msg)
+ raise BrowserError(message=msg, long_term_memory=msg)
except Exception as e:
- error_msg = f'Failed to get dropdown options for element {index_for_logging}: {str(e)}'
+ msg = f'Failed to get dropdown options for element with index {index_for_logging}'
+ error_msg = f'{msg}: {str(e)}'
self.logger.error(error_msg)
- raise ValueError(error_msg) from e
+ raise BrowserError(
+ message=error_msg, long_term_memory=f'Failed to get dropdown options for index {index_for_logging}.'
+ )
async def on_SelectDropdownOptionEvent(self, event: SelectDropdownOptionEvent) -> dict[str, str]:
"""Handle select dropdown option request with CDP."""
@@ -1651,7 +1671,7 @@ class DefaultActionWatchdog(BaseWatchdog):
}
}
- // Show all available options for debugging
+ // Return available options as separate field
const availableOptions = options.map(opt => ({
text: opt.text.trim(),
value: opt.value
@@ -1659,7 +1679,8 @@ class DefaultActionWatchdog(BaseWatchdog):
return {
success: false,
- error: `Option with text or value '${targetText}' not found in select element. Available options: ${JSON.stringify(availableOptions, null, 2)}`
+ error: `Option with text or value '${targetText}' not found in select element`,
+ availableOptions: availableOptions
};
}
@@ -1699,7 +1720,7 @@ class DefaultActionWatchdog(BaseWatchdog):
}
}
- // Show all available options for debugging
+ // Return available options as separate field
const availableOptions = Array.from(menuItems).map(item => ({
text: item.textContent ? item.textContent.trim() : '',
value: item.getAttribute('data-value') || ''
@@ -1707,7 +1728,8 @@ class DefaultActionWatchdog(BaseWatchdog):
return {
success: false,
- error: `Menu item with text or value '${targetText}' not found. Available options: ${JSON.stringify(availableOptions, null, 2)}`
+ error: `Menu item with text or value '${targetText}' not found`,
+ availableOptions: availableOptions
};
}
@@ -1754,7 +1776,7 @@ class DefaultActionWatchdog(BaseWatchdog):
}
}
- // Show all available options for debugging
+ // Return available options as separate field
const availableOptions = Array.from(menuItems).map(item => ({
text: item.textContent ? item.textContent.trim() : '',
value: item.getAttribute('data-value') || ''
@@ -1762,7 +1784,8 @@ class DefaultActionWatchdog(BaseWatchdog):
return {
success: false,
- error: `Custom dropdown item with text or value '${targetText}' not found. Available options: ${JSON.stringify(availableOptions, null, 2)}`
+ error: `Custom dropdown item with text or value '${targetText}' not found`,
+ availableOptions: availableOptions
};
}
@@ -1837,8 +1860,46 @@ class DefaultActionWatchdog(BaseWatchdog):
}
else:
error_msg = selection_result.get('error', f'Failed to select option: {target_text}')
+ available_options = selection_result.get('availableOptions', [])
self.logger.error(f'❌ {error_msg}')
- raise ValueError(error_msg)
+ self.logger.debug(f'Available options from JavaScript: {available_options}')
+
+ # If we have available options, return structured error data
+ if available_options:
+ # Format options for short_term_memory (simple bulleted list)
+ short_term_options = []
+ for opt in available_options:
+ if isinstance(opt, dict):
+ text = opt.get('text', '').strip()
+ value = opt.get('value', '').strip()
+ if text:
+ short_term_options.append(f'- {text}')
+ elif value:
+ short_term_options.append(f'- {value}')
+ elif isinstance(opt, str):
+ short_term_options.append(f'- {opt}')
+
+ if short_term_options:
+ short_term_memory = f'Available dropdown options at index {index_for_logging} are:\n' + '\n'.join(
+ short_term_options
+ )
+ long_term_memory = f"Couldn't select the dropdown option at index {index_for_logging} as '{target_text}' is not one of the available options."
+
+ # Return error result with structured memory instead of raising exception
+ return {
+ 'success': 'false',
+ 'error': error_msg,
+ 'short_term_memory': short_term_memory,
+ 'long_term_memory': long_term_memory,
+ 'element_index': str(index_for_logging),
+ }
+
+ # Fallback to regular error result if no available options
+ return {
+ 'success': 'false',
+ 'error': error_msg,
+ 'element_index': str(index_for_logging),
+ }
except Exception as e:
error_msg = f'Failed to select dropdown option: {str(e)}'
diff --git a/browser_use/tools/registry/service.py b/browser_use/tools/registry/service.py
index 354e34d3c..000bbc467 100644
--- a/browser_use/tools/registry/service.py
+++ b/browser_use/tools/registry/service.py
@@ -379,6 +379,8 @@ class Registry(Generic[Context]):
raise RuntimeError(str(e)) from e
else:
raise RuntimeError(f'Error executing action {action_name}: {str(e)}') from e
+ except TimeoutError as e:
+ raise RuntimeError(f'Error executing action {action_name} due to timeout.')
except Exception as e:
raise RuntimeError(f'Error executing action {action_name}: {str(e)}') from e
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 20948f908..51b485a8b 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -65,26 +65,19 @@ Context = TypeVar('Context')
T = TypeVar('T', bound=BaseModel)
-def extract_llm_error_message(error: Exception) -> str:
- """
- Extract the clean error message from an exception that may contain <llm_error_msg> tags.
-
- If the tags are found, returns the content between them.
- Otherwise, returns the original error string.
- """
- import re
-
- error_str = str(error)
-
- # Look for content between <llm_error_msg> tags
- pattern = r'<llm_error_msg>(.*?)</llm_error_msg>'
- match = re.search(pattern, error_str, re.DOTALL)
-
- if match:
- return match.group(1).strip()
-
- # Fallback: return the original error string
- return error_str
+def handle_browser_error(e: BrowserError) -> ActionResult:
+ if e.long_term_memory is not None:
+ if e.short_term_memory is not None:
+ return ActionResult(
+ extracted_content=e.short_term_memory, error=e.long_term_memory, include_extracted_content_only_once=True
+ )
+ else:
+ return ActionResult(error=e.long_term_memory)
+ # Fallback to original error handling if long_term_memory is None
+ logger.warning(
+ '⚠️ A BrowserError was raised without long_term_memory - always set long_term_memory when raising BrowserError to propagate right messages to LLM.'
+ )
+ raise e
class Tools(Generic[Context]):
@@ -180,8 +173,7 @@ class Tools(Generic[Context]):
return ActionResult(extracted_content=memory, include_in_memory=True, long_term_memory=memory)
except Exception as e:
logger.error(f'Failed to search Google: {e}')
- clean_msg = extract_llm_error_message(e)
- return ActionResult(error=f'Failed to search Google for "{params.query}": {clean_msg}')
+ return ActionResult(error=f'Failed to search Google for "{params.query}": {str(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
@@ -206,7 +198,6 @@ class Tools(Generic[Context]):
error_msg = str(e)
# Always log the actual error first for debugging
browser_session.logger.error(f'❌ Navigation failed: {error_msg}')
- clean_msg = extract_llm_error_message(e)
# Check if it's specifically a RuntimeError about CDP client
if isinstance(e, RuntimeError) and 'CDP client not initialized' in error_msg:
@@ -223,12 +214,12 @@ class Tools(Generic[Context]):
'net::',
]
):
- site_unavailable_msg = f'Site unavailable: {params.url} - {error_msg}'
- browser_session.logger.warning(f'⚠️ {site_unavailable_msg}')
+ site_unavailable_msg = f'Navigation failed - site unavailable: {params.url}'
+ browser_session.logger.warning(f'⚠️ {site_unavailable_msg} - {error_msg}')
return ActionResult(error=site_unavailable_msg)
else:
# Return error in ActionResult instead of re-raising
- return ActionResult(error=f'Navigation failed: {clean_msg}')
+ return ActionResult(error=f'Navigation failed: {str(e)}')
@self.registry.action('Go back', param_model=NoParamsAction)
async def go_back(_: NoParamsAction, browser_session: BrowserSession):
@@ -241,8 +232,7 @@ class Tools(Generic[Context]):
return ActionResult(extracted_content=memory)
except Exception as e:
logger.error(f'Failed to dispatch GoBackEvent: {type(e).__name__}: {e}')
- clean_msg = extract_llm_error_message(e)
- error_msg = f'Failed to go back: {clean_msg}'
+ error_msg = f'Failed to go back: {str(e)}'
return ActionResult(error=error_msg)
@self.registry.action(
@@ -295,21 +285,22 @@ class Tools(Generic[Context]):
long_term_memory=memory,
metadata=click_metadata if isinstance(click_metadata, dict) else None,
)
+ except BrowserError as e:
+ return handle_browser_error(e)
except Exception as e:
- logger.error(f'Failed to execute ClickElementEvent: {type(e).__name__}: {e}')
- clean_msg = extract_llm_error_message(e)
- error_msg = f'Failed to click element {params.index}: {clean_msg}'
+ error_msg = f'Failed to click element {params.index}: {str(e)}'
# If it's a select dropdown error, automatically get the dropdown options
- if 'dropdown' in str(e) and node:
- try:
- return await get_dropdown_options(
- params=GetDropdownOptionsAction(index=params.index), browser_session=browser_session
- )
- except Exception as dropdown_error:
- logger.error(
- f'Failed to get dropdown options as shortcut during click_element_by_index on dropdown: {type(dropdown_error).__name__}: {dropdown_error}'
- )
+ # comment this out for now as it should be handled properly
+ # if 'dropdown' in str(e) and node:
+ # try:
+ # return await get_dropdown_options(
+ # params=GetDropdownOptionsAction(index=params.index), browser_session=browser_session
+ # )
+ # except Exception as dropdown_error:
+ # logger.error(
+ # f'Failed to get dropdown options as shortcut during click_element_by_index on dropdown: {type(dropdown_error).__name__}: {dropdown_error}'
+ # )
return ActionResult(error=error_msg)
@@ -340,6 +331,8 @@ class Tools(Generic[Context]):
long_term_memory=f"Input '{params.text}' into element {params.index}.",
metadata=input_metadata if isinstance(input_metadata, dict) else None,
)
+ except BrowserError as e:
+ return handle_browser_error(e)
except Exception as e:
# Log the full error for debugging
logger.error(f'Failed to dispatch TypeTextEvent: {type(e).__name__}: {e}')
@@ -370,27 +363,28 @@ class Tools(Generic[Context]):
if not browser_session.is_local:
pass
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.'
- )
+ msg = f'File path {params.path} is not available. Upload files must be in available_file_paths, downloaded_files, or a file managed by file_system.'
+ logger.error(f'❌ {msg}')
+ return ActionResult(error=msg)
else:
# If browser is remote, allow passing a remote-accessible absolute path
if not browser_session.is_local:
pass
else:
- raise BrowserError(
- f'File path {params.path} is not available. Must be in available_file_paths or downloaded_files.'
- )
+ msg = f'File path {params.path} is not available. Upload files must be in available_file_paths, downloaded_files, or a file managed by file_system.'
+ raise BrowserError(message=msg, long_term_memory=msg)
# For local browsers, ensure the file exists on the local filesystem
if browser_session.is_local:
if not os.path.exists(params.path):
- raise BrowserError(f'File {params.path} does not exist')
+ msg = f'File {params.path} does not exist'
+ return ActionResult(error=msg)
# Get the selector map to find the node
selector_map = await browser_session.get_selector_map()
if params.index not in selector_map:
- raise BrowserError(f'Element with index {params.index} not found in selector map')
+ msg = f'Element with index {params.index} does not exist.'
+ return ActionResult(error=msg)
node = selector_map[params.index]
@@ -515,8 +509,7 @@ class Tools(Generic[Context]):
return ActionResult(extracted_content=memory, include_in_memory=True, long_term_memory=memory)
except Exception as e:
logger.error(f'Failed to switch tab: {type(e).__name__}: {e}')
- clean_msg = extract_llm_error_message(e)
- return ActionResult(error=f'Failed to switch to tab {params.tab_id or params.url}: {clean_msg}')
+ return ActionResult(error=f'Failed to switch to tab {params.tab_id or params.url}.')
@self.registry.action('Close an existing tab', param_model=CloseTabAction)
async def close_tab(params: CloseTabAction, browser_session: BrowserSession):
@@ -540,8 +533,7 @@ class Tools(Generic[Context]):
)
except Exception as e:
logger.error(f'Failed to close tab: {e}')
- clean_msg = extract_llm_error_message(e)
- return ActionResult(error=f'Failed to close tab {params.tab_id}: {clean_msg}')
+ return ActionResult(error=f'Failed to close tab {params.tab_id}.')
# Content Actions
@@ -700,8 +692,7 @@ Provide the extracted information in a clear, structured format."""
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}')
- clean_msg = extract_llm_error_message(e)
- error_msg = f'Failed to scroll: {clean_msg}'
+ error_msg = 'Failed to execute scroll action.'
return ActionResult(error=error_msg)
@self.registry.action(
@@ -720,8 +711,7 @@ Provide the extracted information in a clear, structured format."""
return ActionResult(extracted_content=memory, include_in_memory=True, long_term_memory=memory)
except Exception as e:
logger.error(f'Failed to dispatch SendKeysEvent: {type(e).__name__}: {e}')
- clean_msg = extract_llm_error_message(e)
- error_msg = f'Failed to send keys: {clean_msg}'
+ error_msg = f'Failed to send keys: {str(e)}'
return ActionResult(error=error_msg)
@self.registry.action(
@@ -762,7 +752,6 @@ Provide the extracted information in a clear, structured format."""
raise ValueError(f'Element index {params.index} not found in DOM')
# Dispatch GetDropdownOptionsEvent to the event handler
- import json
event = browser_session.event_bus.dispatch(GetDropdownOptionsEvent(node=node))
dropdown_data = await event.event_result(timeout=3.0, raise_if_none=True, raise_if_any=True)
@@ -770,14 +759,10 @@ Provide the extracted information in a clear, structured format."""
if not dropdown_data:
raise ValueError('Failed to get dropdown options - no data returned')
- # Extract the message from the returned data
- msg = dropdown_data.get('message', '')
- options_count = len(json.loads(dropdown_data.get('options', '[]'))) # Parse the string back to list to get count
-
+ # Use structured memory from the handler
return ActionResult(
- extracted_content=msg,
- include_in_memory=True,
- long_term_memory=f'Found {options_count} dropdown options for index {params.index}',
+ extracted_content=dropdown_data['short_term_memory'],
+ long_term_memory=dropdown_data['long_term_memory'],
include_extracted_content_only_once=True,
)
@@ -801,14 +786,28 @@ Provide the extracted information in a clear, structured format."""
if not selection_data:
raise ValueError('Failed to select dropdown option - no data returned')
- # Extract the message from the returned data
- msg = selection_data.get('message', f'Selected option: {params.text}')
-
- return ActionResult(
- extracted_content=msg,
- include_in_memory=True,
- long_term_memory=f"Selected dropdown option '{params.text}' at index {params.index}",
- )
+ # Check if the selection was successful
+ if selection_data.get('success') == 'true':
+ # Extract the message from the returned data
+ msg = selection_data.get('message', f'Selected option: {params.text}')
+ return ActionResult(
+ extracted_content=msg,
+ include_in_memory=True,
+ long_term_memory=f"Selected dropdown option '{params.text}' at index {params.index}",
+ )
+ else:
+ # Handle structured error response
+ # TODO: raise BrowserError instead of returning ActionResult
+ if 'short_term_memory' in selection_data and 'long_term_memory' in selection_data:
+ return ActionResult(
+ extracted_content=selection_data['short_term_memory'],
+ long_term_memory=selection_data['long_term_memory'],
+ include_extracted_content_only_once=True,
+ )
+ else:
+ # Fallback to regular error
+ error_msg = selection_data.get('error', f'Failed to select option: {params.text}')
+ return ActionResult(error=error_msg)
# File System Actions
@self.registry.action(
@@ -1001,12 +1000,14 @@ Provide the extracted information in a clear, structured format."""
sensitive_data=sensitive_data,
available_file_paths=available_file_paths,
)
+ except BrowserError as e:
+ logger.error(f'❌ Action {action_name} failed with BrowserError: {str(e)}')
+ result = handle_browser_error(e)
+ return result
except Exception as e:
# Log the original exception with traceback for observability
- logger.error(f"Action '{action_name}' failed")
- # Extract clean error message from llm_error_msg tags if present
- clean_msg = extract_llm_error_message(e)
- result = ActionResult(error=clean_msg)
+ logger.error(f"Action '{action_name}' failed with error: {str(e)}")
+ result = ActionResult(error=str(e))
if Laminar is not None:
Laminar.set_span_output(result)
diff --git a/pyproject.toml b/pyproject.toml
index 9e33f959c..22a067623 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -14,7 +14,7 @@ dependencies = [
"aiofiles>=24.1.0",
"aiohttp==3.12.15",
"anyio>=4.9.0",
- "bubus>=1.5.4",
+ "bubus>=1.5.6",
"google-api-core>=2.25.0",
"httpx>=0.28.1",
"markdownify==1.1.0",