{"record":{"id":"d58c4056856e6b1e","repo":"apify/crawlee","slug":"no-browser-context-available","errorCode":null,"errorMessage":"No browser context available","messagePattern":"No browser context available","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/stagehand-crawler/src/internals/stagehand-controller.ts","lineNumber":54,"sourceCode":"        if (!this.#stagehand) {\n            this.#stagehand = this.#stagehandInstances.get(this.browser)!;\n            if (!this.#stagehand) {\n                throw new Error('Stagehand instance not found for browser');\n            }\n        }\n        return this.#stagehand;\n    }\n\n    /**\n     * Creates a new page using the browser's default context.\n     * We use Playwright's browser API directly since we connected via CDP.\n     */\n    protected override async _newPage(_contextOptions?: unknown): Promise<Page> {\n        try {\n            // Get the default context from the Playwright browser (connected via CDP)\n            const contexts = this.browser.contexts();\n            if (contexts.length === 0) {\n                throw new Error('No browser context available');\n            }\n\n            const context = contexts[0];\n            const page = await context.newPage();\n\n            // Track active pages\n            page.once('close', () => {\n                this.activePages--;\n            });\n\n            try {\n                await this.waitForStagehandToRegisterPage(page);\n            } catch (error) {\n                await page.close().catch(() => {});\n                throw error;\n            }\n\n            return page;","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/stagehand-crawler/src/internals/stagehand-controller.ts#L36-L72","documentation":"StagehandController._newPage() connects to the browser over CDP and needs Playwright's default browser context to spawn new pages. When this.browser.contexts() returns an empty array there is no context to create pages from, so the controller throws immediately before attempting newPage().","triggerScenarios":"Calling _newPage (triggered by the browser pool opening a new page) on a browser connected via connectOverCDP that reported zero contexts — e.g. the CDP connection attached without a default context, the browser was closed between connect and newPage, or all contexts were closed explicitly.","commonSituations":"Chromium crashed/exited during the run leaving a dead CDP connection, connecting to a remote browser that has no open targets, race where browser.close() runs while the pool still requests pages, or a custom browser plugin returning a context-less browser.","solutions":["Verify the Stagehand-managed browser is still running and the CDP connection is alive before crawling","Ensure StagehandPlugin._launch succeeded and chromium.connectOverCDP(cdpUrl) returned a browser with contexts (log browser.contexts().length)","Recreate the browser (restart the pool / crawler) if the underlying Chrome process died","Check for code that calls browser.contexts()[0].close() or newContext cleanup that empties the context list"],"exampleFix":"// before\nconst page = await crawler.browserPool.newPage(); // browser already dead\n// after\nif (!browser.isConnected()) {\n    await browserPool.retireBrowser(browser); // force pool to open a fresh one\n}\nconst page = await crawler.browserPool.newPage();","handlingStrategy":"validation","validationCode":"if (!browser.isConnected?.() || browser.contexts().length === 0) {\n    throw new Error('Browser has no contexts; cannot create pages');\n}","typeGuard":"const hasUsableContext = (browser) =>\n    typeof browser?.contexts === 'function' && browser.contexts().length > 0;","tryCatchPattern":"try {\n    const page = await browserPool.newPage();\n} catch (err) {\n    if (err.message.includes('No browser context available')) {\n        await browserPool.retireActiveBrowsers(); // recycle dead browser\n    }\n    throw err;\n}","preventionTips":["Check browser.isConnected() before heavy page creation","Avoid manually closing the default context","Recycle/retire browsers that crashed instead of reusing them","Monitor CDP connection health for long crawls"],"tags":["stagehand","playwright","browser-context","cdp"],"backgroundTag":"no-browser-context","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}