{"record":{"id":"3df071fdbc719321","repo":"grafana/k6","slug":"waiting-for-selector-q-execution-context-q-not","errorCode":null,"errorMessage":"waiting for selector %q: execution context %q not found","messagePattern":"waiting for selector %q: execution context %q not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":516,"sourceCode":"\tif handle == nil {\n\t\treturn nil, fmt.Errorf(\"waiting for selector %q did not result in any nodes\", selector)\n\t}\n\n\t// We always return ElementHandles in the main execution context (aka \"DOM world\")\n\tf.executionContextMu.RLock()\n\tdefer f.executionContextMu.RUnlock()\n\n\tuec := f.executionContexts[utilityWorld]\n\n\t// An element should belong to the current main world execution context, and\n\t// not to the utility world context, otherwise, we should adopt it to the\n\t// current world's execution context. This is only valid when the handle\n\t// is from the current frame and not part of a nested frame.\n\tadopted := handle\n\tif uec != nil && uec == handle.execCtx {\n\t\twec := f.executionContexts[mainWorld]\n\t\tif wec == nil {\n\t\t\treturn nil, fmt.Errorf(\"waiting for selector %q: execution context %q not found\", selector, mainWorld)\n\t\t}\n\n\t\tif adopted, err = wec.adoptElementHandle(handle); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"waiting for selector %q: adopting element handle: %w\", selector, err)\n\t\t}\n\n\t\tif err = handle.Dispose(); err != nil {\n\t\t\tf.log.Warnf(\n\t\t\t\t\"Frame:waitForSelector\",\n\t\t\t\t\"fid:%s furl:%q sel:%q disposing element handle: %v\",\n\t\t\t\tf.ID(), f.URL(), selector, err,\n\t\t\t)\n\t\t}\n\t}\n\n\treturn adopted, nil\n}\n","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L498-L534","documentation":"waitForSelector found the element in the utility world and wants to adopt it into the main world, but the frame's main-world execution context is missing (nil). Contexts are destroyed and recreated on navigation; if the page navigated between finding the element and the adoption step, the mainWorld context is gone and this error names the selector and the missing world.","triggerScenarios":"A navigation destroying the main-world context right after the element was found (inside waitForSelector's adoption step); the frame being detached at that moment; context creation lag on brand-new frames.","commonSituations":"Querying elements right as SPAs navigate; frames created and immediately navigated away; heavy pages where context (re)creation lags behind queries.","solutions":["Retry the wait — the element will be found directly in the new context","Wait for load state before querying","Sequence queries after waitForNavigation to avoid the race window"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  el = await page.waitForSelector('#x');\n} catch (e) {\n  if (/execution context .* not found/.test(e.message)) {\n    await page.waitForLoadState();\n    el = await page.waitForSelector('#x');\n  } else throw e;\n}","preventionTips":["Sequence queries after waitForNavigation on fast-SPA pages","Wait for load state before querying new frames","Retry once on context-recreation races before failing the test"],"tags":["browser","execution-context","navigation","race-condition","wait-for-selector"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}