grafana/k6 · error
getting document: %w
Error message
getting document: %w
What it means
Frame.count() first resolves the frame's document element handle via f.document(). That helper waits for the main-world execution context and creates a document handle over CDP; if that fails (execution context destroyed, frame navigating/detached), count wraps the failure as 'getting document'.
Source
Thrown at internal/js/modules/k6/browser/common/frame.go:636
click := func(apiCtx context.Context, handle *ElementHandle, p *Position) (any, error) {
return nil, handle.click(p, opts.ToMouseClickOptions())
}
act := f.newPointerAction(
selector, DOMElementStateAttached, opts.Strict, click, &opts.ElementHandleBasePointerOptions,
)
if _, err := call(f.ctx, act, opts.Timeout); err != nil {
return errorFromDOMError(err)
}
return nil
}
func (f *Frame) count(selector string) (int, error) {
f.log.Debugf("Frame:count", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)
document, err := f.document()
if err != nil {
return 0, fmt.Errorf("getting document: %w", err)
}
c, err := document.count(f.ctx, selector)
if err != nil {
return 0, fmt.Errorf("counting elements: %w", err)
}
return c, nil
}
// Check clicks the first element found that matches selector.
func (f *Frame) Check(selector string, popts *FrameCheckOptions) error {
f.log.Debugf("Frame:Check", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)
if err := f.check(selector, popts); err != nil {
return fmt.Errorf("checking %q: %w", selector, err)
}
View on GitHub (pinned to 93accf6570)
Solutions
- Wait for the page/frame to settle: await page.waitForLoadState('load') (or waitForNavigation) before count().
- Retry count() once after a short delay — execution contexts are recreated after navigation.
- If targeting an iframe, get the frame via page.frames() and ensure it is attached before counting.
Example fix
// before
const n = page.frame('shop').count('.item');
// after
const fr = page.frame('shop');
await fr.waitForSelector('.item', { state: 'attached', timeout: 30000 });
const n = fr.count('.item'); Defensive patterns
Strategy: retry
Validate before calling
await page.waitForLoadState('load');
const fr = page.frames().find((f) => f.name() === 'shop');
if (!fr) throw new Error('frame not attached yet'); Try / catch
let n;
for (let i = 0; i < 3; i++) {
try { n = frame.count(sel); break; } catch (e) { await sleep(500); }
}
if (n === undefined) throw new Error('count failed after retries'); Prevention
- Never call count() mid-navigation; wait for load state.
- For iframes, resolve the frame object after it attaches.
- Structure tests so goto+wait happen before any DOM query.
When it happens
Trigger: Calling frame.count(selector) while the frame is navigating (context destroyed and not yet recreated), on a detached iframe, or when the main-world execution context has not appeared — document() returns errors like 'getting new document handle: ...' or 'waiting for selector %q: execution context %q not found'.
Common situations: Counting elements immediately after page.goto() before load settles; counting inside an iframe during its load; test running against a page that redirects; slow CI machines where context creation exceeds waits.
Related errors
- getting document element handle: %w
- setting content: %w
- getting frame title: %w
- missing required argument 'url'
- getting injected script: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/c7d822755cdac996.
Report an issue: GitHub.