Skip to content

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

more get_dropdown_options fixes

Nick Sweetingd509759c5737
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index b3f1f6b5d..f5e75b789 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -703,7 +703,7 @@ Provide the extracted information in a clear, structured format."""
# Dropdown Actions
@self.registry.action(
- 'Get all options from any dropdown (native <select>, ARIA menus, or custom dropdowns like Semantic UI). Searches target element and up to 4 levels of children to find dropdowns. This only works on dropdown elements.',
+ 'Get list of options from any dropdown (<select>, ARIA menus, or custom dropdowns like Semantic UI). Searches target element and up to 4 levels of children to find dropdowns. This only works on dropdown elements, not other input types.',
param_model=GetDropdownOptionsAction,
)
async def get_dropdown_options(params: GetDropdownOptionsAction, browser_session: BrowserSession):
@@ -719,7 +719,7 @@ Provide the extracted information in a clear, structured format."""
from browser_use.browser.events import GetDropdownOptionsEvent
event = browser_session.event_bus.dispatch(GetDropdownOptionsEvent(node=node))
- dropdown_data = await event.event_result()
+ dropdown_data = await event.event_result(timeout=3.0, raise_if_none=True, raise_if_any=True)
if not dropdown_data:
raise ValueError('Failed to get dropdown options - no data returned')
@@ -825,297 +825,6 @@ Provide the extracted information in a clear, structured format."""
include_extracted_content_only_once=True,
)
- # TODO: Refactor to use events instead of direct page/dom access
- # @self.registry.action(
- # description='Get all options from a native dropdown or ARIA menu',
- # )
- # async def get_dropdown_options(index: int, browser_session: BrowserSession) -> ActionResult:
- # """Get all options from a native dropdown or ARIA menu"""
-
- # dom_element = await browser_session.get_dom_element_by_index(index)
- # if dom_element is None:
- # raise Exception(f'Element index {index} does not exist - retry or use alternative actions')
-
- # try:
- # # Frame-aware approach since we know it works
- # all_options = []
- # frame_index = 0
-
- # for frame in page.frames:
- # try:
- # # First check if it's a native select element
- # options = await frame.evaluate(
- # """
- # (xpath) => {
- # const element = document.evaluate(xpath, document, null,
- # XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- # if (!element) return null;
-
- # // Check if it's a native select element
- # if (element.tagName.toLowerCase() === 'select') {
- # return {
- # type: 'select',
- # options: Array.from(element.options).map(opt => ({
- # text: opt.text, //do not trim, because we are doing exact match in select_dropdown_option
- # value: opt.value,
- # index: opt.index
- # })),
- # id: element.id,
- # name: element.name
- # };
- # }
-
- # // Check if it's an ARIA menu
- # if (element.getAttribute('role') === 'menu' ||
- # element.getAttribute('role') === 'listbox' ||
- # element.getAttribute('role') === 'combobox') {
- # // Find all menu items
- # const menuItems = element.querySelectorAll('[role="menuitem"], [role="option"]');
- # const options = [];
-
- # menuItems.forEach((item, idx) => {
- # // Get the text content of the menu item
- # const text = item.textContent.trim();
- # if (text) {
- # options.push({
- # text: text,
- # value: text, // For ARIA menus, use text as value
- # index: idx
- # });
- # }
- # });
-
- # return {
- # type: 'aria',
- # options: options,
- # id: element.id || '',
- # name: element.getAttribute('aria-label') || ''
- # };
- # }
-
- # return null;
- # }
- # """,
- # dom_element.xpath,
- # )
-
- # if options:
- # logger.debug(f'Found {options["type"]} dropdown in frame {frame_index}')
- # logger.debug(f'Element ID: {options["id"]}, Name: {options["name"]}')
-
- # formatted_options = []
- # for opt in options['options']:
- # # encoding ensures AI uses the exact string in select_dropdown_option
- # encoded_text = json.dumps(opt['text'])
- # formatted_options.append(f'{opt["index"]}: text={encoded_text}')
-
- # all_options.extend(formatted_options)
-
- # except Exception as frame_e:
- # logger.debug(f'Frame {frame_index} evaluation failed: {str(frame_e)}')
-
- # frame_index += 1
-
- # if all_options:
- # msg = '\n'.join(all_options)
- # msg += '\nUse the exact text string in select_dropdown_option'
- # logger.info(msg)
- # return ActionResult(
- # extracted_content=msg,
- # include_in_memory=True,
- # long_term_memory=f'Found dropdown options for index {index}.',
- # include_extracted_content_only_once=True,
- # )
- # else:
- # msg = 'No options found in any frame for dropdown'
- # logger.info(msg)
- # return ActionResult(
- # extracted_content=msg, include_in_memory=True, long_term_memory='No dropdown options found'
- # )
-
- # except Exception as e:
- # logger.error(f'Failed to get dropdown options: {str(e)}')
- # msg = f'Error getting options: {str(e)}'
- # logger.info(msg)
- # return ActionResult(extracted_content=msg, include_in_memory=True)
-
- # TODO: Refactor to use events instead of direct page/dom access
- # @self.registry.action(
- # description='Select dropdown option or ARIA menu item for interactive element index by the text of the option you want to select',
- # )
- # async def select_dropdown_option(
- # index: int,
- # text: str,
- # browser_session: BrowserSession,
- # ) -> ActionResult:
- # """Select dropdown option or ARIA menu item by the text of the option you want to select"""
- # page = await browser_session.get_current_page()
- # dom_element = await browser_session.get_dom_element_by_index(index)
- # if dom_element is None:
- # raise Exception(f'Element index {index} does not exist - retry or use alternative actions')
-
- # logger.debug(f"Attempting to select '{text}' using xpath: {dom_element.xpath}")
- # logger.debug(f'Element attributes: {dom_element.attributes}')
- # logger.debug(f'Element tag: {dom_element.tag_name}')
-
- # xpath = '//' + dom_element.xpath
-
- # try:
- # frame_index = 0
- # for frame in page.frames:
- # try:
- # logger.debug(f'Trying frame {frame_index} URL: {frame.url}')
-
- # # First check what type of element we're dealing with
- # element_info_js = """
- # (xpath) => {
- # try {
- # const element = document.evaluate(xpath, document, null,
- # XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- # if (!element) return null;
-
- # const tagName = element.tagName.toLowerCase();
- # const role = element.getAttribute('role');
-
- # // Check if it's a native select
- # if (tagName === 'select') {
- # return {
- # type: 'select',
- # found: true,
- # id: element.id,
- # name: element.name,
- # tagName: element.tagName,
- # optionCount: element.options.length,
- # currentValue: element.value,
- # availableOptions: Array.from(element.options).map(o => o.text.trim())
- # };
- # }
-
- # // Check if it's an ARIA menu or similar
- # if (role === 'menu' || role === 'listbox' || role === 'combobox') {
- # const menuItems = element.querySelectorAll('[role="menuitem"], [role="option"]');
- # return {
- # type: 'aria',
- # found: true,
- # id: element.id || '',
- # role: role,
- # tagName: element.tagName,
- # itemCount: menuItems.length,
- # availableOptions: Array.from(menuItems).map(item => item.textContent.trim())
- # };
- # }
-
- # return {
- # error: `Element is neither a select nor an ARIA menu (tag: ${tagName}, role: ${role})`,
- # found: false
- # };
- # } catch (e) {
- # return {error: e.toString(), found: false};
- # }
- # }
- # """
-
- # element_info = await frame.evaluate(element_info_js, dom_element.xpath)
-
- # if element_info and element_info.get('found'):
- # logger.debug(f'Found {element_info.get("type")} element in frame {frame_index}: {element_info}')
-
- # if element_info.get('type') == 'select':
- # # Handle native select element
- # # "label" because we are selecting by text
- # # nth(0) to disable error thrown by strict mode
- # # timeout=1000 because we are already waiting for all network events
- # selected_option_values = (
- # await frame.locator('//' + dom_element.xpath).nth(0).select_option(label=text, timeout=1000)
- # )
-
- # msg = f'selected option {text} with value {selected_option_values}'
- # logger.info(msg + f' in frame {frame_index}')
-
- # return ActionResult(
- # extracted_content=msg, include_in_memory=True, long_term_memory=f"Selected option '{text}'"
- # )
-
- # elif element_info.get('type') == 'aria':
- # # Handle ARIA menu
- # click_aria_item_js = """
- # (params) => {
- # const { xpath, targetText } = params;
- # try {
- # const element = document.evaluate(xpath, document, null,
- # XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- # if (!element) return {success: false, error: 'Element not found'};
-
- # // Find all menu items
- # const menuItems = element.querySelectorAll('[role="menuitem"], [role="option"]');
-
- # for (const item of menuItems) {
- # const itemText = item.textContent.trim();
- # if (itemText === targetText) {
- # // Simulate click on the menu item
- # item.click();
-
- # // Also try dispatching a click event in case the click handler needs it
- # const clickEvent = new MouseEvent('click', {
- # view: window,
- # bubbles: true,
- # cancelable: true
- # });
- # item.dispatchEvent(clickEvent);
-
- # return {
- # success: true,
- # message: `Clicked menu item: ${targetText}`
- # };
- # }
- # }
-
- # return {
- # success: false,
- # error: `Menu item with text '${targetText}' not found`
- # };
- # } catch (e) {
- # return {success: false, error: e.toString()};
- # }
- # }
- # """
-
- # result = await frame.evaluate(
- # click_aria_item_js, {'xpath': dom_element.xpath, 'targetText': text}
- # )
-
- # if result.get('success'):
- # msg = result.get('message', f'Selected ARIA menu item: {text}')
- # logger.info(msg + f' in frame {frame_index}')
- # return ActionResult(
- # extracted_content=msg,
- # include_in_memory=True,
- # long_term_memory=f"Selected menu item '{text}'",
- # )
- # else:
- # logger.error(f'Failed to select ARIA menu item: {result.get("error")}')
- # continue
-
- # elif element_info:
- # logger.error(f'Frame {frame_index} error: {element_info.get("error")}')
- # continue
-
- # except Exception as frame_e:
- # logger.error(f'Frame {frame_index} attempt failed: {str(frame_e)}')
- # logger.error(f'Frame type: {type(frame)}')
- # logger.error(f'Frame URL: {frame.url}')
-
- # frame_index += 1
-
- # msg = f"Could not select option '{text}' in any frame"
- # logger.info(msg)
- # return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)
-
- # except Exception as e:
- # msg = f'Selection failed: {str(e)}'
- # logger.error(msg)
- # raise BrowserError(msg)
-
# @self.registry.action('Google Sheets: Get the contents of the entire sheet', domains=['https://docs.google.com'])
# async def read_sheet_contents(browser_session: BrowserSession):
# # Use send keys events to select and copy all cells
diff --git a/tests/ci/test_controller.py b/tests/ci/test_controller.py
index 4b552386d..a84f4f7d3 100644
--- a/tests/ci/test_controller.py
+++ b/tests/ci/test_controller.py
@@ -683,20 +683,19 @@ class TestControllerIntegration:
# Wait for the page to load using CDP
cdp_session = browser_session.agent_focus
- assert cdp_session is not None, "CDP session not initialized"
-
+ 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
+ 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)
+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
# Get the selector map
selector_map = await browser_session.get_selector_map()
@@ -738,18 +737,28 @@ class TestControllerIntegration:
assert option['text'] in result.extracted_content, f"Option '{option['text']}' not found in result content"
# Verify the instruction for using the text in select_dropdown_option is included
- assert 'Use the exact text string in select_dropdown_option' in result.extracted_content
-
- # Verify the actual dropdown options in the DOM
- dropdown_options = await page.evaluate("""
- () => {
- const select = document.getElementById('test-dropdown');
- return Array.from(select.options).map(opt => ({
- text: opt.text,
- value: opt.value
- }));
- }
- """)
+ assert 'Use the exact text string' in result.extracted_content and 'select_dropdown_option' in result.extracted_content
+
+ # Verify the actual dropdown options in the DOM using CDP
+ dropdown_options_result = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={
+ 'expression': """
+ JSON.stringify((() => {
+ const select = document.getElementById('test-dropdown');
+ return Array.from(select.options).map(opt => ({
+ text: opt.text,
+ value: opt.value
+ }));
+ })())
+ """,
+ 'returnByValue': True,
+ },
+ session_id=cdp_session.session_id,
+ )
+ dropdown_options_json = dropdown_options_result.get('result', {}).get('value', '[]')
+ import json
+
+ dropdown_options = json.loads(dropdown_options_json) if isinstance(dropdown_options_json, str) else dropdown_options_json
# Verify the dropdown has the expected options
assert len(dropdown_options) == len(expected_options), (
@@ -798,20 +807,19 @@ class TestControllerIntegration:
# Wait for the page to load using CDP
cdp_session = browser_session.agent_focus
- assert cdp_session is not None, "CDP session not initialized"
-
+ 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
+ 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)
+ await browser_session.get_browser_state_summary(cache_clickable_elements_hashes=True)
# Now get the selector map which should contain our dropdown
selector_map = await browser_session.get_selector_map()
@@ -847,8 +855,7 @@ class TestControllerIntegration:
# 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
+ 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"