xpzouying/xiaohongshu-mcp · error

输入标签[%s]失败

Error message

输入标签[%s]失败

What it means

This error wraps any failure from inputTag for a specific hashtag: it names the tag in the message and propagates the underlying cause (typing '#' failed, typing tag text failed, or selection confirmation failed). inputTags stops at the first failing tag, so the publish's tag-adding step aborts with this contextual error.

Source

Thrown at xiaohongshu/publish.go:718

			return errors.Wrap(err, "按下方向键失败")
		}
		time.Sleep(10 * time.Millisecond)
	}

	ka, err := contentElem.KeyActions()
	if err != nil {
		return errors.Wrap(err, "创建键盘操作失败")
	}
	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) // 技术等待:等联想结果刷新

View on GitHub (pinned to 332d196854)

Solutions

  1. Inspect the wrapped cause and the tag name in the message; remove or reformat problematic tags (avoid spaces/special characters, strip leading '#').
  2. Increase waits before reading the suggestion container if the network is slow.
  3. Verify the topic-container selector (#creator-editor-topic-container) still matches the live site DOM.
  4. Retry the publish; transient dropdown-rendering failures often succeed on retry.

Example fix

// before
tags := []string{"travel diary", "#food", "美食/探店"} // problematic tags
err := publisher.Publish(ctx, opts)
// after
tags := []string{"旅行日记", "美食探店"} // clean tags; leading # stripped and invalid chars removed
for i := range tags {
    tags[i] = strings.TrimLeft(strings.TrimSpace(tags[i]), "#")
}
err := publisher.Publish(ctx, opts)
Defensive patterns

Strategy: validation

Validate before calling

// sanitize tags before publish: trim, strip '#', reject unsafe chars
var clean []string
for _, t := range tags {
    t = strings.TrimSpace(strings.TrimLeft(t, "#"))
    if t == "" || strings.ContainsAny(t, " /\\,;") {
        continue
    }
    clean = append(clean, t)
}
opts.Tags = clean

Type guard

func validTag(tag string) bool {
    tag = strings.TrimLeft(strings.TrimSpace(tag), "#")
    return tag != "" && !strings.ContainsFunc(tag, func(r rune) bool {
        return r == ' ' || r == '/' || r == ','
    })
}

Try / catch

err := publisher.Publish(ctx, opts)
if err != nil && strings.Contains(err.Error(), "输入标签[") {
    tag := extractTag(err.Error())
    log.Printf("标签 %q 输入失败,去掉该标签重试", tag)
    opts.Tags = removeTag(opts.Tags, tag)
    err = publisher.Publish(ctx, opts)
}

Prevention

When it happens

Trigger: submitPublish/submitPublishVideo pass a tags list to inputTags; for tag N, inputTag fails — e.g. typing failed, the topic suggestion container (#creator-editor-topic-container) never rendered and even the space fallback failed, or the suggestion item couldn't be clicked.

Common situations: Tag contains characters the site's suggestion box rejects (spaces, special symbols); slow network so the suggestion dropdown doesn't appear in time; site DOM change removed the topic container; caret left the editor so typing went nowhere.

Related errors


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