{"record":{"id":"9f3d770cb448a805","repo":"grafana/k6","slug":"getting-document-element-w","errorCode":null,"errorMessage":"getting document element: %w","messagePattern":"getting document element: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1132,"sourceCode":"\t}\n\n\treturn ok, nil\n}\n\n// OwnerFrame returns the frame containing this element.\nfunc (h *ElementHandle) OwnerFrame() (_ *Frame, rerr error) {\n\tfn := `\n\t\t(node, injected) => {\n\t\t\treturn injected.getDocumentElement(node);\n\t\t}\n\t`\n\topts := evalOptions{\n\t\tforceCallable: true,\n\t\treturnByValue: false,\n\t}\n\tres, err := h.evalWithScript(h.ctx, opts, fn)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting document element: %w\", err)\n\t}\n\tif res == nil {\n\t\treturn nil, errors.New(\"getting document element: nil document\")\n\t}\n\n\tdocumentHandle, ok := res.(*ElementHandle)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"unexpected result type while getting document element: %T\", res)\n\t}\n\tdefer func() {\n\t\tif err := documentHandle.Dispose(); err != nil {\n\t\t\terr = fmt.Errorf(\"disposing document element: %w\", err)\n\t\t\trerr = errors.Join(err, rerr)\n\t\t}\n\t}()\n\n\tif documentHandle.remoteObject.ObjectID == \"\" {\n\t\treturn nil, err","sourceCodeStart":1114,"sourceCodeEnd":1150,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle.go#L1114-L1150","documentation":"Returned by ElementHandle.ownerFrame() when the initial injected-script evaluation fails. OwnerFrame evaluates injected.getDocumentElement(node) with returnByValue:false to walk up to the document element; this error wraps an evaluation failure - typically the execution context being destroyed by an in-flight navigation, a stale/detached handle, or a closed CDP session.","triggerScenarios":"Calling ownerFrame() while the page is navigating (context destroyed); on a handle detached by re-render; on a handle from a previous page; after the browser/page closed or the session dropped.","commonSituations":"Resolving the frame of an element right after a click that triggers navigation; iframes loading content concurrently; slow pages where navigation overlaps the call.","solutions":["Wait for the page to settle first: await page.waitForLoadState('load')","Re-query the handle immediately before ownerFrame()","Retry the call once after load state if a navigation race is likely","Ensure the page and browser are still open"],"exampleFix":"// before\nconst frame = await h.ownerFrame();\n// after\nawait page.waitForLoadState('load');\nconst el = await page.$('#in-frame'); // fresh handle\nconst frame = await el.ownerFrame();","handlingStrategy":"retry","validationCode":"await page.waitForLoadState('load');\nconst el = await page.$('#in-frame');\nif (el) {\n  const frame = await el.ownerFrame();\n}","typeGuard":null,"tryCatchPattern":"async function safeOwnerFrame(page, sel) {\n  for (let i = 0; i < 2; i++) {\n    try {\n      const el = await page.$(sel);\n      return await el.ownerFrame();\n    } catch (e) {\n      if (!String(e).includes('getting document element')) throw e;\n      await page.waitForLoadState('load');\n    }\n  }\n  throw new Error('ownerFrame failed after retry');\n}","preventionTips":["Call ownerFrame() only after the page and iframes settle (waitForLoadState)","Use a fresh handle each attempt","Avoid calling it concurrently with actions that navigate"],"tags":["browser","element-handle","owner-frame","navigation","detached-handle"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}