Skip to content

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

move dropdown options to proper event handlers

Nick Sweeting5cc7d0f301c1
diff --git a/browser_use/browser/default_action_watchdog.py b/browser_use/browser/default_action_watchdog.py
index d9986ad67..66c941042 100644
--- a/browser_use/browser/default_action_watchdog.py
+++ b/browser_use/browser/default_action_watchdog.py
@@ -1,16 +1,19 @@
"""Default browser action handlers using CDP."""
import asyncio
+import json
import platform
from typing import Any
from browser_use.browser.events import (
ClickElementEvent,
+ GetDropdownOptionsEvent,
GoBackEvent,
GoForwardEvent,
RefreshEvent,
ScrollEvent,
ScrollToTextEvent,
+ SelectDropdownOptionEvent,
SendKeysEvent,
TypeTextEvent,
UploadFileEvent,
@@ -23,6 +26,8 @@ from browser_use.dom.service import EnhancedDOMTreeNode
# Import EnhancedDOMTreeNode and rebuild event models that have forward references to it
# This must be done after all imports are complete
ClickElementEvent.model_rebuild()
+GetDropdownOptionsEvent.model_rebuild()
+SelectDropdownOptionEvent.model_rebuild()
TypeTextEvent.model_rebuild()
ScrollEvent.model_rebuild()
UploadFileEvent.model_rebuild()
@@ -225,10 +230,17 @@ class DefaultActionWatchdog(BaseWatchdog):
element_type = element_node.attributes.get('type', '').lower() if element_node.attributes else ''
if tag_name == 'select':
- raise Exception('<llm_error_msg>Cannot click on <select> elements. Use select_dropdown_option action instead.</llm_error_msg>')
+ try:
+ dropdown_options = await self.event_bus.dispatch(GetDropdownOptionsEvent(node=element_node)).event_result(raise_if_none=True) or {}
+ except Exception as e:
+ self.logger.warning(f'Failed to get dropdown options: {type(e).__name__}: {e}')
+ dropdown_options = {}
+ available_options = ("Available options: [" + ", ".join(dropdown_options.keys()) + "]") if dropdown_options else "Call get_dropdown_options() to get available options list."
+ self.logger.warning(f'Cannot click on <select> elements. Use select_dropdown_option() action instead. {available_options}]')
+ raise Exception(f'Cannot click on <select> elements. Use select_dropdown_option() action instead. {available_options}')
if tag_name == 'input' and element_type == 'file':
- raise Exception('<llm_error_msg>Cannot click on file input elements. File uploads must be handled programmatically.</llm_error_msg>')
+ raise Exception('Cannot click on file input elements. File uploads must be handled using upload_file_to_element()')
# Get CDP client
cdp_session = await self.browser_session.get_or_create_cdp_session(target_id=element_node.target_id, focus=False)
@@ -1284,3 +1296,467 @@ class DefaultActionWatchdog(BaseWatchdog):
return None
else:
raise BrowserError(f'Text not found: "{event.text}"', details={'text': event.text})
+
+ async def on_GetDropdownOptionsEvent(self, event: GetDropdownOptionsEvent) -> dict[str, str]:
+ """Handle get dropdown options request with CDP."""
+ try:
+ # Use the provided node
+ element_node = event.node
+ index_for_logging = element_node.element_index or 'unknown'
+
+ # Get CDP session for this node
+ cdp_session = await self.browser_session.cdp_client_for_node(element_node)
+
+ # Convert node to object ID for CDP operations
+ try:
+ object_result = await cdp_session.cdp_client.send.DOM.resolveNode(
+ params={'backendNodeId': element_node.backend_node_id}, session_id=cdp_session.session_id
+ )
+ remote_object = object_result.get('object', {})
+ object_id = remote_object.get('objectId')
+ if not object_id:
+ raise ValueError('Could not get object ID from resolved node')
+ 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;
+
+ // 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'
+ };
+ }
+
+ // 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: '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;
+ }
+
+ // 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})`
+ };
+ }
+ """
+
+ 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', {})
+
+ if dropdown_data.get('error'):
+ raise ValueError(dropdown_data['error'])
+
+ if not dropdown_data.get('options'):
+ raise ValueError('No options found in dropdown')
+
+ # 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}')
+
+ dropdown_type = dropdown_data.get('type', 'unknown')
+ element_info = f'ID: {dropdown_data.get("id", "none")}, Name: {dropdown_data.get("name", "none")}'
+ source_info = dropdown_data.get('source', 'unknown')
+
+ 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 += '\n\nUse the exact text string (without quotes) in select_dropdown_option'
+
+ 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}'
+ )
+
+ # 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
+ }
+
+ 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
+
+ except Exception as e:
+ error_msg = f'Failed to get dropdown options for element {index_for_logging}: {str(e)}'
+ self.logger.error(error_msg)
+ raise ValueError(error_msg) from e
+
+ async def on_SelectDropdownOptionEvent(self, event: SelectDropdownOptionEvent) -> dict[str, str]:
+ """Handle select dropdown option request with CDP."""
+ try:
+ # Use the provided node
+ element_node = event.node
+ index_for_logging = element_node.element_index or 'unknown'
+ target_text = event.text
+
+ # Get CDP session for this node
+ cdp_session = await self.browser_session.cdp_client_for_node(element_node)
+
+ # Convert node to object ID for CDP operations
+ try:
+ object_result = await cdp_session.cdp_client.send.DOM.resolveNode(
+ params={'backendNodeId': element_node.backend_node_id}, session_id=cdp_session.session_id
+ )
+ remote_object = object_result.get('object', {})
+ object_id = remote_object.get('objectId')
+ if not object_id:
+ raise ValueError('Could not get object ID from resolved node')
+ except Exception as e:
+ raise ValueError(f'Failed to resolve node to object: {e}') from e
+
+ try:
+ # Use JavaScript to select the option
+ selection_script = """
+ function(targetText) {
+ const startElement = this;
+
+ // Function to attempt selection on a dropdown element
+ function attemptSelection(element) {
+ // Handle native select elements
+ if (element.tagName.toLowerCase() === 'select') {
+ const options = Array.from(element.options);
+ const targetTextLower = targetText.toLowerCase();
+
+ for (const option of options) {
+ const optionTextLower = option.text.trim().toLowerCase();
+ const optionValueLower = option.value.toLowerCase();
+
+ // Match against both text and value (case-insensitive)
+ if (optionTextLower === targetTextLower || optionValueLower === targetTextLower) {
+ element.value = option.value;
+ option.selected = true;
+
+ // Trigger change events
+ const changeEvent = new Event('change', { bubbles: true });
+ element.dispatchEvent(changeEvent);
+
+ return {
+ success: true,
+ message: `Selected option: ${option.text.trim()} (value: ${option.value})`,
+ value: option.value
+ };
+ }
+ }
+
+ // Show all available options for debugging
+ const availableOptions = options.map(opt => ({
+ text: opt.text.trim(),
+ value: opt.value
+ }));
+
+ return {
+ success: false,
+ error: `Option with text or value '${targetText}' not found in select element. Available options: ${JSON.stringify(availableOptions, null, 2)}`
+ };
+ }
+
+ // Handle ARIA dropdowns/menus
+ const role = element.getAttribute('role');
+ if (role === 'menu' || role === 'listbox' || role === 'combobox') {
+ const menuItems = element.querySelectorAll('[role="menuitem"], [role="option"]');
+ const targetTextLower = targetText.toLowerCase();
+
+ for (const item of menuItems) {
+ if (item.textContent) {
+ const itemTextLower = item.textContent.trim().toLowerCase();
+ const itemValueLower = (item.getAttribute('data-value') || '').toLowerCase();
+
+ // Match against both text and data-value (case-insensitive)
+ if (itemTextLower === targetTextLower || itemValueLower === targetTextLower) {
+ // Clear previous selections
+ menuItems.forEach(mi => {
+ mi.setAttribute('aria-selected', 'false');
+ mi.classList.remove('selected');
+ });
+
+ // Select this item
+ item.setAttribute('aria-selected', 'true');
+ item.classList.add('selected');
+
+ // Trigger click and change events
+ item.click();
+ const clickEvent = new MouseEvent('click', { view: window, bubbles: true, cancelable: true });
+ item.dispatchEvent(clickEvent);
+
+ return {
+ success: true,
+ message: `Selected ARIA menu item: ${item.textContent.trim()}`
+ };
+ }
+ }
+ }
+
+ // Show all available options for debugging
+ const availableOptions = Array.from(menuItems).map(item => ({
+ text: item.textContent ? item.textContent.trim() : '',
+ value: item.getAttribute('data-value') || ''
+ })).filter(opt => opt.text || opt.value);
+
+ return {
+ success: false,
+ error: `Menu item with text or value '${targetText}' not found. Available options: ${JSON.stringify(availableOptions, null, 2)}`
+ };
+ }
+
+ // Handle Semantic UI or custom dropdowns
+ if (element.classList.contains('dropdown') || element.classList.contains('ui')) {
+ const menuItems = element.querySelectorAll('.item, .option, [data-value]');
+ const targetTextLower = targetText.toLowerCase();
+
+ for (const item of menuItems) {
+ if (item.textContent) {
+ const itemTextLower = item.textContent.trim().toLowerCase();
+ const itemValueLower = (item.getAttribute('data-value') || '').toLowerCase();
+
+ // Match against both text and data-value (case-insensitive)
+ if (itemTextLower === targetTextLower || itemValueLower === targetTextLower) {
+ // Clear previous selections
+ menuItems.forEach(mi => {
+ mi.classList.remove('selected', 'active');
+ });
+
+ // Select this item
+ item.classList.add('selected', 'active');
+
+ // Update dropdown text if there's a text element
+ const textElement = element.querySelector('.text');
+ if (textElement) {
+ textElement.textContent = item.textContent.trim();
+ }
+
+ // Trigger click and change events
+ item.click();
+ const clickEvent = new MouseEvent('click', { view: window, bubbles: true, cancelable: true });
+ item.dispatchEvent(clickEvent);
+
+ // Also dispatch on the main dropdown element
+ const dropdownChangeEvent = new Event('change', { bubbles: true });
+ element.dispatchEvent(dropdownChangeEvent);
+
+ return {
+ success: true,
+ message: `Selected custom dropdown item: ${item.textContent.trim()}`
+ };
+ }
+ }
+ }
+
+ // Show all available options for debugging
+ const availableOptions = Array.from(menuItems).map(item => ({
+ text: item.textContent ? item.textContent.trim() : '',
+ value: item.getAttribute('data-value') || ''
+ })).filter(opt => opt.text || opt.value);
+
+ return {
+ success: false,
+ error: `Custom dropdown item with text or value '${targetText}' not found. Available options: ${JSON.stringify(availableOptions, null, 2)}`
+ };
+ }
+
+ return null; // Not a dropdown element
+ }
+
+ // Function to recursively search children for dropdowns
+ function searchChildrenForSelection(element, maxDepth, currentDepth = 0) {
+ if (currentDepth >= maxDepth) return null;
+
+ // Check all direct children
+ for (let child of element.children) {
+ // Try selection on this child
+ const result = attemptSelection(child);
+ if (result && result.success) {
+ return result;
+ }
+
+ // Recursively check this child's children
+ const childResult = searchChildrenForSelection(child, maxDepth, currentDepth + 1);
+ if (childResult && childResult.success) {
+ return childResult;
+ }
+ }
+
+ return null;
+ }
+
+ // First try the target element itself
+ let selectionResult = attemptSelection(startElement);
+ if (selectionResult) {
+ // If attemptSelection returned a result (success or failure), use it
+ // Don't search children if we found a dropdown element but selection failed
+ return selectionResult;
+ }
+
+ // Only search children if target element is not a dropdown element
+ selectionResult = searchChildrenForSelection(startElement, 4);
+ if (selectionResult && selectionResult.success) {
+ return selectionResult;
+ }
+
+ return {
+ success: false,
+ error: `Element and its children (depth 4) do not contain a dropdown with option '${targetText}' (tag: ${startElement.tagName}, role: ${startElement.getAttribute('role')}, classes: ${startElement.className})`
+ };
+ }
+ """
+
+ result = await cdp_session.cdp_client.send.Runtime.callFunctionOn(
+ params={
+ 'functionDeclaration': selection_script,
+ 'arguments': [{'value': target_text}],
+ 'objectId': object_id,
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+
+ selection_result = result.get('result', {}).get('value', {})
+
+ if selection_result.get('success'):
+ msg = selection_result.get('message', f'Selected option: {target_text}')
+ self.logger.info(f'βœ… {msg}')
+
+ # Return the result as a dict
+ return {
+ 'success': 'true',
+ 'message': msg,
+ 'value': selection_result.get('value', target_text),
+ 'element_index': str(index_for_logging)
+ }
+ else:
+ error_msg = selection_result.get('error', f'Failed to select option: {target_text}')
+ self.logger.error(f'❌ {error_msg}')
+ raise ValueError(error_msg)
+
+ except Exception as e:
+ error_msg = f'Failed to select dropdown option: {str(e)}'
+ self.logger.error(error_msg)
+ raise ValueError(error_msg) from e
+
+ except Exception as e:
+ error_msg = f'Failed to select dropdown option "{target_text}" for element {index_for_logging}: {str(e)}'
+ self.logger.error(error_msg)
+ raise ValueError(error_msg) from e
diff --git a/browser_use/browser/events.py b/browser_use/browser/events.py
index b11dbb06f..7358780d9 100644
--- a/browser_use/browser/events.py
+++ b/browser_use/browser/events.py
@@ -214,6 +214,27 @@ class UploadFileEvent(ElementSelectedEvent[None]):
event_timeout: float | None = 30.0 # seconds
+class GetDropdownOptionsEvent(ElementSelectedEvent[dict[str, str]]):
+ """Get all options from any dropdown (native <select>, ARIA menus, or custom dropdowns).
+
+ Returns a dict containing dropdown type, options list, and element metadata."""
+
+ node: 'EnhancedDOMTreeNode'
+
+ event_timeout: float | None = 8.0 # seconds
+
+
+class SelectDropdownOptionEvent(ElementSelectedEvent[dict[str, str]]):
+ """Select a dropdown option by exact text from any dropdown type.
+
+ Returns a dict containing success status and selection details."""
+
+ node: 'EnhancedDOMTreeNode'
+ text: str # The option text to select
+
+ event_timeout: float | None = 8.0 # seconds
+
+
class ScrollToTextEvent(BaseEvent[None]):
"""Scroll to specific text on the page. Raises exception if text not found."""
diff --git a/browser_use/browser/watchdog_base.py b/browser_use/browser/watchdog_base.py
index bc2a150c7..783929a43 100644
--- a/browser_use/browser/watchdog_base.py
+++ b/browser_use/browser/watchdog_base.py
@@ -21,7 +21,10 @@ class BaseWatchdog(BaseModel):
"""
model_config = ConfigDict(
- arbitrary_types_allowed=True, validate_assignment=True, extra='forbid', revalidate_instances='never'
+ arbitrary_types_allowed=True, # allow non-serializable objects like EventBus/BrowserSession in fields
+ extra='forbid', # dont allow implicit class/instance state, everything must be a properly typed Field or PrivateAttr
+ validate_assignment=False, # avoid re-triggering __init__ / validators on values on every assignment
+ revalidate_instances='never', # avoid re-triggering __init__ / validators and erasing private attrs
)
# Class variables to statically define the list of events relevant to each watchdog
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index de319487c..7e9785918 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -715,197 +715,26 @@ Provide the extracted information in a clear, structured format."""
if node is None:
raise ValueError(f'Element index {params.index} not found in DOM')
- # Get CDP session for this node
- cdp_session = await browser_session.cdp_client_for_node(node)
-
- # Convert node to object ID for CDP operations
- try:
- object_result = await cdp_session.cdp_client.send.DOM.resolveNode(
- params={'backendNodeId': node.backend_node_id}, session_id=cdp_session.session_id
- )
- remote_object = object_result.get('object', {})
- object_id = remote_object.get('objectId')
- if not object_id:
- raise ValueError('Could not get object ID from resolved node')
- 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;
-
- // 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'
- };
- }
-
- // 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: '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;
- }
-
- // 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})`
- };
- }
- """
-
- 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', {})
-
- if dropdown_data.get('error'):
- raise ValueError(dropdown_data['error'])
-
- if not dropdown_data.get('options'):
- raise ValueError('No options found in dropdown')
-
- # 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}')
-
- dropdown_type = dropdown_data.get('type', 'unknown')
- element_info = f'ID: {dropdown_data.get("id", "none")}, Name: {dropdown_data.get("name", "none")}'
- source_info = dropdown_data.get('source', 'unknown')
-
- 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 += '\n\nUse the exact text string (without quotes) in select_dropdown_option'
+ # Dispatch GetDropdownOptionsEvent to the event handler
+ from browser_use.browser.events import GetDropdownOptionsEvent
+ import json
+
+ event = browser_session.event_bus.dispatch(GetDropdownOptionsEvent(node=node))
+ dropdown_data = await event.event_result()
- if source_info == 'target':
- logger.info(f'πŸ“‹ Found {len(dropdown_data["options"])} dropdown options for index {params.index}')
- else:
- logger.info(
- f'πŸ“‹ Found {len(dropdown_data["options"])} dropdown options for index {params.index} in {source_info}'
- )
- return ActionResult(
- extracted_content=msg,
- include_in_memory=True,
- long_term_memory=f'Found {len(dropdown_data["options"])} dropdown options for index {params.index}',
- include_extracted_content_only_once=True,
- )
+ if not dropdown_data:
+ raise ValueError('Failed to get dropdown options - no data returned')
- except Exception as e:
- error_msg = f'Failed to get dropdown options: {str(e)}'
- logger.error(error_msg)
- raise ValueError(error_msg) from e
+ # 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
+
+ return ActionResult(
+ extracted_content=msg,
+ include_in_memory=True,
+ long_term_memory=f'Found {options_count} dropdown options for index {params.index}',
+ include_extracted_content_only_once=True,
+ )
@self.registry.action(
'Select dropdown option by exact text from any dropdown type (native <select>, ARIA menus, or custom dropdowns). Searches target element and children to find selectable options.',
@@ -918,285 +747,23 @@ Provide the extracted information in a clear, structured format."""
if node is None:
raise ValueError(f'Element index {params.index} not found in DOM')
- # Get CDP session for this node
- cdp_session = await browser_session.cdp_client_for_node(node)
-
- # Convert node to object ID for CDP operations
- try:
- object_result = await cdp_session.cdp_client.send.DOM.resolveNode(
- params={'backendNodeId': node.backend_node_id}, session_id=cdp_session.session_id
- )
- remote_object = object_result.get('object', {})
- object_id = remote_object.get('objectId')
- if not object_id:
- raise ValueError('Could not get object ID from resolved node')
- except Exception as e:
- raise ValueError(f'Failed to resolve node to object: {e}') from e
-
- try:
- # Use JavaScript to select the option
- selection_script = """
- function(targetText) {
- const startElement = this;
-
- // Function to attempt selection on a dropdown element
- function attemptSelection(element) {
- // Handle native select elements
- if (element.tagName.toLowerCase() === 'select') {
- const options = Array.from(element.options);
- const targetTextLower = targetText.toLowerCase();
-
- for (const option of options) {
- const optionTextLower = option.text.trim().toLowerCase();
- const optionValueLower = option.value.toLowerCase();
-
- // Match against both text and value (case-insensitive)
- if (optionTextLower === targetTextLower || optionValueLower === targetTextLower) {
- element.value = option.value;
- option.selected = true;
-
- // Trigger change events
- const changeEvent = new Event('change', { bubbles: true });
- element.dispatchEvent(changeEvent);
-
- return {
- success: true,
- message: `Selected option: ${option.text.trim()} (value: ${option.value})`,
- value: option.value
- };
- }
- }
-
- // Show all available options for debugging
- const availableOptions = options.map(opt => ({
- text: opt.text.trim(),
- value: opt.value
- }));
-
- return {
- success: false,
- error: `Option with text or value '${targetText}' not found in select element. Available options: ${JSON.stringify(availableOptions, null, 2)}`
- };
- }
-
- // Handle ARIA dropdowns/menus
- const role = element.getAttribute('role');
- if (role === 'menu' || role === 'listbox' || role === 'combobox') {
- const menuItems = element.querySelectorAll('[role="menuitem"], [role="option"]');
- const targetTextLower = targetText.toLowerCase();
-
- for (const item of menuItems) {
- if (item.textContent) {
- const itemTextLower = item.textContent.trim().toLowerCase();
- const itemValueLower = (item.getAttribute('data-value') || '').toLowerCase();
-
- // Match against both text and data-value (case-insensitive)
- if (itemTextLower === targetTextLower || itemValueLower === targetTextLower) {
- // Clear previous selections
- menuItems.forEach(mi => {
- mi.setAttribute('aria-selected', 'false');
- mi.classList.remove('selected');
- });
-
- // Select this item
- item.setAttribute('aria-selected', 'true');
- item.classList.add('selected');
-
- // Trigger click and change events
- item.click();
- const clickEvent = new MouseEvent('click', { view: window, bubbles: true, cancelable: true });
- item.dispatchEvent(clickEvent);
-
- return {
- success: true,
- message: `Selected ARIA menu item: ${item.textContent.trim()}`
- };
- }
- }
- }
-
- // Show all available options for debugging
- const availableOptions = Array.from(menuItems).map(item => ({
- text: item.textContent ? item.textContent.trim() : '',
- value: item.getAttribute('data-value') || ''
- })).filter(opt => opt.text || opt.value);
-
- return {
- success: false,
- error: `Menu item with text or value '${targetText}' not found. Available options: ${JSON.stringify(availableOptions, null, 2)}`
- };
- }
-
- // Handle Semantic UI or custom dropdowns
- if (element.classList.contains('dropdown') || element.classList.contains('ui')) {
- const menuItems = element.querySelectorAll('.item, .option, [data-value]');
- const targetTextLower = targetText.toLowerCase();
-
- for (const item of menuItems) {
- if (item.textContent) {
- const itemTextLower = item.textContent.trim().toLowerCase();
- const itemValueLower = (item.getAttribute('data-value') || '').toLowerCase();
-
- // Match against both text and data-value (case-insensitive)
- if (itemTextLower === targetTextLower || itemValueLower === targetTextLower) {
- // Clear previous selections
- menuItems.forEach(mi => {
- mi.classList.remove('selected', 'active');
- });
-
- // Select this item
- item.classList.add('selected', 'active');
-
- // Update dropdown text if there's a text element
- const textElement = element.querySelector('.text');
- if (textElement) {
- textElement.textContent = item.textContent.trim();
- }
-
- // Trigger click and change events
- item.click();
- const clickEvent = new MouseEvent('click', { view: window, bubbles: true, cancelable: true });
- item.dispatchEvent(clickEvent);
-
- // Also dispatch on the main dropdown element
- const dropdownChangeEvent = new Event('change', { bubbles: true });
- element.dispatchEvent(dropdownChangeEvent);
-
- return {
- success: true,
- message: `Selected custom dropdown item: ${item.textContent.trim()}`
- };
- }
- }
- }
-
- // Show all available options for debugging
- const availableOptions = Array.from(menuItems).map(item => ({
- text: item.textContent ? item.textContent.trim() : '',
- value: item.getAttribute('data-value') || ''
- })).filter(opt => opt.text || opt.value);
-
- return {
- success: false,
- error: `Custom dropdown item with text or value '${targetText}' not found. Available options: ${JSON.stringify(availableOptions, null, 2)}`
- };
- }
-
- return null; // Not a dropdown element
- }
-
- // Function to recursively search children for dropdowns
- function searchChildrenForSelection(element, maxDepth, currentDepth = 0) {
- if (currentDepth >= maxDepth) return null;
-
- // Check all direct children
- for (let child of element.children) {
- // Try selection on this child
- const result = attemptSelection(child);
- if (result && result.success) {
- return result;
- }
-
- // Recursively check this child's children
- const childResult = searchChildrenForSelection(child, maxDepth, currentDepth + 1);
- if (childResult && childResult.success) {
- return childResult;
- }
- }
-
- return null;
- }
-
- // First try the target element itself
- let selectionResult = attemptSelection(startElement);
- if (selectionResult) {
- // If attemptSelection returned a result (success or failure), use it
- // Don't search children if we found a dropdown element but selection failed
- return selectionResult;
- }
-
- // Only search children if target element is not a dropdown element
- selectionResult = searchChildrenForSelection(startElement, 4);
- if (selectionResult && selectionResult.success) {
- return selectionResult;
- }
-
- return {
- success: false,
- error: `Element and its children (depth 4) do not contain a dropdown with option '${targetText}' (tag: ${startElement.tagName}, role: ${startElement.getAttribute('role')}, classes: ${startElement.className})`
- };
- }
- """
-
- result = await cdp_session.cdp_client.send.Runtime.callFunctionOn(
- params={
- 'functionDeclaration': selection_script,
- 'arguments': [{'value': params.text}],
- 'objectId': object_id,
- 'returnByValue': True,
- },
- session_id=cdp_session.session_id,
- )
-
- selection_result = result.get('result', {}).get('value', {})
+ # Dispatch SelectDropdownOptionEvent to the event handler
+ from browser_use.browser.events import SelectDropdownOptionEvent
+
+ event = browser_session.event_bus.dispatch(SelectDropdownOptionEvent(node=node, text=params.text))
+ selection_data = await event.event_result()
- if selection_result.get('success'):
- msg = selection_result.get('message', f'Selected option: {params.text}')
- logger.info(f'βœ… {msg}')
- return ActionResult(
- extracted_content=msg,
- include_in_memory=True,
- long_term_memory=f"Selected dropdown option '{params.text}' at index {params.index}",
- )
- else:
- # Handle error case with clean error message and extracted options
- full_error = selection_result.get('error', f'Failed to select option: {params.text}')
-
- # Parse available options from the error message if present
- available_options = None
- if 'Available options:' in full_error:
- try:
- # Extract JSON part from error message
- json_start = full_error.find('Available options:') + len('Available options:')
- json_str = full_error[json_start:].strip()
- available_options = json.loads(json_str)
- except (json.JSONDecodeError, ValueError):
- # If parsing fails, just use the full error
- pass
-
- # Create clean error message
- clean_error = f"Failed to select dropdown option: {params.text}"
- logger.error(f'❌ {clean_error}')
-
- # Format available options for extracted_content if we have them
- if available_options:
- formatted_options = []
- for i, opt in enumerate(available_options):
- text = opt.get('text', '')
- value = opt.get('value', '')
- if text:
- formatted_options.append(f'{i}: text="{text}", value="{value}"')
-
- extracted_content = 'Available dropdown options:\n' + '\n'.join(formatted_options)
-
- return ActionResult(
- error=clean_error,
- extracted_content=extracted_content,
- include_extracted_content_only_once=True,
- )
- else:
- # No parseable options, return the clean error
- return ActionResult(
- error=clean_error,
- )
+ if not selection_data:
+ raise ValueError('Failed to select dropdown option - no data returned')
- except Exception as e:
- # Handle unexpected exceptions
- clean_error = f"Failed to select dropdown option: {params.text}"
- logger.error(f'❌ {clean_error} - Details: {str(e)}')
- return ActionResult(
- error=clean_error,
- )
+ # 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}",
+ )
# File System Actions
@self.registry.action(
diff --git a/tests/ci/test_controller.py b/tests/ci/test_controller.py
index 7847a81bb..4b552386d 100644
--- a/tests/ci/test_controller.py
+++ b/tests/ci/test_controller.py
@@ -681,19 +681,23 @@ class TestControllerIntegration:
await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- # Wait for the page to load
- page = await browser_session.get_current_page()
- await page.wait_for_load_state()
+ # Wait for the page to load using CDP
+ cdp_session = browser_session.agent_focus
+ assert cdp_session is not None, "CDP session not initialized"
+
+ # Wait for page load by checking document ready state
+ await asyncio.sleep(0.5) # Brief wait for navigation to start
+ ready_state = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.readyState'},
+ session_id=cdp_session.session_id
+ )
+ # If not complete, wait a bit more
+ if ready_state.get('result', {}).get('value') != 'complete':
+ await asyncio.sleep(1.0)
# Initialize the DOM state to populate the selector map
await browser_session.get_state_summary(cache_clickable_elements_hashes=True)
- # Interact with the dropdown to ensure it's recognized
- await page.click('select#test-dropdown')
-
- # Update the state after interaction
- await browser_session.get_state_summary(cache_clickable_elements_hashes=True)
-
# Get the selector map
selector_map = await browser_session.get_selector_map()
@@ -792,9 +796,19 @@ class TestControllerIntegration:
await controller.act(GoToUrlActionModel(**goto_action), browser_session)
- # Wait for the page to load
- page = await browser_session.get_current_page()
- await page.wait_for_load_state()
+ # Wait for the page to load using CDP
+ cdp_session = browser_session.agent_focus
+ assert cdp_session is not None, "CDP session not initialized"
+
+ # Wait for page load by checking document ready state
+ await asyncio.sleep(0.5) # Brief wait for navigation to start
+ ready_state = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'document.readyState'},
+ session_id=cdp_session.session_id
+ )
+ # If not complete, wait a bit more
+ if ready_state.get('result', {}).get('value') != 'complete':
+ await asyncio.sleep(1.0)
# populate the selector map with highlight indices
await browser_session.get_state_summary(cache_clickable_elements_hashes=True)
@@ -831,6 +845,10 @@ class TestControllerIntegration:
assert 'selected option' in result.extracted_content.lower()
assert 'Second Option' in result.extracted_content
- # Verify the actual dropdown selection was made by checking the DOM
- selected_value = await page.evaluate("document.getElementById('test-dropdown').value")
+ # Verify the actual dropdown selection was made by checking the DOM using CDP
+ selected_value_result = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': "document.getElementById('test-dropdown').value"},
+ session_id=cdp_session.session_id
+ )
+ selected_value = selected_value_result.get('result', {}).get('value')
assert selected_value == 'option2' # Second Option has value "option2"