{"record":{"id":"d44fed39a500c733","repo":"microsoft/playwright","slug":"frame-does-not-yet-have-a-main-execution-context","errorCode":null,"errorMessage":"Frame does not yet have a main execution context","messagePattern":"Frame does not yet have a main execution context","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"packages/playwright-core/src/server/frames.ts","lineNumber":626,"sourceCode":"      throw new EvaluationStalledError('Open JavaScript dialog prevents evaluation');\n\n    const promise = new ManualPromise<T>();\n    this._raceAgainstEvaluationStallingEventsPromises.add(promise);\n    try {\n      return await Promise.race([\n        cb(),\n        promise\n      ]);\n    } finally {\n      this._raceAgainstEvaluationStallingEventsPromises.delete(promise);\n    }\n  }\n\n  nonStallingRawEvaluateInExistingMainContext(expression: string): Promise<any> {\n    return this.raceAgainstEvaluationStallingEvents(() => {\n      const context = this.existingContext('main');\n      if (!context)\n        throw new Error('Frame does not yet have a main execution context');\n      return context.rawEvaluateJSON(expression);\n    });\n  }\n\n  nonStallingEvaluateInExistingContext(expression: string, world: types.World): Promise<any> {\n    return this.raceAgainstEvaluationStallingEvents(() => {\n      const context = this._contextData.get(world)?.context;\n      if (!context)\n        throw new Error('Frame does not yet have the execution context');\n      return context.evaluateExpression(expression, { isFunction: false });\n    });\n  }\n\n  _recalculateNetworkIdle(frameThatAllowsRemovingNetworkIdle?: Frame) {\n    let isNetworkIdle = this._firedNetworkIdleSelf;\n    for (const child of this._childFrames) {\n      child._recalculateNetworkIdle(frameThatAllowsRemovingNetworkIdle);\n      // We require networkidle event to be fired in the whole frame subtree, and then consider it done.","sourceCodeStart":608,"sourceCodeEnd":644,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/server/frames.ts#L608-L644","documentation":"nonStallingRawEvaluateInExistingMainContext needs a main (page) execution context that already exists (non-blocking). If the frame has not yet received its main context - e.g. right after attachment or before first commit - it throws 'Frame does not yet have a main execution context'.","triggerScenarios":"Running an evaluation that uses the 'existing context' (non-stalling) path on a frame that has not yet had its main world context created: freshly attached iframes, about:blank before commit, or frames mid-navigation before the new document's context is ready.","commonSituations":"Acting on a just-added iframe before DOMContentLoaded; cross-frame helper calls during rapid navigations; evaluations triggered from page event handlers that fire before the context is attached; timing-sensitive tests on slow CI.","solutions":["Use the stalling context() method instead, which awaits the context promise.","Wait for the frame's load/DOMContentLoaded lifecycle before evaluating.","Ensure the frame is the one you expect (not a transient ad/popup iframe).","Retry after a short waitForFunction on the page-level context."],"exampleFix":"// before: existing-context call on a fresh iframe\nconst result = await frame.nonStallingRawEvaluate(...); // throws\n// after: await the context\nawait frame.context('main');\n// or wait for load\nawait frame.waitForLoadState?.('domcontentloaded');","handlingStrategy":"validation","validationCode":"// Wait for the main context before evaluating\nawait page.frameLocator(selector).locator('body').waitFor();\n// then operate","typeGuard":"// Guard: does the frame expose a context yet?\nfunction hasMainContext(frame: any): boolean {\n  return !!frame._contextData?.get('main')?.context;\n}","tryCatchPattern":"null","preventionTips":["For user-driven flows, prefer waitForLoadState before evaluating in new frames.","Avoid the nonStalling* APIs in user code; they are internal fast-paths."],"tags":["frames","execution-context","timing"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}