This commit is contained in:
Josh at WLTechBlog 2025-08-19 07:52:34 -05:00
parent a6489cd5d0
commit 1e142c1f3f
1 changed files with 94 additions and 21 deletions

View File

@ -1966,7 +1966,31 @@ func (d *Daemon) selectElement(tabID, selector, value string, selectionTimeout,
if err != nil {
// If text selection failed, use JavaScript as fallback
// This handles both option value and option text selection
script := fmt.Sprintf(`document.querySelector("%s").value = "%s"; document.querySelector("%s").dispatchEvent(new Event('change', { bubbles: true })); document.querySelector("%s").value`, selector, value, selector, selector)
script := fmt.Sprintf(`
(function() {
var select = document.querySelector("%s");
if (!select) return null;
// First try to select by value
select.value = "%s";
if (select.value === "%s") {
select.dispatchEvent(new Event('change', { bubbles: true }));
return select.value;
}
// If value selection failed, try to find by text content
var options = select.options;
for (var i = 0; i < options.length; i++) {
if (options[i].text === "%s") {
select.selectedIndex = i;
select.dispatchEvent(new Event('change', { bubbles: true }));
return select.value;
}
}
return null;
})()
`, selector, value, value, value)
// Execute JavaScript and get the result
result, err := page.Eval(script)
@ -1975,8 +1999,8 @@ func (d *Daemon) selectElement(tabID, selector, value string, selectionTimeout,
}
// Check if JavaScript selection worked
if result.Value.String() != value {
return fmt.Errorf("failed to select option '%s' in element (JavaScript fallback failed)", value)
if result.Value.Nil() {
return fmt.Errorf("failed to select option '%s' in element (no matching option found)", value)
}
}
@ -3575,20 +3599,42 @@ func (d *Daemon) interactMultiple(tabID, interactionsJSON string, timeout int) (
// Try to select by text first (most common case)
err = element.Select([]string{interaction.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 })); } })()`, interaction.Selector, interaction.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 select = document.querySelector("%s");
if (!select) return null;
// Always verify success by checking the element's actual value
currentValue, err := element.Property("value")
// First try to select by value
select.value = "%s";
if (select.value === "%s") {
select.dispatchEvent(new Event('change', { bubbles: true }));
return select.value;
}
// If value selection failed, try to find by text content
var options = select.options;
for (var i = 0; i < options.length; i++) {
if (options[i].text === "%s") {
select.selectedIndex = i;
select.dispatchEvent(new Event('change', { bubbles: true }));
return select.value;
}
}
return null;
})()
`, interaction.Selector, interaction.Value, interaction.Value, interaction.Value)
// Execute JavaScript and get the result
result, err := page.Eval(script)
if err != nil {
interactionResult.Error = fmt.Sprintf("failed to verify selection: %v", err)
} else if currentValue.Str() == interaction.Value {
interactionResult.Success = true
interactionResult.Error = fmt.Sprintf("failed to execute JavaScript selection: %v", err)
} else if result.Value.Nil() {
interactionResult.Error = fmt.Sprintf("failed to select option '%s' (no matching option found)", interaction.Value)
} else {
interactionResult.Error = fmt.Sprintf("failed to select option: %s (current value: %s)", interaction.Value, currentValue.Str())
interactionResult.Success = true
}
} else {
interactionResult.Success = true
@ -3775,14 +3821,41 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
fieldResult.Action = "select"
err = element.Select([]string{fieldValue}, true, rod.SelectorTypeText)
if err != nil {
// If text selection failed, try by value
script := fmt.Sprintf(`(function(){ var el = document.querySelector("%s"); if(el) { el.value = "%s"; el.dispatchEvent(new Event('change', { bubbles: true })); } })()`, fieldResult.Selector, fieldValue)
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 select = document.querySelector("%s");
if (!select) return null;
// Verify the selection worked
actualValue, err := element.Attribute("value")
if err != nil || actualValue == nil || *actualValue != fieldValue {
fieldResult.Error = fmt.Sprintf("failed to select option '%s'", fieldValue)
// First try to select by value
select.value = "%s";
if (select.value === "%s") {
select.dispatchEvent(new Event('change', { bubbles: true }));
return select.value;
}
// If value selection failed, try to find by text content
var options = select.options;
for (var i = 0; i < options.length; i++) {
if (options[i].text === "%s") {
select.selectedIndex = i;
select.dispatchEvent(new Event('change', { bubbles: true }));
return select.value;
}
}
return null;
})()
`, fieldResult.Selector, fieldValue, fieldValue, fieldValue)
// Execute JavaScript and get the result
jsResult, err := page.Eval(script)
if err != nil {
fieldResult.Error = fmt.Sprintf("failed to execute JavaScript selection: %v", err)
result.ErrorCount++
} else if jsResult.Value.Nil() {
fieldResult.Error = fmt.Sprintf("failed to select option '%s' (no matching option found)", fieldValue)
result.ErrorCount++
} else {
fieldResult.Success = true