{"record":{"id":"2bc44ffa37df4bec","repo":"grafana/k6","slug":"getting-document-element-handle-w","errorCode":null,"errorMessage":"getting document element handle: %w","messagePattern":"getting document element handle: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":289,"sourceCode":"\t// see: nullContext().\n\tf.executionContextMu.RLock()\n\tdefer f.executionContextMu.RUnlock()\n\n\treturn f.documentHandle, f.documentHandle != nil\n}\n\nfunc (f *Frame) newDocumentHandle() (*ElementHandle, error) {\n\tresult, err := f.evaluate(\n\t\tf.ctx,\n\t\tmainWorld,\n\t\tevalOptions{\n\t\t\tforceCallable: false,\n\t\t\treturnByValue: false,\n\t\t},\n\t\t\"document\",\n\t)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting document element handle: %w\", err)\n\t}\n\tif result == nil {\n\t\treturn nil, fmt.Errorf(\"document element handle is nil\")\n\t}\n\tdh, ok := result.(*ElementHandle)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"unexpected document handle type: %T\", result)\n\t}\n\n\treturn dh, nil\n}\n\nfunc (f *Frame) hasContext(world executionWorld) bool {\n\tf.executionContextMu.RLock()\n\tdefer f.executionContextMu.RUnlock()\n\n\treturn f.executionContexts[world] != nil\n}","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L271-L307","documentation":"newDocumentHandle evaluates the expression 'document' in the frame's main world with returnByValue=false; any evaluation failure is wrapped here. It fires when the execution context k6 was about to use no longer exists — the classic navigation race — or when the frame's target is detached/closed. It is the inner error that 751 wraps.","triggerScenarios":"Evaluating 'document' while the context is destroyed by a navigation; the frame's session closing (page/target shutdown); the frame detached between the cached-handle check and the evaluation.","commonSituations":"Element queries immediately after goto or form submits; SPA route changes between actions; iterating over frames while the page unloads them.","solutions":["Wait for load state after navigations before any query","Retry the query — the recreated context will serve it","Verify the frame is still attached (page.frames()) before using a stored reference","Avoid document-dependent calls during teardown"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  el = await frame.waitForSelector('#x');\n} catch (e) {\n  if (/document element handle|Execution context was destroyed/.test(e.message)) {\n    await page.waitForLoadState();\n    el = await frame.waitForSelector('#x');\n  } else throw e;\n}","preventionTips":["Wait for load state after navigations before any query","Verify a stored frame is still in page.frames() before using it","Avoid document-dependent calls during page teardown"],"tags":["browser","frame","execution-context","navigation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}