xpzouying/xiaohongshu-mcp · error

获取新版发布按钮位置失败

Error message

获取新版发布按钮位置失败

What it means

clickPublishWidget wraps the error from rod's Element.Shape, which fetches the button's geometry (content quads) via CDP. It fails when the element cannot provide a shape — typically because it is detached/hidden or the CDP session is broken. Note the same message also appears as a plain errors.New when Shape succeeds but returns zero quads (no clickable area).

Source

Thrown at xiaohongshu/publish.go:553

		} else if cls != nil && hasExactClass(*cls, "disabled") {
			return &publishButton{elem: oldButton}, "旧版发布按钮包含 disabled class", nil
		}

		return &publishButton{elem: oldButton}, "", nil
	}

	return nil, "", nil
}

func clickPublishWidget(page *rod.Page, widget *rod.Element) error {
	if err := widget.ScrollIntoView(); err != nil {
		return errors.Wrap(err, "滚动新版发布按钮到可视区域失败")
	}
	time.Sleep(200 * time.Millisecond)

	shape, err := widget.Shape()
	if err != nil {
		return errors.Wrap(err, "获取新版发布按钮位置失败")
	}
	if len(shape.Quads) == 0 {
		return errors.New("获取新版发布按钮位置失败: 无可点击区域")
	}

	quad := shape.Quads[0]
	minX, maxX := quad[0], quad[0]
	minY, maxY := quad[1], quad[1]
	for i := 0; i < quad.Len(); i++ {
		x := quad[i*2]
		y := quad[i*2+1]
		if x < minX {
			minX = x
		}
		if x > maxX {
			maxX = x
		}
		if y < minY {

View on GitHub (pinned to 332d196854)

Solutions

  1. Retry with a fresh element: re-run find + click instead of reusing a stale rod.Element handle
  2. Add a short stability wait before clicking so the widget finishes layout (WaitStable / WaitDOMStable)
  3. Check element visibility before clicking (isElementVisible equivalent) to skip zero-size/detached widgets
  4. Verify the browser connection is alive; re-launch Chrome and reopen the publish page if the session died

Example fix

// before
shape, err := widget.Shape()
if err != nil {
    return errors.Wrap(err, "获取新版发布按钮位置失败")
}
// after
if err := page.WaitStable(300 * time.Millisecond); err != nil {
    return err
}
shape, err := widget.Shape()
if err != nil {
    return errors.Wrap(err, "获取新版发布按钮位置失败")
}
Defensive patterns

Strategy: retry

Validate before calling

if !isElementVisible(widget) { return errors.New("新版发布按钮不可见") }
shape, err := widget.Shape()
if err != nil || len(shape.Quads) == 0 { return errors.New("新版发布按钮无有效形状") }

Type guard

func hasQuads(e *rod.Element) bool {
    shape, err := e.Shape()
    return err == nil && len(shape.Quads) > 0
}

Try / catch

err := clickPublishButton(page)
if err != nil && strings.Contains(err.Error(), "获取新版发布按钮位置失败") {
    time.Sleep(500 * time.Millisecond)
    err = clickPublishButton(page)
}

Prevention

When it happens

Trigger: Element.Shape on the `xhs-publish-btn` widget after it was removed from the DOM; the widget has display:none / zero-size layout so no content quads exist; tab closed before the shape query; CDP connection failure.

Common situations: Clicking immediately after page load before the web component lays out; page re-render between ScrollIntoView and Shape; viewport minimized or widget collapsed; headless Chrome crashed.

Related errors


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