{"record":{"id":"4b23a967a2fce075","repo":"jackwener/OpenCLI","slug":"could-not-find-search-result-at-index-index-1","errorCode":null,"errorMessage":"Could not find search result at index ${index + 1}","messagePattern":"Could not find search result at index (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/google-scholar/cite.js","lineNumber":41,"sourceCode":"\n        await page.goto(`https://scholar.google.com/scholar?q=${encodeURIComponent(query)}&hl=en`);\n        await page.wait(3);\n\n        const clicked = await page.evaluate(`(() => {\n            var cites = document.querySelectorAll('a.gs_or_cit');\n            if (cites.length <= ${index}) return { ok: false, reason: 'result not found at index ${index + 1}' };\n            var titleEl = document.querySelectorAll('.gs_r.gs_or.gs_scl')[${index}];\n            var title = '';\n            if (titleEl) {\n                var t = titleEl.querySelector('.gs_rt a, h3 a');\n                title = t ? t.textContent.trim() : '';\n            }\n            cites[${index}].click();\n            return { ok: true, title: title };\n        })()`);\n\n        if (!clicked?.ok) {\n            throw new CommandExecutionError(clicked?.reason || `Could not find search result at index ${index + 1}`);\n        }\n\n        await page.wait(2);\n\n        const formatMap = { bibtex: 'BibTeX', endnote: 'EndNote', refman: 'RefMan', refworks: 'RefWorks' };\n        const formatLabel = formatMap[format] || 'BibTeX';\n\n        const citeUrl = await page.evaluate(`(() => {\n            var links = document.querySelectorAll('#gs_cit a.gs_citi');\n            for (var i = 0; i < links.length; i++) {\n                if (links[i].textContent.trim() === '${formatLabel}') return links[i].href;\n            }\n            return null;\n        })()`);\n\n        if (!citeUrl) {\n            throw new CommandExecutionError(`Could not find ${formatLabel} citation link for result ${index + 1}`);\n        }","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/google-scholar/cite.js#L23-L59","documentation":"In google-scholar/cite.js, clicking the Nth result's Cite link is done inside page.evaluate which returns {ok, reason} or a falsy value; if the click did not succeed the code throws CommandExecutionError `Could not find search result at index ${index+1}` (or the page's own reason). The index is 0-based internally but reported 1-based to the user.","triggerScenarios":"Calling cite with an index beyond the number of results on the page (e.g. index 15 when only 8 results rendered); Scholar showing a CAPTCHA/error page; results not yet loaded before evaluate runs.","commonSituations":"Scripting citation downloads for a long list of indices without checking result count; rate limiting leading to an 'unusual traffic' page with zero results; slow load causing evaluate to run before results render.","solutions":["Use a smaller/valid index within the returned result count","Catch the error and retry after re-running the search and waiting for results","Check for a CAPTCHA/unusual-traffic page and slow down request rate","Ensure the search step succeeded and wait for results to render before citing"],"exampleFix":"// before\nawait scholarCite(page, 15, 'bibtex'); // assumes 16+ results\n// after\nconst results = await scholarSearch(page, query);\nif (15 < results.length) await scholarCite(page, 15, 'bibtex');\nelse console.warn('result index out of range');","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(index) || index < 0 || index >= results.length) throw new Error(`index ${index} out of range (0..${results.length - 1})`);","typeGuard":"const isValidIndex = (i, n) => Number.isInteger(i) && i >= 0 && i < n;","tryCatchPattern":"try { await scholarCite(page, i, fmt); }\ncatch (e) { if (e instanceof CommandExecutionError && /Could not find search result/.test(e.message)) { await page.wait(3); /* retry or skip */ } else throw e; }","preventionTips":["Cap indices at the number of results actually returned by search","Detect CAPTCHA/unusual-traffic pages and back off","Wait for results to render before issuing cite calls"],"tags":["index-out-of-range","google-scholar","ui-automation"],"backgroundTag":"index-out-of-range","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}