{"record":{"id":"2f62596a87f5e76f","repo":"jackwener/OpenCLI","slug":"gmail-operation-could-not-find-the-search-input","errorCode":null,"errorMessage":"Gmail ${operation} could not find the search input","messagePattern":"Gmail (.+?) could not find the search input","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/gmail/utils.js","lineNumber":461,"sourceCode":"    const parsedDate = row.dateText ? new Date(row.dateText) : null;\n    return {\n      ...row,\n      threadId: cleanString(target).replace(/^#/, ''),\n      date: parsedDate && !Number.isNaN(parsedDate.getTime()) ? parsedDate.toISOString() : null,\n      body: cleanString(row.body).slice(0, MAX_BODY_CHARS) || null,\n    };\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 } = {}) {","sourceCodeStart":443,"sourceCodeEnd":479,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/gmail/utils.js#L443-L479","documentation":"submitSearch throws this CommandExecutionError when the in-page evaluate focused/selecting input[name=\"q\"] returns false — i.e. the search input that ensureGmailReady saw earlier has disappeared by the time the search runs. The library aborts rather than typing into a nonexistent field.","triggerScenarios":"queryThreads or listLabels calls submitSearch after readiness, but Gmail re-rendered the DOM (view switch, navigation, load race) removing the search box between the readiness probe and the type step.","commonSituations":"Gmail auto-navigated or showed a dialog/interstitial right after load; slow render where the probe saw an early DOM then it was replaced; running multiple operations concurrently on the same page causing navigation races.","solutions":["Retry the command; the race usually resolves on a second attempt after Gmail settles","Reload Gmail and wait for full load before running the operation","Avoid running concurrent Gmail operations on the same page","Serialize operations with a small delay after page load so Gmail finishes initial rendering"],"exampleFix":"// before: fire immediately after readiness\nawait queryThreads(page, 'in:unread');\n// after: settle, then retry-once on failure\nawait page.sleep(2);\ntry {\n  await queryThreads(page, 'in:unread');\n} catch (e) {\n  await page.reload(); await page.sleep(3);\n  await queryThreads(page, 'in:unread');\n}","handlingStrategy":"retry","validationCode":"const present = await page.evaluate('() => !!document.querySelector(\"input[name=q]\")');\nif (!present) throw new Error('Gmail search box missing; reload and let the UI settle before searching');","typeGuard":null,"tryCatchPattern":"try {\n  await queryThreads(page, 'is:starred');\n} catch (e) {\n  if (/could not find the search input/.test(e.message)) {\n    await page.reload(); await page.sleep(3); // let Gmail finish rendering\n    return queryThreads(page, 'is:starred');\n  }\n  throw e;\n}","preventionTips":["Wait for full Gmail render before operations; avoid races right after load","Never run concurrent Gmail operations on the same page","Dismiss dialogs/interstitials that displace the search box","Retry once on this race — it usually clears after the UI settles"],"tags":["browser-automation","dom","gmail","race-condition"],"backgroundTag":"element-not-found","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}