{"record":{"id":"d163ea699d5ac8e1","repo":"grafana/k6","slug":"query-w","errorCode":null,"errorMessage":"query: %w","messagePattern":"query: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/frame.go","lineNumber":2360,"sourceCode":"\tEval(apiCtx context.Context, js string, args ...any) (any, error)\n\n\t// EvalHandle evaluates the provided JavaScript within this execution\n\t// context and returns a JSHandle.\n\tEvalHandle(apiCtx context.Context, js string, args ...any) (JSHandleAPI, error)\n\n\t// Frame returns the frame that this execution context belongs to.\n\tFrame() *Frame\n\n\t// id returns the CDP runtime ID of this execution context.\n\tID() runtime.ExecutionContextID\n}\n\nfunc (f *Frame) runActionOnSelector(\n\tctx context.Context, selector string, strict bool, fn elementHandleActionFunc, nullResponder func() bool,\n) (bool, error) {\n\thandle, err := f.Query(selector, strict)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"query: %w\", err)\n\t}\n\tif handle == nil {\n\t\tf.log.Debugf(\"Frame:runActionOnSelector:nilHandler\", \"fid:%s furl:%q selector:%s\", f.ID(), f.URL(), selector)\n\t\treturn nullResponder(), err\n\t}\n\n\tv, err := fn(ctx, handle)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"calling function: %w\", err)\n\t}\n\n\tbv, ok := v.(bool)\n\tif !ok {\n\t\treturn false, fmt.Errorf(\"unexpected type %T\", v)\n\t}\n\n\treturn bv, nil\n}","sourceCodeStart":2342,"sourceCodeEnd":2378,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/frame.go#L2342-L2378","documentation":"Thrown by Frame.runActionOnSelector when f.Query(selector, strict) itself errors before any element action runs. Query failure means the selector could not be evaluated by the browser (invalid selector syntax) or strict mode rejected it (multiple/zero matches surfaced as error rather than nil). The %w carries the precise query error.","triggerScenarios":"page.click('a[href=') with broken syntax; page.focus('#x') matching 2+ nodes with strict mode on; querying in a detached frame. Note this differs from a timeout: it fails fast on the query step, not after waiting.","commonSituations":"Dynamically built selectors with unescaped quotes/brackets; generic selectors like 'div > button' used on pages with repeated component instances; operating on a frame removed during SPA rerenders.","solutions":["Paste the selector into DevTools document.querySelectorAll(...) to confirm it parses and matches exactly one node","Make the selector unique (add :nth-of-type, an id, or a data-test attribute)","Pass strict: false where clicking any of several matches is acceptable"],"exampleFix":"// before\nawait page.click('div > button'); // strict: 3 matches -> query error\n\n// after\nawait page.click('div.card--primary > button');","handlingStrategy":"validation","validationCode":"const n = await page.evaluate((s) => document.querySelectorAll(s).length, sel);\nif (n !== 1) throw new Error(`${sel} matches ${n} nodes; expected exactly 1`);","typeGuard":"function isQueryError(e) {\n  return e instanceof Error && /^query:/.test(e.message);\n}","tryCatchPattern":null,"preventionTips":["Check selector uniqueness before actions in setup helpers","Build selectors from stable data- attributes"],"tags":["browser","selector","strict-mode","query"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}