This commit is contained in:
Josh at WLTechBlog 2025-08-19 06:32:41 -05:00
parent 36adab7878
commit bfbda8e6a3
1 changed files with 20 additions and 9 deletions

View File

@ -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)
}
}