{"record":{"id":"a567faeb797bddc8","repo":"microsoft/playwright","slug":"target-page-context-or-browser-has-been-closed-a567fa","errorCode":null,"errorMessage":"Target page, context or browser has been closed","messagePattern":"Target page, context or browser has been closed","errorType":"exception","errorClass":"TargetClosedError","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/server/webkit/wkPage.ts","lineNumber":530,"sourceCode":"      worldName = 'utility';\n    const context = new dom.FrameExecutionContext(delegate, frame, worldName);\n    if (worldName)\n      frame.contextCreated(worldName, context);\n    this._contextIdToContext.set(contextPayload.id, context);\n  }\n\n  private async _onBindingCalled(contextId: Protocol.Runtime.ExecutionContextId, argument: string) {\n    const pageOrError = await this._page.waitForInitializedOrError();\n    if (!(pageOrError instanceof Error)) {\n      const context = this._contextIdToContext.get(contextId);\n      if (context)\n        await this._page.onBindingCalled(argument, context);\n    }\n  }\n\n  async navigateFrame(frame: frames.Frame, url: string, referrer: string | undefined): Promise<frames.GotoResult> {\n    if (this._pageProxySession.isDisposed())\n      throw new TargetClosedError(this._page.closeReason());\n    const pageProxyId = this._pageProxySession.sessionId;\n    const result = await this._pageProxySession.connection.browserSession.send('Playwright.navigate', { url, pageProxyId, frameId: frame._id, referrer });\n    return { newDocumentId: result.loaderId };\n  }\n\n  _onConsoleMessage(event: Protocol.Console.messageAddedPayload) {\n    // Note: do no introduce await in this function, otherwise we lose the ordering.\n    // For example, frame.setContent relies on this.\n    const { type, level, text, parameters, url, line: lineNumber, column: columnNumber, source } = event.message;\n    if (level === 'error' && source === 'javascript') {\n      const { name, message } = splitErrorMessage(text);\n\n      let stack: string;\n      if (event.message.stackTrace) {\n        stack = text + '\\n' + event.message.stackTrace.callFrames.map(callFrame => {\n          return `    at ${callFrame.functionName || 'unknown'} (${callFrame.url}:${callFrame.lineNumber}:${callFrame.columnNumber})`;\n        }).join('\\n');\n      } else {","sourceCodeStart":512,"sourceCodeEnd":548,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/server/webkit/wkPage.ts#L512-L548","documentation":"navigateFrame() checks pageProxySession.isDisposed() before sending Playwright.navigate; if the page session is already torn down it throws TargetClosedError carrying the page's recorded closeReason. This is the WebKit equivalent of 'navigation attempted on a closed page'.","triggerScenarios":"Calling page.goto()/frame.goto() (or any navigation-triggering API) after page.close() began, after a crash, or during teardown. Also from locator.click() that resolves a navigation on a closing page.","commonSituations":"Navigating in a hook after the page was closed in the test body. Fire-and-forget navigations racing with context teardown. Re-navigating a page whose tab crashed.","solutions":["Await all navigations before closing; do not navigate in teardown after close.","Guard with page.isClosed(); skip navigation when true.","Wrap navigation in try/catch and treat TargetClosedError as a clean stop during teardown."],"exampleFix":"// before\nawait page.goto(url); // page already closed\n\n// after\nif (!page.isClosed()) await page.goto(url);","handlingStrategy":"try-catch","validationCode":"if (!page.isClosed()) await page.goto(url);","typeGuard":"null","tryCatchPattern":"try { await page.goto(url); }\ncatch (e) {\n  if (e.name === 'TargetClosedError' || /has been closed/.test(e.message)) return; // expected during teardown\n  throw e;\n}","preventionTips":["Do not navigate after close() in hooks.","Guard navigations with page.isClosed()."],"tags":["webkit","navigation","lifecycle","closed"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}