{"record":{"id":"6cc7e1d4b7a1db30","repo":"grafana/k6","slug":"getting-page-title-w","errorCode":null,"errorMessage":"getting page title: %w","messagePattern":"getting page title: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/page.go","lineNumber":1799,"sourceCode":"\tp.logger.Debugf(\"Page:TextContent\", \"sid:%v selector:%s\", p.sessionID(), selector)\n\n\treturn p.MainFrame().TextContent(selector, popts)\n}\n\n// Timeout will return the default timeout or the one set by the user.\n// It's an internal method not to be exposed as a JS API.\nfunc (p *Page) Timeout() time.Duration {\n\treturn p.defaultTimeout()\n}\n\n// Title returns the page title.\nfunc (p *Page) Title() (string, error) {\n\tp.logger.Debugf(\"Page:Title\", \"sid:%v\", p.sessionID())\n\n\tjs := `() => document.title`\n\tv, err := p.Evaluate(js)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"getting page title: %w\", err)\n\t}\n\ts, ok := v.(string)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"getting page title: expected string, got %T\", v)\n\t}\n\n\treturn s, nil\n}\n\n// ThrottleCPU will slow the CPU down from chrome's perspective to simulate\n// a test being run on a slower device.\nfunc (p *Page) ThrottleCPU(cpuProfile CPUProfile) error {\n\tp.logger.Debugf(\"Page:ThrottleCPU\", \"sid:%v\", p.sessionID())\n\n\tp.frameSessionsMu.RLock()\n\tdefer p.frameSessionsMu.RUnlock()\n\n\tfor _, fs := range p.frameSessions {","sourceCodeStart":1781,"sourceCodeEnd":1817,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/page.go#L1781-L1817","documentation":"Thrown by Page.Title() when evaluating the JavaScript expression `() => document.title` in the page fails. The evaluation runs through Frame.Evaluate, so any failure in the JS execution context (page navigating, execution context destroyed, page crashed, session gone) is wrapped as 'getting page title'. The page title itself is not validated here — only the ability to run the expression.","triggerScenarios":"Calling page.title() while the page is navigating (the isolated/main world execution context is being replaced), after the page or browser has crashed or closed, or when the CDP session is detached. Also fires if navigation interrupts evaluation before the timeout elapses.","commonSituations":"Reading the title immediately after page.goto() without waiting for the lifecycle event; calling page.title() after a download or redirect swapped the document; browser OOM/crash in CI containers; interacting with a page whose renderer was killed.","solutions":["Wait for a lifecycle event after navigation before reading the title: await page.waitForLoadState('load').","Confirm the page is not closed before calling page.title().","Inspect the wrapped error — 'execution context destroyed' means retry after navigation settles; 'target closed' means the page is gone.","Increase action/timeout defaults with page.setDefaultTimeout() if slow pages cause context churn.","If the renderer crashed, fix the environment (memory, shm) rather than the call site."],"exampleFix":"// before\nawait page.goto(url);\nconst title = page.title(); // may race navigation\n\n// after\nawait page.goto(url);\nawait page.waitForLoadState('load');\nconst title = page.title();","handlingStrategy":"retry","validationCode":"if (page.isClosed()) throw new Error('page closed');\nawait page.waitForLoadState('load'); // settle navigation before reading title","typeGuard":null,"tryCatchPattern":"let title;\nfor (let i = 0; i < 3; i++) {\n  try { title = page.title(); break; }\n  catch (e) {\n    if (/context destroyed/i.test(String(e)) && i < 2) { page.waitForTimeout(500); continue; }\n    throw e;\n  }\n}","preventionTips":["Wait for a lifecycle event after goto before reading the title.","Treat 'execution context destroyed' as transient and retry once.","Avoid calling page APIs after close()."],"tags":["k6","browser","page-title","evaluation","cdp"],"backgroundTag":"execution-context-destroyed","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"}