{"record":{"id":"da73a9e0b08154fb","repo":"xpzouying/xiaohongshu-mcp","slug":"error-da73a9","errorCode":null,"errorMessage":"触发搜索失败","messagePattern":"触发搜索失败","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"xiaohongshu/publish.go","lineNumber":1280,"sourceCode":"\tif err != nil {\n\t\treturn errors.Wrap(err, \"未找到商品搜索框\")\n\t}\n\n\t// 2. 清空并输入关键词。SelectAllText 走 Eval(this.select())，只改选区、不额外\n\t// 派发事件，暂时保留（换键盘全选要区分 Ctrl/Cmd）。\n\tif err := searchInput.SelectAllText(); err != nil {\n\t\tslog.Warn(\"选择搜索框文本失败\", \"error\", err)\n\t}\n\ttime.Sleep(100 * time.Millisecond)\n\n\tif err := humanize.Type(ctx, searchInput, keyword); err != nil {\n\t\treturn errors.Wrap(err, \"输入搜索关键词失败\")\n\t}\n\ttime.Sleep(300 * time.Millisecond)\n\n\t// 3. 触发搜索（模拟键盘 Enter）\n\tif err := page.Keyboard.Press(input.Enter); err != nil {\n\t\treturn errors.Wrap(err, \"触发搜索失败\")\n\t}\n\n\t// 4. 等待搜索结果加载\n\ttime.Sleep(1 * time.Second)\n\n\t// 等待 loading 消失（使用与工作代码相同的选择器）\n\tdeadline := time.Now().Add(10 * time.Second)\n\tfor time.Now().Before(deadline) {\n\t\tloading, err := modal.Element(\".goods-list-loading\")\n\t\tif err != nil || loading == nil {\n\t\t\tbreak\n\t\t}\n\t\tvisible, _ := loading.Visible()\n\t\tif !visible {\n\t\t\tbreak\n\t\t}\n\t\ttime.Sleep(100 * time.Millisecond)\n\t}","sourceCodeStart":1262,"sourceCodeEnd":1298,"githubUrl":"https://github.com/xpzouying/xiaohongshu-mcp/blob/332d196854a9eac0d2b8c2c0e3d0cc43139d724c/xiaohongshu/publish.go#L1262-L1298","documentation":"searchAndSelectProduct 输入关键词后用 page.Keyboard.Press(input.Enter) 触发搜索失败：键盘事件派发失败（页面焦点异常或 CDP 输入被拒绝），搜索未执行。","triggerScenarios":"page.Keyboard.Press 返回非 nil：页面/浏览器连接异常、CDP Input.dispatchKeyEvent 失败、页面正在跳转或 target 已关闭。","commonSituations":"浏览器实例被关闭或 tab 崩溃；页面导航中断开 CDP 会话；输入后前端立即重渲染导致页面 context 异常；自动化长时间运行后连接失效。","solutions":["展开 wrapped error 确认是否连接/会话级问题，若是则重建浏览器会话而非重试按键","输入后先确认焦点仍在搜索框（可重新 focus），再按 Enter","改用更稳健的触发方式：searchInput.Press(input.Enter)（元素级）或点击搜索按钮","增加页面健康检查：按 Enter 前 page.MustWaitStable / 检查 target 存活"],"exampleFix":"// before\nif err := page.Keyboard.Press(input.Enter); err != nil {\n    return errors.Wrap(err, \"触发搜索失败\")\n}\n// after\nif err := searchInput.Press(input.Enter); err != nil {\n    return errors.Wrap(err, \"触发搜索失败\")\n}","handlingStrategy":"try-catch","validationCode":"if err := page.AssertExists(\"body\"); err != nil {\n    return errors.New(\"页面不可用，无法触发搜索\")\n}","typeGuard":null,"tryCatchPattern":"if err := page.Keyboard.Press(input.Enter); err != nil {\n    if errors.Is(err, context.Canceled) || strings.Contains(err.Error(), \"target closed\") {\n        return fmt.Errorf(\"页面会话已失效，需重建浏览器: %w\", err)\n    }\n    return errors.Wrap(err, \"触发搜索失败\")\n}","preventionTips":["优先使用元素级 Press（searchInput.Press）而非 page 级键盘","确保输入后焦点仍在搜索框","长任务中检测浏览器/页面连接健康状态","按键后校验搜索结果出现作为成功判据","对 target closed 类错误走重建会话路径而非重试"],"tags":["automation","rod","keyboard","cdp"],"backgroundTag":"keyboard-input-failed","analyzedSha":"332d196854a9eac0d2b8c2c0e3d0cc43139d724c","analyzedAt":"2026-09-05T22:22:55.988Z","contentChangedAt":"2026-09-05T22:22:55.988Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}