grafana/k6 · error
unexpected result type while getting document element: %T
Error message
unexpected result type while getting document element: %T
What it means
Internal invariant failure inside ElementHandle.ownerFrame(): the injected getDocumentElement evaluation returned a result, but it was not an *ElementHandle (e.g. null/undefined or a non-node remote object). This indicates the injected script could not resolve a document element from the given node - usually a protocol/version mismatch or a k6 bug rather than script misuse.
Source
Thrown at internal/js/modules/k6/browser/common/element_handle.go:1140
(node, injected) => {
return injected.getDocumentElement(node);
}
`
opts := evalOptions{
forceCallable: true,
returnByValue: false,
}
res, err := h.evalWithScript(h.ctx, opts, fn)
if err != nil {
return nil, fmt.Errorf("getting document element: %w", err)
}
if res == nil {
return nil, errors.New("getting document element: nil document")
}
documentHandle, ok := res.(*ElementHandle)
if !ok {
return nil, fmt.Errorf("unexpected result type while getting document element: %T", res)
}
defer func() {
if err := documentHandle.Dispose(); err != nil {
err = fmt.Errorf("disposing document element: %w", err)
rerr = errors.Join(err, rerr)
}
}()
if documentHandle.remoteObject.ObjectID == "" {
return nil, err
}
var node *cdp.Node
action := dom.DescribeNode().WithObjectID(documentHandle.remoteObject.ObjectID)
if node, err = action.Do(cdp.WithExecutor(h.ctx, h.session)); err != nil {
return nil, fmt.Errorf("getting node in frame: %w", err)
}
if node == nil || node.FrameID == "" {View on GitHub (pinned to 93accf6570)
Solutions
- Upgrade k6 to the latest release
- Pin a Chromium version compatible with your k6 release
- Work around by getting the frame via evaluate on contentWindow/frameElement
- Report to k6 with the script and browser version
Example fix
// before
const f = await h.ownerFrame();
// after
await page.waitForLoadState('load');
const f = await h.ownerFrame();
if (!f) throw new Error('ownerFrame returned no frame'); Defensive patterns
Strategy: try-catch
Validate before calling
await page.waitForLoadState('load');
const el = await page.$('#in-frame');
const frame = el ? await el.ownerFrame() : null;
if (!frame) throw new Error('no frame resolved'); Try / catch
try {
return await handle.ownerFrame();
} catch (e) {
if (String(e).includes('unexpected result type')) {
return null; // signal caller to fall back to evaluate-based frame detection
}
throw e;
} Prevention
- Keep k6 and Chromium versions aligned
- Check the returned frame for null before using it
- Report persistent occurrences to k6 maintainers
When it happens
Trigger: The evaluation returns a non-node value for getDocumentElement - seen with Chromium/CDP versions mismatched to the k6 release, handles on unusual nodes (e.g. already-collected documents), or k6 regressions. Rare in normal use.
Common situations: Canary/newer Chromium against an older k6; custom browser executables; calling ownerFrame on document-level handles.
Related errors
- getting attribute %q of element: unexpected type %T (expecti
- unexpected type %T (expecting string)
- unexpected select element type %T
- evaluating handle of %q: unexpected type %T
- can't fetch the page for unknown reason
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/e63c8166b732fb60.
Report an issue: GitHub.