xpzouying/xiaohongshu-mcp · error

输入日期时间失败

Error message

输入日期时间失败

What it means

Thrown by setDateTime when humanize.Type fails to type the formatted datetime string into the input. Humanized typing simulates keystrokes; it fails if the element detaches mid-typing, focus is stolen, ctx is cancelled, or the page closes.

Source

Thrown at xiaohongshu/publish.go:965

	return nil
}

// setDateTime 设置日期时间
func setDateTime(ctx context.Context, page *rod.Page, t time.Time) error {
	dateTimeStr := t.Format("2006-01-02 15:04")

	elem, err := page.Element(".date-picker-container input")
	if err != nil {
		return errors.Wrap(err, "查找日期时间输入框失败")
	}

	// SelectAllText 走 Eval(this.select()),只改选区、不额外派发事件,暂时保留。
	// 换成键盘全选要区分 Ctrl/Cmd,换成三击又可能触发日期控件的其他行为。
	if err := elem.SelectAllText(); err != nil {
		return errors.Wrap(err, "选择日期时间文本失败")
	}
	if err := humanize.Type(ctx, elem, dateTimeStr); err != nil {
		return errors.Wrap(err, "输入日期时间失败")
	}
	slog.Info("已设置日期时间", "datetime", dateTimeStr)

	return nil
}

// setOriginal 设置原创声明
func setOriginal(page *rod.Page) error {
	// 根据小红书创作者页面的实际结构:
	// div.custom-switch-card 包含 span.has-tips 文本为"原创声明"
	// 开关是 div.d-switch 组件

	// 查找包含"原创声明"文本的 custom-switch-card
	switchCards, err := page.Elements("div.custom-switch-card")
	if err != nil {
		return errors.Wrap(err, "查找原创声明卡片失败")
	}

View on GitHub (pinned to 332d196854)

Solutions

  1. Re-acquire the element and retry the full SelectAllText + Type sequence once.
  2. Ensure ctx has an adequate deadline for the typing duration.
  3. Dismiss any popup calendar overlaying the input before typing.
  4. Verify the input still accepts keyboard events on the live XHS page; update the typing strategy if the component changed.

Example fix

// before
if err := humanize.Type(ctx, elem, dateTimeStr); err != nil {
	return errors.Wrap(err, "输入日期时间失败")
}
// after
if err := humanize.Type(ctx, elem, dateTimeStr); err != nil {
	elem, err = page.Element(".date-picker-container input")
	if err != nil {
		return errors.Wrap(err, "查找日期时间输入框失败")
	}
	_ = elem.SelectAllText()
	if err := humanize.Type(ctx, elem, dateTimeStr); err != nil {
		return errors.Wrap(err, "输入日期时间失败")
	}
}
Defensive patterns

Strategy: try-catch

Validate before calling

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

Try / catch

if err := setSchedulePublish(ctx, page, t); err != nil {
	if errors.Is(err, context.DeadlineExceeded) || strings.Contains(err.Error(), "输入日期时间失败") {
		slog.Warn("datetime typing failed, retrying once")
		return setSchedulePublish(ctx, page, t)
	}
	return err
}

Prevention

When it happens

Trigger: A popup calendar steals focus mid-typing; the picker re-renders as characters are typed; ctx deadline expired; the input rejects synthetic key events after an XHS front-end change.

Common situations: Slow environments where typing outlasts a re-render; automation runs with a short-lived context; XHS input validation closing the picker on invalid intermediate values.

Related errors


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