{"record":{"id":"1f5b1e6a8e37eeed","repo":"microsoft/playwright","slug":"page-expected-page-or-frame","errorCode":null,"errorMessage":"page: expected Page or Frame","messagePattern":"page: expected Page or Frame","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/client/browserContext.ts","lineNumber":504,"sourceCode":"  }\n\n  async setStorageState(storageState: string | SetStorageState): Promise<void> {\n    const state = await prepareStorageState(storageState);\n    await this._channel.setStorageState({ storageState: state }, kNoTimeout);\n  }\n\n  backgroundPages(): Page[] {\n    return [];\n  }\n\n  serviceWorkers(): Worker[] {\n    return [...this._serviceWorkers];\n  }\n\n  async newCDPSession(page: Page | Frame): Promise<api.CDPSession> {\n    // channelOwner.ts's validation messages don't handle the pseudo-union type, so we're explicit here\n    if (!(page instanceof Page) && !(page instanceof Frame))\n      throw new Error('page: expected Page or Frame');\n    const result = await this._channel.newCDPSession(page instanceof Page ? { page: page._channel } : { frame: page._channel }, kNoTimeout);\n    return CDPSession.from(result.session);\n  }\n\n  _onClose() {\n    this._closingStatus = 'closed';\n    this._browser?._contexts.delete(this);\n    this._browser?._browserType._contexts.delete(this);\n    this._browser?._browserType._playwright.selectors._contextsForSelectors.delete(this);\n    this._disposeHarRouters();\n    this.tracing._resetStackCounter();\n    this.request.tracing._resetStackCounter();\n    this.emit(Events.BrowserContext.Close, this);\n  }\n\n  async [Symbol.asyncDispose]() {\n    await this.close();\n  }","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/client/browserContext.ts#L486-L522","documentation":"Thrown by BrowserContext.newCDPSession(page) when the argument is neither a Page nor a Frame instance. Because the parameter type is a pseudo-union (Page|Frame) that the generic channelOwner validator cannot describe, this method does explicit instanceof checks and throws with a clear message naming the param.","triggerScenarios":"Passing a Worker, BrowserContext, JSHandle, ElementHandle, a raw channel object, or a value from a different Playwright instance into newCDPSession(). Both instanceof checks fail.","commonSituations":"Passing a worker reference when intending a page; mixing objects across two separate Playwright connections (each has its own class realm); passing a serialized/hand-off object.","solutions":["Pass a Page or Frame from the same connection: `await context.newCDPSession(page)`.","For worker targets use the appropriate chromium worker API, not newCDPSession.","Ensure the object originates from the same browser/connection instance."],"exampleFix":"// before\nawait context.newCDPSession(worker);\n// after\nawait context.newCDPSession(page);","handlingStrategy":"type-guard","validationCode":"function isValidCdpTarget(t: unknown): boolean {\n  return t instanceof Page || t instanceof Frame;\n}\nif (!isValidCdpTarget(target))\n  throw new Error('newCDPSession expects a Page or Frame from the same connection.');","typeGuard":"function isPageOrFrame(t: unknown): t is Page | Frame {\n  return t instanceof Page || t instanceof Frame;\n}","tryCatchPattern":null,"preventionTips":["Pass only Page/Frame objects obtained from the same connection.","Avoid reusing objects across separate Playwright instances."],"tags":["cdp","type-validation","browser-context"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}