grafana/k6 · error
getting document element: %w
Error message
getting document element: %w
What it means
Returned by ElementHandle.ownerFrame() when the initial injected-script evaluation fails. OwnerFrame evaluates injected.getDocumentElement(node) with returnByValue:false to walk up to the document element; this error wraps an evaluation failure - typically the execution context being destroyed by an in-flight navigation, a stale/detached handle, or a closed CDP session.
Source
Thrown at internal/js/modules/k6/browser/common/element_handle.go:1132
}
return ok, nil
}
// OwnerFrame returns the frame containing this element.
func (h *ElementHandle) OwnerFrame() (_ *Frame, rerr error) {
fn := `
(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, errView on GitHub (pinned to 93accf6570)
Solutions
- Wait for the page to settle first: await page.waitForLoadState('load')
- Re-query the handle immediately before ownerFrame()
- Retry the call once after load state if a navigation race is likely
- Ensure the page and browser are still open
Example fix
// before
const frame = await h.ownerFrame();
// after
await page.waitForLoadState('load');
const el = await page.$('#in-frame'); // fresh handle
const frame = await el.ownerFrame(); Defensive patterns
Strategy: retry
Validate before calling
await page.waitForLoadState('load');
const el = await page.$('#in-frame');
if (el) {
const frame = await el.ownerFrame();
} Try / catch
async function safeOwnerFrame(page, sel) {
for (let i = 0; i < 2; i++) {
try {
const el = await page.$(sel);
return await el.ownerFrame();
} catch (e) {
if (!String(e).includes('getting document element')) throw e;
await page.waitForLoadState('load');
}
}
throw new Error('ownerFrame failed after retry');
} Prevention
- Call ownerFrame() only after the page and iframes settle (waitForLoadState)
- Use a fresh handle each attempt
- Avoid calling it concurrently with actions that navigate
When it happens
Trigger: Calling ownerFrame() while the page is navigating (context destroyed); on a handle detached by re-render; on a handle from a previous page; after the browser/page closed or the session dropped.
Common situations: Resolving the frame of an element right after a click that triggers navigation; iframes loading content concurrently; slow pages where navigation overlaps the call.
Related errors
- disposing document element: %w
- focusing on element: %w
- getting attribute %q of element: %w
- getting element's inner HTML: %w
- getting element's inner text: %w
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/9f3d770cb448a805.
Report an issue: GitHub.