{"record":{"id":"33998c7cf0b3b7b4","repo":"grafana/k6","slug":"querying-selector-q-w","errorCode":null,"errorMessage":"querying selector %q: %w","messagePattern":"querying selector %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle.go","lineNumber":1226,"sourceCode":"\t\t\treturn nil, err\n\t\t}\n\n\t\treturn frame.Query(afterFrameSelector, strict)\n\t}\n\n\t// No frame navigation - proceed with normal Query logic\n\tquerySelector := `\n\t\t(node, injected, selector, strict) => {\n\t\t\treturn injected.querySelector(selector, strict, node || document);\n\t\t}\n\t`\n\topts := evalOptions{\n\t\tforceCallable: true,\n\t\treturnByValue: false,\n\t}\n\tresult, err := h.evalWithScript(h.ctx, opts, querySelector, parsedSelector, strict)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"querying selector %q: %w\", selector, err)\n\t}\n\tif result == nil {\n\t\treturn nil, nil //nolint:nilnil\n\t}\n\thandle, ok := result.(JSHandleAPI)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"querying selector %q, wrong type %T\", selector, result)\n\t}\n\telement := handle.AsElement()\n\tif element == nil {\n\t\tdefer func() {\n\t\t\tif err := handle.Dispose(); err != nil {\n\t\t\t\terr = fmt.Errorf(\"disposing element handle: %w\", err)\n\t\t\t\trerr = errors.Join(err, rerr)\n\t\t\t}\n\t\t}()\n\t\treturn nil, fmt.Errorf(\"querying selector %q\", selector)\n\t}","sourceCodeStart":1208,"sourceCodeEnd":1244,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle.go#L1208-L1244","documentation":"ElementHandle.Query() evaluated the injected querySelector script in the element's execution context and the evaluation itself failed (element_handle.go:1226). The wrapped error is a CDP/evaluation error: context destroyed by navigation, target crashed or closed, or the injected script could not run.","triggerScenarios":"Page navigates or the iframe's context is destroyed between the call and evaluation; browser/tab closed mid-call; page crashes under load; the element's frame is cross-origin and being swapped.","commonSituations":"Querying immediately after a click that triggers an SPA route change; k6 tears the browser down while a query promise is pending; flaky under concurrency.","solutions":["Await page.waitForLoadState() after any action that can navigate before querying","Re-acquire the ElementHandle (its context died with the old document) and retry the query","Wrap the call in try/catch with a single retry — these failures are usually transient races","Check the browser/process logs if it correlates with resource exhaustion (target crash)"],"exampleFix":"// before\nawait page.click('a.details');\nconst row = await page.$('.row'); // context may be mid-navigation\n// after\nawait page.click('a.details');\nawait page.waitForLoadState('domcontentloaded');\nconst row = await page.$('.row');","handlingStrategy":"try-catch","validationCode":"// Stabilize before querying\nawait page.waitForLoadState('domcontentloaded');\nif (!(await el.isVisible())) throw new Error('context likely navigated; re-query element');","typeGuard":null,"tryCatchPattern":"try { return await el.$(sel); }\ncatch (e) {\n  if (/querying selector/.test(e.message) && /context|target|navigation/i.test(e.message)) {\n    await page.waitForLoadState(); return await el.$(sel); // single retry\n  }\n  throw e;\n}","preventionTips":["Await load state after navigating actions","Expect one-shot races; retry once, not forever","Don't hold handles across navigations"],"tags":["browser","evaluation","navigation","race-condition"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}