{"record":{"id":"24b1954559d08393","repo":"grafana/k6","slug":"unexpected-end-of-selector-while-parsing-selector","errorCode":null,"errorMessage":"Unexpected end of selector while parsing selector `${selector}`","messagePattern":"Unexpected end of selector while parsing selector `(.+?)`","errorType":"exception","errorClass":"InvalidSelectorError","httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/js/injected_script.js","lineNumber":989,"sourceCode":"\n// packages/playwright-core/src/utils/isomorphic/cssParser.ts\nvar InvalidSelectorError = class extends Error {\n};\n\n// packages/playwright-core/src/utils/isomorphic/selectorParser.ts\nfunction parseAttributeSelector(selector, allowUnquotedStrings) {\n  let wp = 0;\n  let EOL = selector.length === 0;\n  const next = () => selector[wp] || \"\";\n  const eat1 = () => {\n    const result2 = next();\n    ++wp;\n    EOL = wp >= selector.length;\n    return result2;\n  };\n  const syntaxError = (stage) => {\n    if (EOL)\n      throw new InvalidSelectorError(`Unexpected end of selector while parsing selector \\`${selector}\\``);\n    throw new InvalidSelectorError(`Error while parsing selector \\`${selector}\\` - unexpected symbol \"${next()}\" at position ${wp}` + (stage ? \" during \" + stage : \"\"));\n  };\n  function skipSpaces() {\n    while (!EOL && /\\s/.test(next()))\n      eat1();\n  }\n  function isCSSNameChar(char) {\n    return char >= \"\\x80\" || char >= \"0\" && char <= \"9\" || char >= \"A\" && char <= \"Z\" || char >= \"a\" && char <= \"z\" || char >= \"0\" && char <= \"9\" || char === \"_\" || char === \"-\";\n  }\n  function readIdentifier() {\n    let result2 = \"\";\n    skipSpaces();\n    while (!EOL && isCSSNameChar(next()))\n      result2 += eat1();\n    return result2;\n  }\n  function readQuotedString(quote) {\n    let result2 = eat1();","sourceCodeStart":971,"sourceCodeEnd":1007,"githubUrl":"https://github.com/grafana/k6/blob/3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee/internal/js/modules/k6/browser/common/js/injected_script.js#L971-L1007","documentation":"This error is returned by the k6 browser module's Frame evaluation of a handle (used by frame.evaluateHandle and internal handle lookups). It wraps the failure of EvalHandle on the frame's main-world execution context: either the execution context for mainWorld does not exist yet (the frame is navigating, loading, or detached) or the CDP Runtime.callFunctionOn evaluation itself failed. The source shows the code waits for the execution context, tries once, and wraps any failure with 'evaluating handle for frame'.","triggerScenarios":"Calling frame/page evaluateHandle-style APIs while the frame is mid-navigation or still loading (execution context not yet created); calling it right after page.goto() before the load event; calling it on a frame that was detached (iframe removed from DOM or its page closed); a JavaScript exception thrown inside the evaluated page function.","commonSituations":"Scripts that evaluate handles immediately after goto() without waiting for load state; acting on iframes that get removed/replaced by SPA rerenders; races between navigation and evaluation in reactive apps; running against slow CI hosts where context creation lags.","solutions":["Wait for the frame to finish loading before evaluating: await page.waitForLoadState('load') (or frame.waitForLoadState) after goto().","Re-acquire the frame from page.frames() to make sure it is still attached, then retry the evaluate call.","Wrap the call in try/catch and retry once after a short delay to ride out navigation races.","If the inner error says the page function threw, fix the JavaScript you pass to the evaluation."],"exampleFix":"// before\nawait page.goto('https://example.com');\nconst handle = await page.evaluateHandle('document.body'); // may race navigation\n\n// after\nawait page.goto('https://example.com');\nawait page.waitForLoadState('load');\nconst handle = await page.evaluateHandle('document.body');","handlingStrategy":"try-catch","validationCode":"await page.waitForLoadState('load'); // ensure navigation finished before handle evaluation\nconst frames = page.frames();\nif (!frames.some(f => f === page.mainFrame())) throw new Error('frame not available');","typeGuard":null,"tryCatchPattern":"try {\n  const h = await page.evaluateHandle('document.body');\n} catch (e) {\n  if (String(e).includes('evaluating handle for frame')) {\n    await page.waitForLoadState('load');\n    return await page.evaluateHandle('document.body'); // one retry after context settles\n  }\n  throw e;\n}","preventionTips":["Always await waitForLoadState (or a selector) after goto() before evaluate/evaluateHandle.","Never cache frame objects across navigations; re-fetch from page.frames().","Keep evaluations on elements that provably exist (waitForSelector first)."],"tags":["k6","browser","frame","execution-context","cdp"],"backgroundTag":"execution-context-destroyed","analyzedSha":"3fcf5388d78cb382f0c0d42ec556a06bfd1b8dee","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}