xpzouying/xiaohongshu-mcp · error

切换到 %s 失败: %w

Error message

切换到 %s 失败: %w

What it means

selectTab found the sub-tab matching the requested label, but humanize.Click on the tab element failed (xiaohongshu/user_profile.go:188). The message names the target tab label (e.g. 喜欢/收藏). The click is wrapped with humanize delays, so failure usually comes from rod's click mechanics (element detached, obscured, or page navigating).

Source

Thrown at xiaohongshu/user_profile.go:188

func (u *UserProfileAction) selectTab(ctx context.Context, page *rod.Page, tab ProfileTab) error {
	if tab == "" || tab == TabNotes {
		return nil
	}

	label := tabLabel[tab]
	elems, err := page.Elements(`.reds-tab-item.sub-tab-list`)
	if err != nil {
		return fmt.Errorf("未找到主页子 tab: %w", err)
	}

	for _, elem := range elems {
		text, err := elem.Text()
		if err != nil || strings.TrimSpace(text) != label {
			continue
		}
		humanize.Delay(ctx, humanize.BeforeClick)
		if err := humanize.Click(elem); err != nil {
			return fmt.Errorf("切换到 %s 失败: %w", label, err)
		}
		humanize.Delay(ctx, humanize.AfterClick)
		page.MustWaitStable()
		return nil
	}
	return fmt.Errorf("未找到子 tab %q", label)
}

View on GitHub (pinned to 332d196854)

Solutions

  1. Retry the whole GetMyProfileViaSidebar call; transient re-renders usually succeed on a second pass.
  2. Verify no login wall or verification dialog is blocking the page.
  3. Wait longer after navigation (increase stability wait) before selecting the tab.
  4. If persistent, re-check the tab element selector against current XHS markup.

Example fix

// before
resp, err := action.GetMyProfileViaSidebar(ctx, tab)
// after
var resp *xiaohongshu.UserProfileResponse
err := retry.Do(3, func() error {
    r, e := action.GetMyProfileViaSidebar(ctx, tab)
    resp, err = r, e
    return e
})
Defensive patterns

Strategy: retry

Try / catch

var resp *UserProfileResponse
err := retry.Do(3, func() error {
    r, e := action.GetMyProfileViaSidebar(ctx, tab)
    resp = r
    return e
})

Prevention

When it happens

Trigger: Calling GetMyProfileViaSidebar(ctx, tab) with a non-default tab where the matching `.reds-tab-item` element becomes detached or unclickable between listing and clicking — a re-render replaced the node, an overlay intercepts the click, or the page navigated.

Common situations: XHS re-rendered the tab bar right before the click; anti-bot overlay/interstitial on top; slow page causing staleness; browser tab closed by user or timeout.

Related errors


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