grafana/k6 · error

getting page title

Error message

getting page title: %w

What it means

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.

Solutions

  1. Wait for a lifecycle event after navigation before reading the title: await page.waitForLoadState('load').
  2. Confirm the page is not closed before calling page.title().
  3. Inspect the wrapped error — 'execution context destroyed' means retry after navigation settles; 'target closed' means the page is gone.
  4. Increase action/timeout defaults with page.setDefaultTimeout() if slow pages cause context churn.
  5. If the renderer crashed, fix the environment (memory, shm) rather than the call site.

Example fix

// before
await page.goto(url);
const title = page.title(); // may race navigation

// after
await page.goto(url);
await page.waitForLoadState('load');
const title = page.title();
Defensive patterns

Strategy: retry

Validate before calling

if (page.isClosed()) throw new Error('page closed');
await page.waitForLoadState('load'); // settle navigation before reading title

Try / catch

let title;
for (let i = 0; i < 3; i++) {
  try { title = page.title(); break; }
  catch (e) {
    if (/context destroyed/i.test(String(e)) && i < 2) { page.waitForTimeout(500); continue; }
    throw e;
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/6cc7e1d4b7a1db30. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/page.go:1799

	p.logger.Debugf("Page:TextContent", "sid:%v selector:%s", p.sessionID(), selector)

	return p.MainFrame().TextContent(selector, popts)
}

// Timeout will return the default timeout or the one set by the user.
// It's an internal method not to be exposed as a JS API.
func (p *Page) Timeout() time.Duration {
	return p.defaultTimeout()
}

// Title returns the page title.
func (p *Page) Title() (string, error) {
	p.logger.Debugf("Page:Title", "sid:%v", p.sessionID())

	js := `() => document.title`
	v, err := p.Evaluate(js)
	if err != nil {
		return "", fmt.Errorf("getting page title: %w", err)
	}
	s, ok := v.(string)
	if !ok {
		return "", fmt.Errorf("getting page title: expected string, got %T", v)
	}

	return s, nil
}

// ThrottleCPU will slow the CPU down from chrome's perspective to simulate
// a test being run on a slower device.
func (p *Page) ThrottleCPU(cpuProfile CPUProfile) error {
	p.logger.Debugf("Page:ThrottleCPU", "sid:%v", p.sessionID())

	p.frameSessionsMu.RLock()
	defer p.frameSessionsMu.RUnlock()

	for _, fs := range p.frameSessions {

View on GitHub (pinned to 3fcf5388d7)