xpzouying/xiaohongshu-mcp · error

元素当前不可命中

Error message

元素当前不可命中

What it means

This error is raised by ensureClickable in humanize/input.go:74. It evaluates getComputedStyle(element).visibility in the browser and fails when the element's computed visibility is 'hidden', i.e. the element exists but is not currently visible/hittable, so a humanized click or hover on it would be pointless or suspicious.

Source

Thrown at humanize/input.go:74

	if pt.X < 0 || pt.Y < 0 || pt.X > size[0] || pt.Y > size[1] {
		return fmt.Errorf("落点 (%.0f,%.0f) 在视口 %.0fx%.0f 之外", pt.X, pt.Y, size[0], size[1])
	}
	return nil
}

// 不用 document.elementFromPoint:结果不稳定
func ensureClickable(elem *rod.Element, pt proto.Point) error {
	if err := ensurePointInViewport(elem.Page(), pt); err != nil {
		return err
	}

	res, err := elem.Eval(`() => getComputedStyle(this).visibility`)
	if err != nil {
		return nil
	}
	if res.Value.Str() == "hidden" {
		return errors.New("元素当前不可命中")
	}
	return nil
}

func Click(elem *rod.Element) error {
	pt, err := elem.WaitInteractable()
	if err != nil {
		return err
	}

	target := jitterOn(elem, *pt)
	if err := ensureClickable(elem, target); err != nil {
		return err
	}

	mouse := elem.Page().Mouse
	if err := moveMouseCurved(mouse, target); err != nil {
		return err

View on GitHub (pinned to 332d196854)

Solutions

  1. Ensure the element is visible first: open the parent menu/modal or wait for it via rod's WaitVisible/WaitInteractable before the humanize call
  2. Add a short wait/sleep or rod.WaitStable so CSS transitions finish and visibility becomes 'visible'
  3. Re-query the element — stale node references from a re-rendered DOM often report hidden
  4. If the element is genuinely display:none and will never show, fix the flow logic instead of forcing the click

Example fix

// before
btn, _ := page.Element(".submit")
humanize.Click(btn)
// after
btn, _ := page.WaitVisible(".submit")
page.WaitStable()
humanize.Click(btn)
Defensive patterns

Strategy: validation

Validate before calling

v, err := elem.Eval(`() => getComputedStyle(this).visibility !== 'hidden'`)
if err == nil && v.Value.Bool() { /* safe to click */ }

Type guard

func isVisible(elem *rod.Element) bool {
    res, err := elem.Eval(`() => getComputedStyle(this).visibility`)
    return err == nil && res.Value.Str() == "visible"
}

Try / catch

if err := humanize.Click(btn); err != nil {
    if strings.Contains(err.Error(), "不可命中") {
        // wait and retry once after making element visible
    }
    return err
}

Prevention

When it happens

Trigger: Calling humanize.Click, ClickNoWait, or Hover on an element whose CSS computed visibility is hidden — e.g. a menu item inside a not-yet-opened dropdown, an element behind a collapsed panel, or one animated with visibility:hidden transitions.

Common situations: Clicking a 'publish' button that is inside a modal not yet displayed; hovering nav elements in a collapsed menu; animations/transitions still running so visibility is temporarily hidden; page not fully hydrated when the call is made.

Related errors


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