jackwener/OpenCLI · error · CommandExecutionError

Could not attach topic "${topic}": no real topic entity appe

Error message

Could not attach topic "${topic}": no real topic entity appeared after selection

What it means

After pressing Enter on a topic suggestion, addTopics verifies the postcondition by counting a "#<topic>[话题]" marker inside the body editor's innerText. If the marker count did not increase, the topic chip/entity was not really created, so it throws instead of reporting a topic as added that isn't.

Source

Thrown at clis/xiaohongshu/publish.js:623

        if (typeof page.pressKey !== 'function') {
            throw new CommandExecutionError(`Could not attach topic "${topic}": page.pressKey is unavailable`);
        }
        try {
            await page.pressKey('Enter');
        }
        catch (err) {
            throw new CommandExecutionError(`Could not attach topic "${topic}": failed to accept suggestion (${err && err.message || err})`);
        }
        await page.wait({ time: 0.8 });
        // Verify the topic chip actually rendered. The chip itself lives in a
        // closed shadow root so we cannot count `<a>` elements, but XHS exposes
        // a stable "#<topic>[话题]" marker in the body editor's innerText once
        // the suggestion is accepted. Require the scoped marker count to
        // increase so an existing marker elsewhere cannot satisfy the write
        // postcondition.
        const afterMarkerCount = Number(unwrapBrowserResult(await page.evaluate(topicMarkerCountScript(topic, bodySelectors)))) || 0;
        if (afterMarkerCount <= beforeMarkerCount) {
            throw new CommandExecutionError(`Could not attach topic "${topic}": no real topic entity appeared after selection`);
        }
        added.push(topic);
        await page.wait({ time: 0.4 });
    }
    return added;
}
async function selectImageTextTab(page) {
    const result = await page.evaluate(`
    () => {
      const isVisible = (el) => {
        if (!el || el.offsetParent === null) return false;
        const rect = el.getBoundingClientRect();
        return rect.width > 0 && rect.height > 0;
      };

      const normalize = (value) => (value || '').replace(/\\s+/g, ' ').trim();
      const selector = 'button, [role="tab"], [role="button"], a, label, div, span, li';
      const nodes = Array.from(document.querySelectorAll(selector));

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Verify the topic exists on XHS (search it in the XHS app/web); topics must match a real topic entity.
  2. Increase wait time before verification or retry addTopics so the chip has time to render.
  3. Try the topic in the XHS UI manually; if XHS changed its DOM, update bodySelectors / topicMarkerCountScript.
  4. Drop the topic from the list or use a close-match topic that XHS suggests.

Example fix

// before
await addTopics(page, ["完全自造的话题"]);
// after
const valid = topics.filter(t => topicLikelyExists(t)); // only real XHS topics
await addTopics(page, valid);
Defensive patterns

Strategy: validation

Validate before calling

const known = new Set(['旅行','美食','摄影']); // topics verified to exist on XHS
const bad = topics.filter(t => !known.has(t));
if (bad.length) console.warn('Topics may not exist on XHS:', bad);

Type guard

null

Try / catch

try { await addTopics(page, topics); }
catch (e) {
  if (/no real topic entity appeared/.test(e.message)) {
    console.warn(`Skipping topic that failed to attach`); // degrade gracefully
  } else throw e;
}

Prevention

When it happens

Trigger: The Enter keypress was sent but XHS did not render a real topic entity: suggestion dropdown had no matching topic, the wrong suggestion was highlighted, or the marker script ran before the chip rendered (count unchanged vs beforeMarkerCount).

Common situations: Topic string with no existing XHS topic (no suggestion appears); typing raced the dropdown; network lag so chip renders after the 0.8s wait; body selector list (bodySelectors) does not match current XHS DOM after a site update.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/4b6add3eb8f20105. Report an issue: GitHub.