bump
This commit is contained in:
parent
a6489cd5d0
commit
1e142c1f3f
115
daemon/daemon.go
115
daemon/daemon.go
|
@ -1966,7 +1966,31 @@ func (d *Daemon) selectElement(tabID, selector, value string, selectionTimeout,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If text selection failed, use JavaScript as fallback
|
// If text selection failed, use JavaScript as fallback
|
||||||
// This handles both option value and option text selection
|
// 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
|
// Execute JavaScript and get the result
|
||||||
result, err := page.Eval(script)
|
result, err := page.Eval(script)
|
||||||
|
@ -1975,8 +1999,8 @@ func (d *Daemon) selectElement(tabID, selector, value string, selectionTimeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if JavaScript selection worked
|
// Check if JavaScript selection worked
|
||||||
if result.Value.String() != value {
|
if result.Value.Nil() {
|
||||||
return fmt.Errorf("failed to select option '%s' in element (JavaScript fallback failed)", value)
|
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)
|
// Try to select by text first (most common case)
|
||||||
err = element.Select([]string{interaction.Value}, true, rod.SelectorTypeText)
|
err = element.Select([]string{interaction.Value}, true, rod.SelectorTypeText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If text selection failed, the value might be the actual option value
|
// If text selection failed, use JavaScript as fallback
|
||||||
// Try to find and select by matching option value using page.Eval
|
// 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 })); } })()`, interaction.Selector, interaction.Value)
|
script := fmt.Sprintf(`
|
||||||
// Execute JavaScript and ignore any rod evaluation quirks
|
(function() {
|
||||||
page.Eval(script)
|
var select = document.querySelector("%s");
|
||||||
|
if (!select) return null;
|
||||||
|
|
||||||
// Always verify success by checking the element's actual value
|
// First try to select by value
|
||||||
currentValue, err := element.Property("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 {
|
if err != nil {
|
||||||
interactionResult.Error = fmt.Sprintf("failed to verify selection: %v", err)
|
interactionResult.Error = fmt.Sprintf("failed to execute JavaScript selection: %v", err)
|
||||||
} else if currentValue.Str() == interaction.Value {
|
} else if result.Value.Nil() {
|
||||||
interactionResult.Success = true
|
interactionResult.Error = fmt.Sprintf("failed to select option '%s' (no matching option found)", interaction.Value)
|
||||||
} else {
|
} else {
|
||||||
interactionResult.Error = fmt.Sprintf("failed to select option: %s (current value: %s)", interaction.Value, currentValue.Str())
|
interactionResult.Success = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
interactionResult.Success = true
|
interactionResult.Success = true
|
||||||
|
@ -3775,14 +3821,41 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
|
||||||
fieldResult.Action = "select"
|
fieldResult.Action = "select"
|
||||||
err = element.Select([]string{fieldValue}, true, rod.SelectorTypeText)
|
err = element.Select([]string{fieldValue}, true, rod.SelectorTypeText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If text selection failed, try by value
|
// If text selection failed, use JavaScript as fallback
|
||||||
script := fmt.Sprintf(`(function(){ var el = document.querySelector("%s"); if(el) { el.value = "%s"; el.dispatchEvent(new Event('change', { bubbles: true })); } })()`, fieldResult.Selector, fieldValue)
|
// This handles both option value and option text selection
|
||||||
page.Eval(script)
|
script := fmt.Sprintf(`
|
||||||
|
(function() {
|
||||||
|
var select = document.querySelector("%s");
|
||||||
|
if (!select) return null;
|
||||||
|
|
||||||
// Verify the selection worked
|
// First try to select by value
|
||||||
actualValue, err := element.Attribute("value")
|
select.value = "%s";
|
||||||
if err != nil || actualValue == nil || *actualValue != fieldValue {
|
if (select.value === "%s") {
|
||||||
fieldResult.Error = fmt.Sprintf("failed to select option '%s'", fieldValue)
|
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++
|
result.ErrorCount++
|
||||||
} else {
|
} else {
|
||||||
fieldResult.Success = true
|
fieldResult.Success = true
|
||||||
|
|
Loading…
Reference in New Issue