xpzouying/xiaohongshu-mcp · error

获取正文长度文本失败

Error message

获取正文长度文本失败

What it means

This error wraps a failure to read the text of the body-length indicator element (div.edit-container div.length-error) via elem.Text() during publish validation. The library throws it when the Xiaohongshu creator page shows a length-error badge but its text content cannot be fetched from the DOM (element detached, page navigated, or CDP call failed). It is an intermediate wrap: the caller checkContentMaxLength detects the over-length UI element and needs its text to build the final length message.

Source

Thrown at xiaohongshu/publish.go:629

		return errors.Wrap(err, "获取标题长度文本失败")
	}

	return makeMaxLengthError(titleLength)
}

func checkContentMaxLength(page *rod.Page) error {
	has, elem, err := page.Has(`div.edit-container div.length-error`)
	if err != nil {
		return errors.Wrap(err, "检查正文长度元素失败")
	}

	if !has {
		return nil
	}

	contentLength, err := elem.Text()
	if err != nil {
		return errors.Wrap(err, "获取正文长度文本失败")
	}

	return makeMaxLengthError(contentLength)
}

func makeMaxLengthError(elemText string) error {
	parts := strings.Split(elemText, "/")
	if len(parts) != 2 {
		return errors.Errorf("长度超过限制: %s", elemText)
	}

	currLen, maxLen := parts[0], parts[1]

	return errors.Errorf("当前输入长度为%s,最大长度为%s", currLen, maxLen)
}

// contentElemSelectors 正文输入框的候选选择器,按先后顺序尝试。
var contentElemSelectors = []string{

View on GitHub (pinned to 332d196854)

Solutions

  1. Retry the publish flow — the error is usually transient DOM churn; checkContentMaxLength is re-evaluated on each attempt.
  2. Shorten the content before publishing so the length-error element never appears and elem.Text() is never called.
  3. Update selectors/page handling if the site DOM changed; re-check div.edit-container div.length-error still exists with a stable text node.
  4. Ensure the browser page stays open and no concurrent navigation occurs during publish.

Example fix

// before
has, elem, err := page.Has(`div.edit-container div.length-error`)
...
contentLength, err := elem.Text()
// after
has, elem, err := page.Has(`div.edit-container div.length-error`)
...
// retry text read to survive transient DOM churn
var contentLength string
for i := 0; i < 3; i++ {
    if contentLength, err = elem.Text(); err == nil {
        break
    }
    time.Sleep(200 * time.Millisecond)
}
Defensive patterns

Strategy: retry

Validate before calling

// pre-check content length in Go before publishing so the over-length badge never appears
if utf8.RuneCountInString(content) > 1000 {
    return fmt.Errorf("正文长度 %d 超过最大 1000", utf8.RuneCountInString(content))
}

Type guard

func hasLengthError(page *rod.Page) bool {
    has, _, err := page.Has(`div.edit-container div.length-error`)
    return err == nil && has
}

Try / catch

err := publisher.Publish(ctx, opts)
if err != nil && strings.Contains(err.Error(), "获取正文长度文本失败") {
    // transient DOM churn: retry once with a fresh page state
    time.Sleep(time.Second)
    err = publisher.Publish(ctx, opts)
}

Prevention

When it happens

Trigger: submitPublish calls checkContentMaxLength after filling content; page.Has finds div.edit-container div.length-error, but elem.Text() fails — typically because the page re-rendered and the element was removed between Has() and Text(), the tab was closed/navigated, or the CDP session errored.

Common situations: Slow or flaky network causing DOM churn during publish; Xiaohongshu front-end update that replaces the length-error node after detection; user closes the browser tab mid-publish; browser automation session interrupted (timeout, context cancelled).

Related errors


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