{"record":{"id":"06732cc10e31684e","repo":"grafana/k6","slug":"disposing-document-element-w","errorCode":null,"errorMessage":"disposing document element: %w","messagePattern":"disposing document element: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1144,"sourceCode":"\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\n\t}\n\n\tvar node *cdp.Node\n\taction := dom.DescribeNode().WithObjectID(documentHandle.remoteObject.ObjectID)\n\tif node, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil {\n\t\treturn nil, fmt.Errorf(\"getting node in frame: %w\", err)\n\t}\n\tif node == nil || node.FrameID == \"\" {\n\t\treturn nil, fmt.Errorf(\"no frame found for node: %w\", err)\n\t}\n\n\tframe, ok := h.frame.manager.getFrameByID(node.FrameID)","sourceCodeStart":1126,"sourceCodeEnd":1162,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle.go#L1126-L1162","documentation":"Cleanup error inside ElementHandle.ownerFrame(): the temporary document-element handle acquired during the call could not be disposed in the deferred cleanup, and the error is joined (errors.Join) with the function's return error. Disposal fails when the remote object was already released - most commonly because a navigation destroyed the execution context between evaluation and dispose - or because the CDP session/page is closing.","triggerScenarios":"The page navigates between the getDocumentElement evaluation and the deferred Dispose; the browser context or page is closed while ownerFrame() runs; the remote object was already garbage-collected/released; the CDP session dropped.","commonSituations":"ownerFrame() racing a redirect or form submit; iframe content reloading concurrently; browser shutdown at the end of a scenario while frame resolution is in flight.","solutions":["Stabilize the page before calling: await page.waitForLoadState('load')","Re-query the element handle immediately before ownerFrame()","Treat the failure as transient: retry ownerFrame() once after the page settles","Upgrade k6 - dispose failures are joined so they surface; newer releases handle context destruction more gracefully"],"exampleFix":"// before\nconst f = await h.ownerFrame();\n// after\nawait page.waitForLoadState('load');\nlet f;\ntry {\n  f = await h.ownerFrame();\n} catch (e) {\n  f = await (await page.$('#in-frame')).ownerFrame(); // retry with a fresh handle\n}","handlingStrategy":"try-catch","validationCode":"await page.waitForLoadState('load');\nconst el = await page.$('#in-frame');\nif (el) {\n  const frame = await el.ownerFrame();\n}","typeGuard":null,"tryCatchPattern":"try {\n  frame = await handle.ownerFrame();\n} catch (e) {\n  if (String(e).includes('disposing document element')) {\n    await page.waitForLoadState('load');\n    frame = await (await page.$(sel)).ownerFrame(); // retry with fresh handle\n  } else {\n    throw e;\n  }\n}","preventionTips":["Stabilize the page (waitForLoadState) before frame resolution","Retry ownerFrame once after navigation races - the dispose failure is transient","Do not call ownerFrame while closing the browser context","Keep k6 current to benefit from improved context-destruction handling"],"tags":["browser","element-handle","owner-frame","cleanup","navigation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}