jackwener/OpenCLI · error · CommandExecutionError

1688 ${action} navigation lost the current browser target

Error message

1688 ${action} navigation lost the current browser target

What it means

gotoAndReadState navigates the page and reads DOM state; if navigation fails because the CDP-attached browser target navigated away or closed, it throws CommandExecutionError with a captcha-hint message. This converts Puppeteer/CDP connection-level failures ('Inspected target navigated or closed', 'Cannot find context with specified id', 'Target closed') into an actionable CLI error telling the user to attach to a fresh tab.

Source

Thrown at clis/1688/shared.js:401

  `);
    return {
        href: cleanText(result.href),
        title: cleanText(result.title),
        body_text: cleanMultilineText(result.body_text),
    };
}
export async function gotoAndReadState(page, url, settleMs = 2500, action = 'page') {
    try {
        await page.goto(url, { settleMs });
        await page.wait(1.5);
        return readPageState(page);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        if (message.includes('Inspected target navigated or closed')
            || message.includes('Cannot find context with specified id')
            || message.includes('Target closed')) {
            throw new CommandExecutionError(`1688 ${action} navigation lost the current browser target`, `${buildCaptchaHint(action)} If CDP is attached to a stale or blocked tab, open a fresh 1688 tab and point OPENCLI_CDP_TARGET at that tab.`);
        }
        throw error;
    }
}
export async function ensure1688Session(page) {
    const state = await gotoAndReadState(page, HOME_URL, 1500, 'homepage');
    assertAuthenticatedState(state, 'homepage');
}
export function assertAuthenticatedState(state, action) {
    if (!isCaptchaState(state) && !isLoginState(state))
        return;
    throw new AuthRequiredError('1688.com', `请先在共享 Chrome 完成 1688 登录/验证,再重试(${action})`);
}
export function assertNotCaptcha(state, action) {
    assertAuthenticatedState(state, action);
}
export function toNumber(value) {
    if (typeof value === 'number' && Number.isFinite(value)) {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Open a fresh 1688 tab in the shared Chrome instance and set OPENCLI_CDP_TARGET to that new tab's target, then retry.
  2. Verify the target tab is still open and stable (not auto-refreshing/closing) before running the command.
  3. Reconnect the CDP attachment (restart the CLI session) to drop the stale session; if anti-bot keeps hijacking the tab, complete login/captcha in Chrome first.

Example fix

// before
OPENCLI_CDP_TARGET=old-closed-tab opencli 1688 item 887904326744
// after — attach to a live 1688 tab
export OPENCLI_CDP_TARGET=$(opencli chrome target --url 'detail.1688.com')
opencli 1688 item 887904326744
Defensive patterns

Strategy: try-catch

Validate before calling

// before running: verify the CDP target tab is alive
const targetAlive = await fetch(`${CDP_HTTP}/json/list`)
  .then(r => r.json())
  .then(list => list.some(t => t.id === TARGET_ID && !t.url.startsWith('devtools')));
if (!targetAlive) throw new Error('OPENCLI_CDP_TARGET is stale — attach to a live 1688 tab');

Type guard

function isHealthyCdpTarget(target) {
  return Boolean(target && target.id && target.url && !target.url.startsWith('devtools') && !target.closed);
}

Try / catch

try {
  await run1688Command();
} catch (e) {
  if (e instanceof CommandExecutionError && e.message.includes('lost the current browser target')) {
    // refresh OPENCLI_CDP_TARGET to a fresh 1688 tab and retry once
    const tabs = await fetch(`${CDP_HTTP}/json/list`).then(r => r.json());
    const tab = tabs.find(t => t.url.includes('1688.com'));
    if (tab) { process.env.OPENCLI_CDP_TARGET = tab.id; return run1688Command(); }
  }
  throw e;
}

Prevention

When it happens

Trigger: OPENCLI_CDP_TARGET points at a tab that closes or navigates during gotoAndReadState; the CDP execution context is destroyed mid-navigation; the shared Chrome tab is closed by the user; 1688 redirects/crashes the tab during anti-bot interstitials.

Common situations: Stale OPENCLI_CDP_TARGET after Chrome restarted or tabs changed; automation attached to an incognito/temporary tab; 1688 anti-bot flow navigating the tab mid-scrape; CDP session invalidated by a manual user action in shared Chrome.

Related errors


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