jackwener/OpenCLI · error · CommandExecutionError

amazon ${action} navigation lost the current browser target

Error message

amazon ${action} navigation lost the current browser target

What it means

A CommandExecutionError thrown by gotoAndReadState when the CDP navigation to an Amazon page fails because the inspected browser target (tab/context) navigated away or closed mid-operation. The library detects these specific CDP error messages and converts them into this clearer error with recovery guidance, instead of surfacing the raw Chrome DevTools error.

Source

Thrown at clis/amazon/shared.js:387

  `);
    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 await 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(`amazon ${action} navigation lost the current browser target`, `${buildChallengeHint(action)} If CDP is attached to a stale tab, open a fresh Amazon tab and retry.`);
        }
        throw error;
    }
}
export function assertUsableState(state, action) {
    if (!isRobotState(state))
        return;
    throw new CommandExecutionError(`amazon ${action} hit a robot check`, buildChallengeHint(action));
}
export const __test__ = {
    buildSearchUrl,
    extractAsin,
    amazonHostFromInput,
    buildProductUrl,
    buildDiscussionUrl,
    normalizeProductUrl,
    canonicalizeAmazonUrl,
    resolveBestsellersUrl,

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Open a fresh Amazon tab in the browser CDP is attached to and retry the command
  2. Verify the target browser is still running and the tab exists
  3. Restart the automation session / re-attach CDP to a live tab
  4. Avoid closing or navigating the inspected tab while the command runs

Example fix

// before
opencli amazon product B0FJS72893   # tab was closed mid-run
// after
# open a new Amazon tab, then:
opencli amazon product B0FJS72893
Defensive patterns

Strategy: retry

Validate before calling

// before calling, confirm the CDP target is alive:
const alive = await page.evaluate(() => document.readyState).then(() => true).catch(() => false);
if (!alive) throw new Error('CDP target is stale — open a fresh tab first');

Try / catch

try {
  await amazonAction(args);
} catch (err) {
  if (err.message.includes('lost the current browser target')) {
    await openFreshAmazonTab();
    await amazonAction(args); // retry once on a fresh tab
  } else throw err;
}

Prevention

When it happens

Trigger: The Chrome tab CDP is attached to is closed or navigates during the `amazon <action>` command; the browser auto-refreshes; an extension or another automation run closes the tab; CDP points at a stale tab that has since been replaced.

Common situations: Long-lived automation sessions where the tab was closed between commands; headless browser restarts; a user manually closing the inspected tab; page redirects triggered by Amazon mid-read.

Related errors


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