Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
use playwright.Locator.select_option instead of custom js
shyam-kingec952493e05c
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 6b04d8754..c79f39c45 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -358,6 +358,8 @@ class Controller:logger.debug(f'Element attributes: {dom_element.attributes}')logger.debug(f'Element tag: {dom_element.tag_name}')+ xpath = "//" + dom_element.xpath+try:frame_index = 0for frame in page.frames:@@ -403,55 +405,14 @@ class Controller:logger.debug(f'Found dropdown in frame {frame_index}: {dropdown_info}')- # Rest of the selection code remains the same...- select_option_js = """- (params) => {- try {- const select = document.evaluate(params.xpath, document, null,- XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;- if (!select || select.tagName.toLowerCase() !== 'select') {- return {success: false, error: 'Select not found or invalid element type'};- }-- const option = Array.from(select.options)- .find(opt => opt.text.trim() === params.text);-- if (!option) {- return {- success: false,- error: 'Option not found',- availableOptions: Array.from(select.options).map(o => o.text.trim())- };- }-- select.value = option.value;- select.dispatchEvent(new Event('change', { bubbles: true }));- return {- success: true,- selectedValue: option.value,- selectedText: option.text.trim()- };- } catch (e) {- return {success: false, error: e.toString()};- }- }- """-- params = {'xpath': dom_element.xpath, 'text': text}+ # "label" because we are selecting by text+ # nth(0) to disable error thrown by strict mode+ selected_option_values = await frame.locator("//" + dom_element.xpath).nth(0).select_option(label=text)- result = await frame.evaluate(select_option_js, params)- logger.debug(f'Selection result: {result}')+ msg = f"selected option {text} with value {selected_option_values}"+ logger.info(msg + f' in frame {frame_index}')- if result.get('success'):- msg = (- f"Selected option '{text}' (value={result.get('selectedValue')}"- )- logger.info(msg + f' in frame {frame_index}')- return ActionResult(extracted_content=msg, include_in_memory=True)- else:- logger.error(f"Selection failed: {result.get('error')}")- if 'availableOptions' in result:- logger.error(f"Available options: {result['availableOptions']}")+ return ActionResult(extracted_content=msg, include_in_memory=True)except Exception as frame_e:logger.error(f'Frame {frame_index} attempt failed: {str(frame_e)}')