{"record":{"id":"1c9db2945e127ddd","repo":"jackwener/OpenCLI","slug":"gmail-operation-could-not-set-the-search-query","errorCode":null,"errorMessage":"Gmail ${operation} could not set the search query exactly","messagePattern":"Gmail (.+?) could not set the search query exactly","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/gmail/utils.js","lineNumber":467,"sourceCode":"    };\n  }).map(({ dateText: _dateText, ...row }) => row);\n}\n\nasync function submitSearch(page, query, operation) {\n  const prepared = unwrapBrowserResult(await page.evaluate(`() => {\n    const field = document.querySelector('input[name=\"q\"]');\n    if (!field) return false;\n    field.focus();\n    field.select();\n    return true;\n  }`), `${operation} search preparation`);\n  if (prepared !== true) throw new CommandExecutionError(`Gmail ${operation} could not find the search input`);\n  if (typeof page.nativeType !== 'function') {\n    throw new CommandExecutionError(`Gmail ${operation} requires native browser input`);\n  }\n  await page.nativeType(query);\n  const actual = unwrapBrowserResult(await page.evaluate(`() => document.querySelector('input[name=\"q\"]')?.value || ''`), `${operation} search input verification`);\n  if (actual !== query) throw new CommandExecutionError(`Gmail ${operation} could not set the search query exactly`);\n  if (typeof page.cdp === 'function') {\n    const key = { key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13, nativeVirtualKeyCode: 13 };\n    await page.cdp('Input.dispatchKeyEvent', { type: 'rawKeyDown', ...key });\n    await page.cdp('Input.dispatchKeyEvent', { type: 'keyUp', ...key });\n  } else if (typeof page.nativeKeyPress === 'function') {\n    await page.nativeKeyPress('Enter');\n  } else {\n    throw new CommandExecutionError(`Gmail ${operation} requires native browser keyboard input`);\n  }\n}\n\nexport async function queryThreads(page, query, { account = 0, limit = DEFAULT_LIMIT } = {}) {\n  const normalizedQuery = cleanString(query);\n  if (!normalizedQuery) throw new ArgumentError('Gmail search query cannot be empty');\n  await ensureGmailReady(page, account, 'thread list');\n  await installGmailCapture(page, account, 'bv', 'thread list');\n\n  const rows = [];","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/gmail/utils.js#L449-L485","documentation":"submitSearch verifies, by re-reading the DOM, that the search input's value exactly equals the requested query; if not it throws this CommandExecutionError. This guards against Gmail silently altering, truncating, or ignoring typed characters (autocorrect, focus loss, IME interference) which would produce wrong search results.","triggerScenarios":"page.nativeType(query) ran but document.querySelector('input[name=\"q\"]').value !== query — characters dropped, Gmail reformatted the query (e.g. stripped or auto-completed tokens), or the input lost focus mid-typing.","commonSituations":"Gmail's search autocomplete rewriting the box while typing; special characters or non-ASCII queries mangled by keyboard layout/IME; focus stolen by a Gmail dialog; very long queries hitting an input limit.","solutions":["Simplify the query (avoid characters Gmail rewrites) and retry","Ensure no dialogs/autocomplete popups steal focus; dismiss them or wait for Gmail to settle before searching","Type the query with a small per-keystroke delay so Gmail's autocomplete doesn't race the input","Verify the query round-trips by reading the input value in your own pre-check","Test whether an IME/keyboard layout is altering input and switch to a plain US layout"],"exampleFix":"// before: query rewritten by autocomplete\nawait queryThreads(page, 'subject:(hello world)');\n// after: quote/simplify and retry with settled focus\nawait page.sleep(1);\nawait queryThreads(page, 'subject:\"hello world\"');","handlingStrategy":"validation","validationCode":"// pre-check the query survives typing: avoid chars Gmail rewrites\nconst safe = /^[\\w:@<>\"{} ]+$/.test(query);\nif (!safe) throw new Error('query contains characters Gmail may rewrite; quote or simplify it');","typeGuard":null,"tryCatchPattern":"try {\n  const threads = await queryThreads(page, rawQuery);\n} catch (e) {\n  if (/could not set the search query exactly/.test(e.message)) {\n    await page.sleep(1); // let autocomplete settle, then retry quoted\n    return queryThreads(page, JSON.stringify(rawQuery));\n  }\n  throw e;\n}","preventionTips":["Quote complex search terms so Gmail doesn't rewrite them","Add typing delays to beat autocomplete races","Disable/avoid IMEs or layouts that alter keystrokes","Keep focus on the search box; dismiss competing dialogs"],"tags":["browser-automation","input","gmail","verification"],"backgroundTag":"input-mismatch","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}