{"record":{"id":"e2840528bc764522","repo":"grafana/k6","slug":"frame-has-been-detached-1","errorCode":null,"errorMessage":"frame has been detached 1","messagePattern":"frame has been detached 1","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":655,"sourceCode":"\t_, err := action.Do(cdp.WithExecutor(p.ctx, p.session))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"evaluating script on document: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (p *Page) getFrameElement(f *Frame) (handle *ElementHandle, _ error) {\n\tif f == nil {\n\t\tp.logger.Debugf(\"Page:getFrameElement\", \"sid:%v frame:nil\", p.sessionID())\n\t} else {\n\t\tp.logger.Debugf(\"Page:getFrameElement\", \"sid:%v fid:%s furl:%s\",\n\t\t\tp.sessionID(), f.ID(), f.URL())\n\t}\n\n\tparent := f.parentFrame\n\tif parent == nil {\n\t\treturn nil, errors.New(\"frame has been detached 1\")\n\t}\n\n\trootFrame := f\n\tfor ; rootFrame.parentFrame != nil; rootFrame = rootFrame.parentFrame {\n\t}\n\n\tparentSession, ok := p.getFrameSession(cdp.FrameID(rootFrame.ID()))\n\tif !ok {\n\t\treturn nil, errors.New(\"parent frame has been detached\")\n\t}\n\n\taction := dom.GetFrameOwner(cdp.FrameID(f.ID()))\n\tbackendNodeID, _, err := action.Do(cdp.WithExecutor(p.ctx, parentSession.session))\n\tif err != nil {\n\t\tif strings.Contains(err.Error(), \"frame with the given id was not found\") {\n\t\t\treturn nil, errors.New(\"frame has been detached\")\n\t\t}\n\t\treturn nil, fmt.Errorf(\"getting frame owner: %w\", err)","sourceCodeStart":637,"sourceCodeEnd":673,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/page.go#L637-L673","documentation":"Page.getFrameElement (page.go:643-653) resolves the <iframe>/<frame> DOM element that owns a frame (used by frame.frameElement()). It first needs the frame's parent; if parentFrame == nil the frame either is the main frame or has been detached, so there is no parent in which to look for the frame element.","triggerScenarios":"Calling page.mainFrame().frameElement() (main frame has no parent); calling frameElement() on an iframe frame that was removed from the DOM by navigation or script while you still held the reference.","commonSituations":"Iterating page.frames() while navigation adds/removes iframes; holding frame references across page.goto; ad or embed iframes deleted by their own scripts.","solutions":["Never call frameElement() on the main frame — it has no owner element by definition","Re-fetch frames from page.frames() after each navigation and use those fresh references","Before calling, confirm the frame is still live (its id still appears in page.frames())","Wrap in try-catch and re-acquire the frame on failure; a detached frame never comes back"],"exampleFix":"// before\nconst el = await page.frames()[0].frameElement(); // may be detached\n\n// after\nconst frames = page.frames();\nconst live = frames.find(f => f.name() === 'chart');\nif (live) { const el = await live.frameElement(); }","handlingStrategy":"validation","validationCode":"// Skip the main frame and confirm the frame is still listed\nconst f = page.frames().find(fr => fr.name() === 'widget');\nif (f) {\n  const el = await f.frameElement();\n} else {\n  // frame gone — re-acquire or skip\n}","typeGuard":null,"tryCatchPattern":"try {\n  handle = await frame.frameElement();\n} catch (e) {\n  if (/detached/.test(e.message)) {\n    const fresh = page.frames().find(fr => fr.name() === 'widget');\n    handle = fresh ? await fresh.frameElement() : null;\n  } else throw e;\n}","preventionTips":["Never call frameElement() on the main frame","Re-fetch frames from page.frames() after every navigation","Treat 'frame has been detached' as 're-acquire the frame' in all handlers"],"tags":["browser","iframe","frame","detached"],"backgroundTag":"frame-detached","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"}