{"record":{"id":"e37bb06bcb64ebe1","repo":"grafana/k6","slug":"getting-injected-script-w","errorCode":null,"errorMessage":"getting injected script: %w","messagePattern":"getting injected script: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1600,"sourceCode":"// WaitForSelector waits for the selector to appear in the DOM.\nfunc (h *ElementHandle) WaitForSelector(selector string, opts *FrameWaitForSelectorOptions) (*ElementHandle, error) {\n\thandle, err := h.waitForSelector(h.ctx, selector, opts)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"waiting for selector %q: %w\", selector, err)\n\t}\n\n\treturn handle, nil\n}\n\n// evalWithScript evaluates the given js code in the scope of this ElementHandle and returns the result.\n// The js code can call helper functions from injected_script.js.\nfunc (h *ElementHandle) evalWithScript(\n\tctx context.Context,\n\topts evalOptions, js string, args ...any,\n) (any, error) {\n\tscript, err := h.execCtx.getInjectedScript(h.ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting injected script: %w\", err)\n\t}\n\treturn h.eval(ctx, opts, js, append([]any{script}, args...)...)\n}\n\n// eval evaluates the given js code in the scope of this ElementHandle and returns the result.\nfunc (h *ElementHandle) eval(\n\tctx context.Context,\n\topts evalOptions, js string, args ...any,\n) (any, error) {\n\t// passing `h` makes it evaluate js code in the element handle's scope.\n\treturn h.execCtx.eval(ctx, opts, js, append([]any{h}, args...)...)\n}\n\nfunc (h *ElementHandle) newAction(\n\tstates []string, fn elementHandleActionFunc, force bool, retry bool, noWaitAfter bool, timeout time.Duration,\n) func(apiCtx context.Context, resultCh chan any, errCh chan error) {\n\t// All or a subset of the following actionability checks are made before performing the actual action:\n\t// 1. Attached to DOM","sourceCodeStart":1582,"sourceCodeEnd":1618,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/element_handle.go#L1582-L1618","documentation":"Internal error in the k6 browser module whenever an ElementHandle runs an injected-script evaluation (behind check, fill, selectOption, setInputFiles, hover, etc.). getInjectedScript evaluates the bundled helper-script source once per ExecutionContext and caches it; failure here means that bootstrap evaluation failed — classically because the execution context was destroyed by navigation, or CDP rejected the evaluation.","triggerScenarios":"Any element action on a handle whose frame navigated (context destroyed) or was closed; evaluating while the page is being torn down at test end; a crash of the renderer process invalidating contexts. Because every action (click state checks, fill, select) calls evalWithScript, this error can surface wrapped inside many higher-level action failures.","commonSituations":"Actions issued against a page mid-navigation (click triggers redirect, then a state check runs on the dead context); test iteration ending while async browser actions are still in flight; renderer OOM/crash under memory pressure in large tests.","solutions":["Gate actions on load state: await page.waitForLoadState('domcontentloaded') or waitForNavigation after clicks that redirect, before further element actions.","Re-acquire page/frame/handle after any navigation — never reuse handles across pages.","Ensure the k6 test iteration does not exit while browser promises are pending (await all async operations or close pages deliberately).","If the renderer is crashing, reduce concurrency or check the page for memory-heavy behavior; run with DEBUG to see the underlying CDP error."],"exampleFix":"// before\nawait page.click('#logout'); // triggers navigation\nconst el = await page.$('#login');\nawait el.fill('user'); // context destroyed -> injected script bootstrap fails\n\n// after\nawait Promise.all([page.waitForNavigation(), page.click('#logout')]);\nconst el = await page.waitForSelector('#login', { state: 'visible' });\nawait el.fill('user');","handlingStrategy":"retry","validationCode":"await page.waitForLoadState('domcontentloaded');\n// re-acquire handle from a live frame before any action that uses the injected script\nconst el = await page.waitForSelector(sel, { state: 'attached' });\nawait el.fill('value');","typeGuard":null,"tryCatchPattern":"for (let i = 0; i < 2; i++) {\n  try { await action(el); break; }\n  catch (e) {\n    if (i === 1 || !String(e).includes('injected script')) throw e;\n    await page.waitForLoadState('domcontentloaded');\n    el = await page.waitForSelector(sel, { state: 'attached' });\n  }\n}","preventionTips":["Never reuse page/frame/element handles across a navigation boundary.","Pair navigation-triggering clicks with waitForNavigation/waitForLoadState.","Close pages deliberately and await all browser promises before the iteration ends."],"tags":["k6","browser","element-handle","execution-context","navigation"],"backgroundTag":"execution-context-destroyed","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}