xpzouying/xiaohongshu-mcp · error
查找定时发布开关失败
Error message
查找定时发布开关失败
What it means
Thrown by clickScheduleSwitch when page.Element(".post-time-wrapper .d-switch") fails to locate the schedule-publish toggle on the Xiaohongshu creator upload page. rod's Element() waits up to its default timeout for the selector before erroring, so the element genuinely did not render in time or the page DOM no longer matches this selector.
Source
Thrown at xiaohongshu/publish.go:940
if err := clickScheduleSwitch(page); err != nil {
return err
}
time.Sleep(800 * time.Millisecond)
// 2. 设置日期时间
if err := setDateTime(ctx, page, t); err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
return nil
}
// clickScheduleSwitch 点击定时发布开关
func clickScheduleSwitch(page *rod.Page) error {
switchElem, err := page.Element(".post-time-wrapper .d-switch")
if err != nil {
return errors.Wrap(err, "查找定时发布开关失败")
}
if err := humanize.Click(switchElem); err != nil {
return errors.Wrap(err, "点击定时发布开关失败")
}
slog.Info("已点击定时发布开关")
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, "查找日期时间输入框失败")
}
View on GitHub (pinned to 332d196854)
Solutions
- Wait for the schedule section to render before calling setSchedulePublish (Wait on .post-time-wrapper).
- Log page.HTML() on failure and verify .post-time-wrapper .d-switch still exists on the live XHS page; update the selector if XHS changed it.
- Dismiss any login/verification popups before this step.
- Increase the selector timeout with page.Timeout(...) if renders are consistently slow.
Example fix
// before
switchElem, err := page.Element(".post-time-wrapper .d-switch")
// after
if err := page.Wait("() => !!document.querySelector('.post-time-wrapper')"); err != nil {
return errors.Wrap(err, "定时发布区域未渲染")
}
switchElem, err := page.Element(".post-time-wrapper .d-switch") Defensive patterns
Strategy: validation
Validate before calling
if err := page.Wait("() => !!document.querySelector('.post-time-wrapper .d-switch')"); err != nil {
return fmt.Errorf("定时发布开关未渲染: %w", err)
} Try / catch
if err := setSchedulePublish(ctx, page, t); err != nil {
if strings.Contains(err.Error(), "查找定时发布开关失败") {
html, _ := page.HTML()
slog.Warn("schedule switch missing", "htmlLen", len(html))
// 重试或跳过定时设置
}
return err
} Prevention
- Wait for .post-time-wrapper to exist before schedule steps rather than relying on fixed sleeps.
- Log page HTML when selectors fail to catch XHS markup changes quickly.
- Keep selector constants centralized with a last-verified-date comment.
- Handle login/verification popups before the publish flow.
When it happens
Trigger: Calling setSchedulePublish when the publish flow is not at the settings stage, the upload page failed to fully load, XHS front-end updated the .post-time-wrapper markup, or the page navigated/closed mid-flow.
Common situations: Slow network on the creator page, a login or verification popup blocking the flow, XHS redesign changing class names, or running setSchedulePublish before content upload finished so the schedule section never rendered.
Related errors
AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05).
Data as JSON: /api/errors/b06ecaa650e75977.
Report an issue: GitHub.