{"record":{"id":"15fbbb518eb43d26","repo":"jackwener/OpenCLI","slug":"amazon-action-navigation-lost-the-current-brows","errorCode":null,"errorMessage":"amazon ${action} navigation lost the current browser target","messagePattern":"amazon (.+?) navigation lost the current browser target","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/amazon/shared.js","lineNumber":387,"sourceCode":"  `);\n    return {\n        href: cleanText(result.href),\n        title: cleanText(result.title),\n        body_text: cleanMultilineText(result.body_text),\n    };\n}\nexport async function gotoAndReadState(page, url, settleMs = 2500, action = 'page') {\n    try {\n        await page.goto(url, { settleMs });\n        await page.wait(1.5);\n        return await readPageState(page);\n    }\n    catch (error) {\n        const message = error instanceof Error ? error.message : String(error);\n        if (message.includes('Inspected target navigated or closed')\n            || message.includes('Cannot find context with specified id')\n            || message.includes('Target closed')) {\n            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.`);\n        }\n        throw error;\n    }\n}\nexport function assertUsableState(state, action) {\n    if (!isRobotState(state))\n        return;\n    throw new CommandExecutionError(`amazon ${action} hit a robot check`, buildChallengeHint(action));\n}\nexport const __test__ = {\n    buildSearchUrl,\n    extractAsin,\n    amazonHostFromInput,\n    buildProductUrl,\n    buildDiscussionUrl,\n    normalizeProductUrl,\n    canonicalizeAmazonUrl,\n    resolveBestsellersUrl,","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/amazon/shared.js#L369-L405","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Open a fresh Amazon tab in the browser CDP is attached to and retry the command","Verify the target browser is still running and the tab exists","Restart the automation session / re-attach CDP to a live tab","Avoid closing or navigating the inspected tab while the command runs"],"exampleFix":"// before\nopencli amazon product B0FJS72893   # tab was closed mid-run\n// after\n# open a new Amazon tab, then:\nopencli amazon product B0FJS72893","handlingStrategy":"retry","validationCode":"// before calling, confirm the CDP target is alive:\nconst alive = await page.evaluate(() => document.readyState).then(() => true).catch(() => false);\nif (!alive) throw new Error('CDP target is stale — open a fresh tab first');","typeGuard":null,"tryCatchPattern":"try {\n  await amazonAction(args);\n} catch (err) {\n  if (err.message.includes('lost the current browser target')) {\n    await openFreshAmazonTab();\n    await amazonAction(args); // retry once on a fresh tab\n  } else throw err;\n}","preventionTips":["Don't close or navigate the inspected tab while commands run","Re-attach CDP after browser restarts","Keep one dedicated stable Amazon tab for automation","Avoid extensions/scripts that reload tabs automatically"],"tags":["browser-automation","cdp","stale-target"],"backgroundTag":"cdp-target-closed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}