{"record":{"id":"f508e2fb0ffd50d2","repo":"grafana/k6","slug":"evaluating-script-on-document-w","errorCode":null,"errorMessage":"evaluating script on document: %w","messagePattern":"evaluating script on document: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":639,"sourceCode":"}\n\nfunc (p *Page) didClose() {\n\tp.logger.Debugf(\"Page:didClose\", \"sid:%v\", p.sessionID())\n\n\tp.closedMu.Lock()\n\t{\n\t\tp.closed = true\n\t}\n\tp.closedMu.Unlock()\n}\n\nfunc (p *Page) evaluateOnNewDocument(source string) error {\n\tp.logger.Debugf(\"Page:evaluateOnNewDocument\", \"sid:%v\", p.sessionID())\n\n\taction := page.AddScriptToEvaluateOnNewDocument(source)\n\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","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/page.go#L621-L657","documentation":"Failure of the CDP page.addScriptToEvaluateOnNewDocument command when registering a user or internal script to run on every new document. The offending input is the script source; typical causes are a closed session or the browser rejecting the command rather than JS syntax (the source is not executed at registration).","triggerScenarios":"Thrown at internal/js/modules/k6/browser/common/page.go:637 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the page session is alive when calling addInitScript","Keep init scripts reasonably sized","Re-add init scripts after browser context recreation"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}