Skip to content

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

encode both text and value, and update the output format for better consistency

shyam-kingdcc3b3de290c
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index 93f3758f3..e3c92deb0 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -1,5 +1,6 @@
import asyncio
import logging
+import json
from main_content_extractor import MainContentExtractor
from playwright.async_api import Page
@@ -288,7 +289,7 @@ class Controller:
return {
options: Array.from(select.options).map(opt => ({
- text: opt.text.trim(),
+ text: opt.text, //do not trim, because we are doing exact match in select_dropdown_option
value: opt.value,
index: opt.index
})),
@@ -306,8 +307,11 @@ class Controller:
formatted_options = []
for opt in options['options']:
+ # encoding ensures AI uses the exact string in select_dropdown_option
+ encoded_text = json.dumps(opt["text"])
+ encoded_value = json.dumps(opt["value"])
formatted_options.append(
- f"{opt['index']}: {opt['text']} (value={opt['value']})"
+ f"{opt['index']}: text={encoded_text} value={encoded_value}"
)
all_options.extend(formatted_options)
@@ -414,13 +418,13 @@ class Controller:
}
const option = Array.from(select.options)
- .find(opt => opt.text.trim() === params.text);
+ .find(opt => opt.text === params.text);
if (!option) {
return {
success: false,
error: 'Option not found',
- availableOptions: Array.from(select.options).map(o => o.text.trim())
+ availableOptions: Array.from(select.options).map(o => o.text)
};
}
@@ -429,7 +433,7 @@ class Controller:
return {
success: true,
selectedValue: option.value,
- selectedText: option.text.trim()
+ selectedText: option.text
};
} catch (e) {
return {success: false, error: e.toString()};
@@ -444,7 +448,7 @@ class Controller:
if result.get('success'):
msg = (
- f"Selected option '{text}' (value={result.get('selectedValue')}"
+ f"Selected option {json.dumps(text)} (value={result.get('selectedValue')}"
)
logger.info(msg + f' in frame {frame_index}')
return ActionResult(extracted_content=msg, include_in_memory=True)