Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): match a native select option by its rendered label (#20490)

Arvin Xu646b2c9efe67
diff --git a/apps/desktop/src/main/controllers/BrowserControlCtr.ts b/apps/desktop/src/main/controllers/BrowserControlCtr.ts
index 3fcb3f7e307..47e96376146 100644
--- a/apps/desktop/src/main/controllers/BrowserControlCtr.ts
+++ b/apps/desktop/src/main/controllers/BrowserControlCtr.ts
@@ -194,11 +194,13 @@ export const fillScript = (ref: string, text: string) => `((ref, text) => {
// setter on it throws. Pick the option by value, then by visible label.
if (el.tagName === 'SELECT') {
const options = Array.from(el.options);
- const label = (option) => option.text.trim().replaceAll(/\\s+/g, ' ').toLowerCase();
+ // The rendered label is the non-empty label attribute, else the text.
+ const shown = (option) => (option.getAttribute('label') || option.text).trim();
+ const label = (option) => shown(option).replaceAll(/\\s+/g, ' ').toLowerCase();
const wanted = String(text).trim().replaceAll(/\\s+/g, ' ').toLowerCase();
const option = options.find((o) => o.value === text) || options.find((o) => label(o) === wanted);
if (!option) {
- const names = options.map((o) => o.text.trim()).slice(0, 30).join(', ');
+ const names = options.map(shown).slice(0, 30).join(', ');
return JSON.stringify({ error: 'no option matches "' + text + '"; options: ' + names });
}
// Assign by index: option values need not be unique (an empty placeholder
diff --git a/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.fill.test.ts b/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.fill.test.ts
index 112818271ce..f75772fe601 100644
--- a/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.fill.test.ts
+++ b/apps/desktop/src/main/controllers/__tests__/BrowserControlCtr.fill.test.ts
@@ -54,6 +54,22 @@ describe('fillScript on a native select', () => {
expect(select.selectedIndex).toBe(1);
});
+ it('matches the rendered label of an option with a label attribute', () => {
+ document.body.innerHTML = `
+ <select aria-label="Language">
+ <option value="zh" label="Chinese">中文(简体)</option>
+ <option value="en" label="English">English (US)</option>
+ </select>`;
+ const select = document.querySelector('select')!;
+ (window as any).__lobeBrowserRefs = { e1: select };
+
+ expect(JSON.parse((0, eval)(fillScript('e1', 'English')))).toEqual({ ok: true });
+ expect(select.value).toBe('en');
+ expect(JSON.parse((0, eval)(fillScript('e1', 'French'))).error).toBe(
+ 'no option matches "French"; options: Chinese, English',
+ );
+ });
+
it('lists the available options when nothing matches', () => {
const { result, value } = fillSelect('Curse');