xpzouying/xiaohongshu-mcp · error

输入#失败

Error message

输入#失败

What it means

inputTag wraps any failure from humanize.Type when typing the '#' character into the content editor element. The '#' keystroke is required to trigger Xiaohongshu's topic-autocomplete dropdown; if the underlying browser automation typing fails, the error is wrapped as 输入#失败. It means the browser could not reliably send the '#' key to the content <p> element.

Source

Thrown at xiaohongshu/publish.go:727

	if err := ka.Press(input.Enter).Press(input.Enter).Do(); err != nil {
		return errors.Wrap(err, "按下回车键失败")
	}

	time.Sleep(1 * time.Second)

	for _, tag := range tags {
		tag = strings.TrimLeft(tag, "#")
		if err := inputTag(ctx, contentElem, tag); err != nil {
			return errors.Wrapf(err, "输入标签[%s]失败", tag)
		}
	}
	return nil
}

func inputTag(ctx context.Context, contentElem *rod.Element, tag string) error {
	// 输入 # 触发话题联想
	if err := humanize.Type(ctx, contentElem, "#"); err != nil {
		return errors.Wrap(err, "输入#失败")
	}
	time.Sleep(200 * time.Millisecond) // 技术等待:等联想下拉框弹出

	if err := humanize.Type(ctx, contentElem, tag); err != nil {
		return errors.Wrap(err, "输入标签内容失败")
	}

	time.Sleep(1 * time.Second) // 技术等待:等联想结果刷新

	page := contentElem.Page()
	topicContainer, err := page.Element("#creator-editor-topic-container")
	if err != nil || topicContainer == nil {
		slog.Warn("未找到标签联想下拉框,直接输入空格", "tag", tag)
		return humanize.Type(ctx, contentElem, " ")
	}

	firstItem, err := topicContainer.Element(".item")
	if err != nil || firstItem == nil {

View on GitHub (pinned to 332d196854)

Solutions

  1. Ensure the publish page and editor are fully loaded before calling inputTags (wait for the content element to be visible/stable).
  2. Re-acquire the content element right before typing instead of reusing a cached *rod.Element.
  3. Increase the rod/browser CDP timeout or check network stability to the browser endpoint.
  4. Retry the tag input once after a short sleep; the UI may have been momentarily busy.

Example fix

// before
tagErr := inputTags(ctx, contentElem, tags)
// after
if err := contentElem.WaitStable(300 * time.Millisecond); err != nil {
    return errors.Wrap(err, "content element not stable")
}
tagErr := inputTags(ctx, contentElem, tags)
Defensive patterns

Strategy: retry

Validate before calling

if err := contentElem.WaitStable(300 * time.Millisecond); err != nil { return errors.Wrap(err, "编辑器不可用") }

Type guard

func isTypingErr(err error) bool { return err != nil && strings.Contains(err.Error(), "输入#失败") }

Try / catch

for i := 0; i < 2; i++ {
    if err := inputTag(ctx, contentElem, tag); err == nil { break }
    time.Sleep(500 * time.Millisecond)
}

Prevention

When it happens

Trigger: Calling inputTags with a tag list when the content element has become stale/detached, the page navigated away, the editor lost focus, or the rod browser connection dropped during humanize.Type of '#'.

Common situations: Slow page load so the editor isn't ready when tags are typed; page re-render replaces the content element mid-typing; browser tab crashed or was closed; network latency to a remote CDP browser exceeding rod's timeouts.

Related errors


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