xpzouying/xiaohongshu-mcp · error

无法输入评论内容: %w

Error message

无法输入评论内容: %w

What it means

PostComment 在点击评论输入框并定位到 p.content-input 后,humanize.Type 模拟人工输入评论内容失败:元素在输入过程中失焦、被重渲染,或页面拦截了键盘事件。

Source

Thrown at xiaohongshu/comment_feed.go:61

		logrus.Warnf("Failed to find comment input box: %v", err)
		return fmt.Errorf("未找到评论输入框,该帖子可能不支持评论或网页端不可访问: %w", err)
	}

	if err := humanize.Click(elem); err != nil {
		logrus.Warnf("Failed to click comment input box: %v", err)
		return fmt.Errorf("无法点击评论输入框: %w", err)
	}
	humanize.Delay(ctx, humanize.AfterClick)

	elem2, err := page.Element("div.input-box div.content-edit p.content-input")
	if err != nil {
		logrus.Warnf("Failed to find comment input field: %v", err)
		return fmt.Errorf("未找到评论输入区域: %w", err)
	}

	if err := humanize.Type(ctx, elem2, content); err != nil {
		logrus.Warnf("Failed to input comment content: %v", err)
		return fmt.Errorf("无法输入评论内容: %w", err)
	}

	humanize.Delay(ctx, humanize.AfterType)

	submitButton, err := page.Element("div.bottom button.submit")
	if err != nil {
		logrus.Warnf("Failed to find submit button: %v", err)
		return fmt.Errorf("未找到提交按钮: %w", err)
	}

	if err := humanize.Click(submitButton); err != nil {
		logrus.Warnf("Failed to click submit button: %v", err)
		return fmt.Errorf("无法点击提交按钮: %w", err)
	}

	humanize.Delay(ctx, humanize.AfterClick)

	// 就地校验:提交后评论应在评论区渲染出现;未出现则判定失败,避免假成功。

View on GitHub (pinned to 332d196854)

Solutions

  1. Retry the operation; transient focus loss often succeeds on a second run
  2. Shorten the comment or strip unusual characters/emoji
  3. Slow down typing / increase humanize delays for slow environments
  4. Verify the account is not restricted (restricted accounts can open but not type/submit)

Example fix

// before
content := strings.Repeat("好", 500) // overly long
clickErr := svc.PostComment(ctx, feedID, content)
// after
content := "非常好,支持!"
err := svc.PostComment(ctx, feedID, content)
if err != nil { /* retry once with same content */ }
Defensive patterns

Strategy: retry

Validate before calling

if utf8.RuneCountInString(content) > 200 || strings.Contains(content, "http") {
    return errors.New("评论内容过长或含链接,易触发输入/过滤问题")
}

Try / catch

var err error
for i := 0; i < 2; i++ {
    if err = svc.PostComment(ctx, feedID, content); err == nil || !strings.Contains(err.Error(), "无法输入评论内容") {
        break
    }
    time.Sleep(2 * time.Second)
}

Prevention

When it happens

Trigger: humanize.Type fails because the editor element was replaced mid-typing, an overlay stole focus, or content triggered client-side length/format guards that clear input.

Common situations: Very long comments or ones with special characters/emoji the editor handles oddly; page rerendered by XHS during typing (e.g. suggestions popup); slow environment where keystroke timing outpaces the page.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/34ecfb9e63433045. Report an issue: GitHub.