jackwener/OpenCLI · error · CommandExecutionError

需要浏览器页面

Error message

需要浏览器页面

What it means

The publish command's func requires a live browser page object; when it is null/undefined the command throws CommandExecutionError('需要浏览器页面'). This guards commands that must run inside an authenticated browser session.

Source

Thrown at clis/wechat-channels/publish.js:577

  name: 'publish',
  access: 'write',
  description: '发布视频到视频号',
  domain: 'channels.weixin.qq.com',
  strategy: Strategy.COOKIE,
  browser: true,
  navigateBefore: false,
  args: [
    { name: 'video',    required: true,  positional: true, help: '视频文件路径 (.mp4/.mov/.avi/.webm)' },
    { name: 'title',    required: false, help: '短标题(建议 6-16 字)' },
    { name: 'caption',  required: false, help: '描述内容,支持直接写 #话题(如:日常生活 #搞笑 #生活)' },
    { name: 'schedule', required: false, help: '定时发布时间(ISO8601 或 Unix 秒,如 "2026-05-20 10:00")' },
    { name: 'draft',    type: 'bool', default: false, help: '保存为草稿' },
    { name: 'manual',   type: 'bool', default: false, help: '填完所有字段后不自动发布,由用户手动点击发表(务必同时传 --site-session persistent,否则表单页约 30 秒后会被重置为空白页)' },
    { name: 'timeout',  type: 'int', required: false, default: 600, help: '命令整体超时秒数(含登录等待 + 上传转码,默认 600)' },
  ],
  columns: ['status', 'title', 'detail'],
  func: async (page, kwargs) => {
    if (!page) throw new CommandExecutionError('需要浏览器页面');

    // ── 1. Validate inputs ───────────────────────────────────────────────
    const timeoutSeconds = parseTimeoutSeconds(kwargs.timeout);
    const deadline = Date.now() + timeoutSeconds * 1000;
    const videoPath = requireFilePath(kwargs.video, '视频', VIDEO_EXTENSIONS);

    const title = String(kwargs.title ?? '').trim();
    const caption = String(kwargs.caption ?? '').trim();
    const scheduleTime = parseScheduleDate(kwargs.schedule || null);
    const isDraft = parseBooleanFlag(kwargs.draft);
    const isManual = parseBooleanFlag(kwargs.manual);

    // ── 2. Navigate to creator center ────────────────────────────────────
    await page.goto(PUBLISH_URL);
    await page.wait({ time: 4 }); // wujie needs extra time to bootstrap

    // ── 3. Login check — fallback: navigate to login page and wait ───────
    {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run the command with the browser/session flag (e.g. --site-session persistent) so a page is provided
  2. Check that the browser launched successfully before the command func runs
  3. When calling func programmatically, pass a real page object or mock
  4. Add an early check in the CLI wrapper to give a clearer message when page creation failed

Example fix

// before
await cli.run('publish', { video: 'v.mp4' });
// after
await cli.run('publish', { video: 'v.mp4', siteSession: 'persistent' });
Defensive patterns

Strategy: validation

Validate before calling

if (typeof page === 'undefined' || !page) {
  throw new Error('需要浏览器页面: run with a browser session (e.g. --site-session persistent)');
}

Type guard

function hasBrowserPage(p) {
  return !!p && typeof p.goto === 'function' && typeof p.evaluate === 'function';
}

Try / catch

try {
  await publishCmd(page, kwargs);
} catch (e) {
  if (e.message === '需要浏览器页面') {
    console.error('No browser page: launch the command with a persistent site session.');
    process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the publish command without a browser session, e.g. missing --site-session persistent / not launching the browser wrapper, or running the func programmatically without passing a page.

Common situations: Forgetting the session flag so no page is created; invoking the CLI function directly in tests without a page fixture; browser failed to launch upstream and page was passed as null.

Related errors


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