fix: Add debug output and fallback selectors for input field detection

This commit is contained in:
王性驊 2026-04-03 01:11:02 +08:00
parent 32673c028e
commit 17b001d8c2
1 changed files with 53 additions and 9 deletions

View File

@ -55,38 +55,82 @@ func TypeInput(page *rod.Page, text string) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel() defer cancel()
// 1. 尋找輸入框 fmt.Println("[GeminiWeb] Looking for input field...")
// 1. 嘗試所有選擇器
var inputEl *rod.Element var inputEl *rod.Element
var err error var err error
for _, sel := range inputSelectors { for _, sel := range inputSelectors {
fmt.Printf(" Trying: %s\n", sel)
inputEl, err = page.Context(ctx).Element(sel) inputEl, err = page.Context(ctx).Element(sel)
if err == nil { if err == nil {
fmt.Printf(" ✓ Found with: %s\n", sel)
break break
} }
} }
if err != nil { if err != nil {
return fmt.Errorf("input field not found") // 2. Fallback: 嘗試等待頁面載入完成後重試
fmt.Println("[GeminiWeb] Waiting for page to fully load...")
time.Sleep(3 * time.Second)
for _, sel := range inputSelectors {
fmt.Printf(" Retrying: %s\n", sel)
inputEl, err = page.Context(ctx).Element(sel)
if err == nil {
fmt.Printf(" ✓ Found with: %s\n", sel)
break
}
}
}
if err != nil {
// 3. Debug: 印出頁面標題和 URL
info, _ := page.Info()
fmt.Printf("[GeminiWeb] DEBUG: URL=%s Title=%s\n", info.URL, info.Title)
// 4. Fallback: 嘗試更通用的選擇器
fmt.Println("[GeminiWeb] Trying generic selectors...")
genericSelectors := []string{
"div[contenteditable]",
"[contenteditable]",
"textarea",
"input[type='text']",
}
for _, sel := range genericSelectors {
fmt.Printf(" Trying generic: %s\n", sel)
inputEl, err = page.Context(ctx).Element(sel)
if err == nil {
fmt.Printf(" ✓ Found with: %s\n", sel)
break
}
}
}
if err != nil {
info, _ := page.Info()
return fmt.Errorf("input field not found after trying all selectors (URL=%s)", info.URL)
} }
// 2. Focus 輸入框 // 2. Focus 輸入框
fmt.Printf("[GeminiWeb] Focusing input field...\n")
if err := inputEl.Focus(); err != nil { if err := inputEl.Focus(); err != nil {
return fmt.Errorf("failed to focus input: %w", err) return fmt.Errorf("failed to focus input: %w", err)
} }
time.Sleep(100 * time.Millisecond) time.Sleep(500 * time.Millisecond)
// 3. 使用 Input 方法Rod 的正確方式) // 3. 使用 Input 方法
fmt.Printf("[GeminiWeb] Typing %d chars...\n", len(text))
if err := inputEl.Input(text); err != nil { if err := inputEl.Input(text); err != nil {
return fmt.Errorf("failed to input text: %w", err) return fmt.Errorf("failed to input text: %w", err)
} }
time.Sleep(100 * time.Millisecond) time.Sleep(200 * time.Millisecond)
// 4. 觸發 Enter 觸發事件
_ = inputEl.SelectAllText()
_ = page.Keyboard.Press('\r') // Enter key
fmt.Println("[GeminiWeb] Input complete")
return nil return nil
} }