xpzouying/xiaohongshu-mcp · error

查找可见范围选项失败

Error message

查找可见范围选项失败

What it means

setVisibility wraps failure from page.Elements("div.d-options-wrapper div.d-grid-item div.custom-option") when listing the visibility options in the opened dropdown popup. The dropdown click succeeded but enumerating the option items failed at the CDP level.

Source

Thrown at xiaohongshu/publish.go:900

	supported := map[string]bool{"仅自己可见": true, "仅互关好友可见": true}
	if !supported[visibility] {
		return errors.Errorf("不支持的可见范围: %s,支持: 公开可见、仅自己可见、仅互关好友可见", visibility)
	}

	dropdown, err := page.Element("div.permission-card-wrapper div.d-select-content")
	if err != nil {
		return errors.Wrap(err, "查找可见范围下拉框失败")
	}
	if err := humanize.Click(dropdown); err != nil {
		return errors.Wrap(err, "点击可见范围下拉框失败")
	}
	time.Sleep(500 * time.Millisecond)

	// 在弹窗中查找并点击目标选项
	opts, err := page.Elements("div.d-options-wrapper div.d-grid-item div.custom-option")
	if err != nil {
		return errors.Wrap(err, "查找可见范围选项失败")
	}
	for _, opt := range opts {
		text, err := opt.Text()
		if err != nil {
			continue
		}
		if strings.Contains(text, visibility) {
			if err := humanize.Click(opt); err != nil {
				return errors.Wrap(err, "选择可见范围失败")
			}
			slog.Info("已设置可见范围", "visibility", visibility)
			time.Sleep(200 * time.Millisecond)
			return nil
		}
	}
	return errors.Errorf("未找到可见范围选项: %s", visibility)
}

View on GitHub (pinned to 332d196854)

Solutions

  1. Wait explicitly for div.d-options-wrapper to appear after clicking instead of a fixed 500ms sleep.
  2. Inspect current DOM and refresh selectors after UI updates.
  3. Retry the Elements query after a short delay (popup may still be animating in).
  4. Ensure the click on the dropdown actually opened the popup before querying.

Example fix

// before
time.Sleep(500 * time.Millisecond)
opts, err := page.Elements("div.d-options-wrapper div.d-grid-item div.custom-option")
// after
time.Sleep(500 * time.Millisecond)
if _, err := page.Element("div.d-options-wrapper"); err != nil {
    time.Sleep(1 * time.Second) // 弹窗可能尚未渲染
}
opts, err := page.Elements("div.d-options-wrapper div.d-grid-item div.custom-option")
Defensive patterns

Strategy: retry

Validate before calling

if _, err := page.Timeout(3*time.Second).Element("div.d-options-wrapper"); err != nil { return errors.New("选项弹窗未出现") }

Type guard

func optionsPopupOpen(p *rod.Page) bool { _, err := p.Element("div.d-options-wrapper div.custom-option"); return err == nil }

Try / catch

opts, err := listVisibilityOptions(page) // 内部封装 Elements 查询
if err != nil {
    time.Sleep(1 * time.Second) // 弹窗动画后再试
    opts, err = listVisibilityOptions(page)
}

Prevention

When it happens

Trigger: Options popup renders in a different container than expected (UI update), page navigated between click and query, or rod Elements query raced with popup animation.

Common situations: Creator UI redesign renaming d-options-wrapper/d-grid-item/custom-option; popup rendered lazily after the 500ms wait was too short; remote browser flakiness.

Related errors


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