{"record":{"id":"9ef4d8a7c27199f5","repo":"jackwener/OpenCLI","slug":"could-not-attach-topic-topic-failed-to-type","errorCode":null,"errorMessage":"Could not attach topic \"${topic}\": failed to type inline topic query","messagePattern":"Could not attach topic \"(.+?)\": failed to type inline topic query","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/xiaohongshu/publish.js","lineNumber":597,"sourceCode":"        // Separate this topic from the preceding text so the dropdown is clean.\n        if (typeof page.pressKey === 'function') {\n            try {\n                await page.pressKey('Enter');\n            }\n            catch { /* non-fatal */ }\n        }\n        // Type the inline \"#<topic>\" query so XHS pops the inline suggestion\n        // dropdown. We must use `page.insertText` (CDP) rather than the legacy\n        // `execCommand` path, otherwise XHS's editor doesn't fire its keyup\n        // listener and no dropdown appears.\n        if (typeof page.insertText !== 'function') {\n            throw new CommandExecutionError(`Could not attach topic \"${topic}\": page.insertText is unavailable`);\n        }\n        try {\n            await page.insertText(`#${topic}`);\n        }\n        catch {\n            throw new CommandExecutionError(`Could not attach topic \"${topic}\": failed to type inline topic query`);\n        }\n        await page.wait({ time: 1.2 }); // Let the suggestion dropdown render.\n        // The suggestion dropdown lives inside the editor's closed shadow root,\n        // so light-DOM queries cannot enumerate its items. XHS auto-highlights\n        // the first matching suggestion as soon as the query is typed, so\n        // pressing Enter accepts it directly. `page.nativeClick` would also\n        // work but is not always wired up in the browser-bridge wrapper.\n        if (typeof page.pressKey !== 'function') {\n            throw new CommandExecutionError(`Could not attach topic \"${topic}\": page.pressKey is unavailable`);\n        }\n        try {\n            await page.pressKey('Enter');\n        }\n        catch (err) {\n            throw new CommandExecutionError(`Could not attach topic \"${topic}\": failed to accept suggestion (${err && err.message || err})`);\n        }\n        await page.wait({ time: 0.8 });\n        // Verify the topic chip actually rendered. The chip itself lives in a","sourceCodeStart":579,"sourceCodeEnd":615,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/xiaohongshu/publish.js#L579-L615","documentation":"CommandExecutionError from addTopics: the page.insertText(`#<topic>`) call itself threw, so the inline topic query could not be typed into the editor. The error wraps the failure with per-topic context so you know which topic failed.","triggerScenarios":"Editor lost focus between focus and typing (overlay, re-render); CDP insertText failing because the target frame/page detached; the element became non-editable; navigation mid-publish.","commonSituations":"Flaky sessions where XHS re-renders the editor while typing; browser tab crashed or navigated; topic text containing characters that break input injection (rare).","solutions":["Retry the publish — transient focus/re-render races often succeed on a second run.","Ensure no dialogs/overlays steal focus and the page stays on the publisher URL.","Slow things down: add waits between publish steps if the environment supports it.","Update the CLI/bridge if insertText failures persist; capture a screenshot to diagnose."],"exampleFix":"// before\nawait xhs.publish({ topics: ['travel', 'food'], ... });\n// after\n// retry wrapper\nasync function publishWithRetry(opts, n = 2) {\n  try { return await xhs.publish(opts); }\n  catch (e) {\n    if (n > 0 && e.message.includes('failed to type inline topic query')) return publishWithRetry(opts, n - 1);\n    throw e;\n  }\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await xhs.publish(opts);\n} catch (err) {\n  if (err.message.includes('failed to type inline topic query')) {\n    return retryPublish(opts, 2); // transient focus/injection races\n  }\n  throw err;\n}","preventionTips":["Retry once or twice — failures are often transient races","Keep the page free of overlays that steal focus","Avoid navigating or interacting with the tab mid-publish","Capture screenshots to diagnose persistent injection failures"],"tags":["automation","cdp","input","xiaohongshu"],"backgroundTag":"browser-input-injection-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}