This commit is contained in:
Josh at WLTechBlog 2025-08-19 10:06:40 -05:00
parent 8fc4f76b28
commit 63860db70b
1 changed files with 19 additions and 60 deletions

View File

@ -3698,68 +3698,27 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
Success: false,
}
// Try different selector strategies for the field
// Try different selector strategies for the field (fast, no individual timeouts)
var element *rod.Element
var selectors []string
// If we have a form, search within it first
if form != nil {
selectors = []string{
fmt.Sprintf("[name='%s']", fieldName),
fmt.Sprintf("#%s", fieldName),
fmt.Sprintf("[id='%s']", fieldName),
fieldName, // In case it's already a full selector
}
for _, selector := range selectors {
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
element, err = form.Context(ctx).Element(selector)
if err == nil {
fieldResult.Selector = selector
cancel() // Cancel context now that we found the element
break
}
cancel() // Cancel if element not found
} else {
element, err = form.Element(selector)
if err == nil {
fieldResult.Selector = selector
break
}
}
}
selectors := []string{
fmt.Sprintf("[name='%s']", fieldName),
fmt.Sprintf("#%s", fieldName),
fmt.Sprintf("[id='%s']", fieldName),
fieldName, // In case it's already a full selector
}
// If not found in form or no form, search in entire page
if element == nil {
// Generate selectors if not already done
if selectors == nil {
selectors = []string{
fmt.Sprintf("[name='%s']", fieldName),
fmt.Sprintf("#%s", fieldName),
fmt.Sprintf("[id='%s']", fieldName),
fieldName, // In case it's already a full selector
}
// Search for element (try form first if available, then page)
for _, selector := range selectors {
// Try without timeout first (should be instant if element exists)
if form != nil {
element, err = form.Element(selector)
} else {
element, err = page.Element(selector)
}
for _, selector := range selectors {
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
element, err = page.Context(ctx).Element(selector)
if err == nil {
fieldResult.Selector = selector
cancel() // Cancel context now that we found the element
break
}
cancel() // Cancel if element not found
} else {
element, err = page.Element(selector)
if err == nil {
fieldResult.Selector = selector
break
}
}
if err == nil {
fieldResult.Selector = selector
break
}
}
@ -3770,8 +3729,8 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
continue
}
// Determine the element type and use appropriate action
tagName, err := element.Eval("() => this.tagName.toLowerCase()")
// Determine the element type using rod's built-in method (much faster than Eval)
tagName, err := element.Property("tagName")
if err != nil {
fieldResult.Error = fmt.Sprintf("failed to get element tag name: %v", err)
result.FilledFields = append(result.FilledFields, fieldResult)
@ -3780,7 +3739,7 @@ func (d *Daemon) fillFormBulk(tabID, formSelector, fieldsJSON string, timeout in
}
// Handle different element types
if tagName.Value.String() == "select" {
if strings.ToLower(tagName.Str()) == "select" {
// Use select action for select elements
fieldResult.Action = "select"
err = element.Select([]string{fieldValue}, true, rod.SelectorTypeText)