diff --git a/daemon/daemon.go b/daemon/daemon.go index e0d63ef..5abbe9a 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -1964,16 +1964,27 @@ func (d *Daemon) selectElement(tabID, selector, value string, selectionTimeout, // Try to select by text first (most common case) err = element.Select([]string{value}, true, rod.SelectorTypeText) if err != nil { - // If text selection failed, the value might be the actual option value - // Try to find and select by matching option value using page.Eval - script := fmt.Sprintf(`(function(){ var el = document.querySelector("%s"); if(el) { el.value = "%s"; el.dispatchEvent(new Event('change', { bubbles: true })); } })()`, selector, value) - // Execute JavaScript and ignore any rod evaluation quirks - page.Eval(script) + // If text selection failed, use JavaScript as fallback + // This handles both option value and option text selection + script := fmt.Sprintf(`(function(){ + var el = document.querySelector("%s"); + if(el) { + el.value = "%s"; + el.dispatchEvent(new Event('change', { bubbles: true })); + return el.value; + } + return null; + })()`, selector, value) - // Verify the selection worked by checking the value - actualValue, err := element.Attribute("value") - if err != nil || actualValue == nil || *actualValue != value { - return fmt.Errorf("failed to select option '%s' in element", value) + // Execute JavaScript and get the result + result, err := page.Eval(script) + if err != nil { + return fmt.Errorf("failed to execute JavaScript selection: %w", err) + } + + // Check if JavaScript selection worked + if result.Value.String() != value { + return fmt.Errorf("failed to select option '%s' in element (JavaScript fallback failed)", value) } }