{"record":{"id":"1d35c34873d04e74","repo":"grafana/k6","slug":"scrolling-element-into-view-w-1d35c3","errorCode":null,"errorMessage":"scrolling element into view: %w","messagePattern":"scrolling element into view: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/screenshotter.go","lineNumber":281,"sourceCode":"\t\t\t\t\"This is non-standard behavior, if possible please report this issue (with a reproducible script) \"+\n\t\t\t\t\"to the https://github.com/grafana/k6/issues/new.\",\n\t\t\tvisualViewportScale, visualViewportPageX, visualViewportPageY,\n\t\t)\n\t}\n\n\treturn visualViewportScale, visualViewportPageX, visualViewportPageY, nil\n}\n\nfunc (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleScreenshotOptions) ([]byte, error) {\n\tformat := opts.Format\n\tviewportSize, originalViewportSize, err := s.originalViewportSize(h.frame.page)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting original viewport size: %w\", err)\n\t}\n\n\terr = h.waitAndScrollIntoViewIfNeeded(h.ctx, false, true, opts.Timeout)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"scrolling element into view: %w\", err)\n\t}\n\n\tbbox, err := h.boundingBox()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"node is either not visible or not an HTMLElement: %w\", err)\n\t}\n\tif bbox.Width <= 0 {\n\t\treturn nil, fmt.Errorf(\"node has 0 width\")\n\t}\n\tif bbox.Height <= 0 {\n\t\treturn nil, fmt.Errorf(\"node has 0 height\")\n\t}\n\n\tvar overriddenViewportSize *Size\n\tfitsViewport := bbox.Width <= viewportSize.Width && bbox.Height <= viewportSize.Height\n\tif !fitsViewport { //nolint:nestif\n\t\toverriddenViewportSize = Size{\n\t\t\tWidth:  math.Max(viewportSize.Width, bbox.Width),","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/screenshotter.go#L263-L299","documentation":"Before capturing an element, the screenshotter scrolls it into view and waits for it to be visible/stable (waitAndScrollIntoViewIfNeeded). This error wraps that wait failing — typically a timeout waiting for the element to become visible, the element becoming detached, or the execution context being destroyed by navigation.","triggerScenarios":"elementHandle.screenshot() where the element is display:none/visibility:hidden/offscreen-unscrollable, is removed from the DOM during the wait, the selector resolved to a stale handle, or the page navigates while waiting.","commonSituations":"Screenshotting lazy-rendered widgets (charts, carousels) before they render; elements inside collapsed accordions/tabs; SPA route changes invalidating the handle; default timeout too short on slow CI.","solutions":["Wait first: await locator.waitFor({ state: 'visible', timeout: 30000 }) before screenshot() (or pass a larger timeout option to screenshot).","Expand/scroll the container in user code so the element is actually visible.","Re-query the element close to the screenshot to avoid stale handles in SPAs.","If the page navigates on a timer, block that navigation or screenshot sooner."],"exampleFix":"// before\nawait page.$('#chart').then(el => el.screenshot({ path: 'c.png' })); // chart not rendered yet\n\n// after\nconst chart = page.locator('#chart');\nawait chart.waitFor({ state: 'visible', timeout: 30000 });\nawait chart.screenshot({ path: 'c.png' });","handlingStrategy":"validation","validationCode":"const loc = page.locator('#target');\nawait loc.waitFor({ state: 'visible', timeout: 30000 });","typeGuard":"async function isVisibleAndAttached(loc) {\n  return (await loc.count()) > 0 && await loc.isVisible();\n}","tryCatchPattern":"try { await el.screenshot({ path: 'e.png' }); }\ncatch (e) { if (String(e).includes('scrolling element into view')) { /* wait for visible, then retry */ } else throw e; }","preventionTips":["waitFor({state:'visible'}) before every element screenshot.","Pass a generous timeout option on slow CI.","Expand collapsed containers before capturing their content."],"tags":["k6","browser","screenshot","element-handle","visibility","timeout"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}