jackwener/OpenCLI · error

Browser page required

Error message

Browser page required

What it means

The xhs publish command's func is invoked with a browser `page` handle supplied by the CLI runner; if the runner could not provide an active browser page the handler throws this plain Error immediately. It is a defensive guard ensuring all subsequent automation (page.goto, page.wait, clicks) has a real page to operate on.

Source

Thrown at clis/xiaohongshu/publish.js:1182

    access: 'write',
    description: '小红书发布图文笔记 (creator center UI automation)',
    domain: 'creator.xiaohongshu.com',
    strategy: Strategy.COOKIE,
    browser: true,
    navigateBefore: false,
    args: [
        { name: 'content', required: true, positional: true, help: '笔记正文' },
        { name: 'title', required: true, help: '笔记标题 (最多20字)' },
        { name: 'images', required: false, help: '图片路径,逗号分隔,最多9张 (jpg/png/gif/webp)' },
        { name: 'card-text', required: false, help: `文字配图卡片文字,多张卡片用 ${CARD_TEXT_DELIM} 分隔,卡内换行用 \\n` },
        { name: 'card-style', required: false, help: `文字配图卡片样式,运行时按页面实际选项匹配;找不到会失败。省略时使用${DEFAULT_CARD_STYLE}。可选: ${CARD_STYLE_GUIDE.map(([n, s]) => `${n}(${s})`).join(' ')}` },
        { name: 'topics', required: false, help: '话题标签,逗号分隔,不含 # 号' },
        { name: 'draft', type: 'bool', default: false, help: '保存为草稿,不直接发布' },
    ],
    columns: ['status', 'detail'],
    func: async (page, kwargs) => {
        if (!page)
            throw new Error('Browser page required');
        const title = String(kwargs.title ?? '').trim();
        const content = String(kwargs.content ?? '').trim();
        const imagePaths = kwargs.images
            ? String(kwargs.images).split(',').map((s) => s.trim()).filter(Boolean)
            : [];
        const topics = kwargs.topics
            ? String(kwargs.topics).split(',').map((s) => s.trim()).filter(Boolean)
            : [];
        const isDraft = Boolean(kwargs.draft);
        const cardText = kwargs['card-text'] ? String(kwargs['card-text']) : '';
        const cards = cardText
            ? cardText.split(CARD_TEXT_DELIM).map((s) => s.trim()).filter(Boolean)
            : [];
        const cardStyle = kwargs['card-style'] ? String(kwargs['card-style']).trim() : '';
        const isTextImage = cards.length > 0;
        // ── Validate inputs ────────────────────────────────────────────────────────
        if (!title)
            throw new ArgumentError('--title is required');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Launch/login to the browser first (run the xhs login/open command) before publishing
  2. Check that the browser process is alive and not crashed; restart it
  3. If calling the command programmatically, pass a valid playwright/puppeteer Page object
  4. Check earlier logs for browser-launch errors that left page undefined

Example fix

// before
await publishCommand.func(null, { title: 'hi', content: 'body' });
// after
const page = await openBrowser();
await publishCommand.func(page, { title: 'hi', content: 'body' });
Defensive patterns

Strategy: validation

Validate before calling

if (!page || typeof page.goto !== 'function') {
  throw new Error('Launch the browser session before running the xhs publish command');
}

Type guard

function hasPage(p) {
  return !!p && typeof p.goto === 'function' && typeof p.wait === 'function';
}

Try / catch

try {
  await runPublish(args);
} catch (e) {
  if (e.message === 'Browser page required') {
    await launchAndLogin();
    await runPublish(args);
  } else throw e;
}

Prevention

When it happens

Trigger: Invoking the xiaohongshu publish command without an active browser session — e.g. the browser failed to launch, was closed beforehand, or the command was called programmatically passing null/undefined as the page argument.

Common situations: Browser not started before running the command (missing --browser/login step); headless browser crashed or was closed mid-session; calling the exported command function directly in tests without stubbing a page; browser startup failure swallowed upstream.

Related errors


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