jackwener/OpenCLI · error · CommandExecutionError

Could not find search result at index ${index + 1}

Error message

Could not find search result at index ${index + 1}

What it means

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.

Source

Thrown at clis/google-scholar/cite.js:41

        await page.goto(`https://scholar.google.com/scholar?q=${encodeURIComponent(query)}&hl=en`);
        await page.wait(3);

        const clicked = await page.evaluate(`(() => {
            var cites = document.querySelectorAll('a.gs_or_cit');
            if (cites.length <= ${index}) return { ok: false, reason: 'result not found at index ${index + 1}' };
            var titleEl = document.querySelectorAll('.gs_r.gs_or.gs_scl')[${index}];
            var title = '';
            if (titleEl) {
                var t = titleEl.querySelector('.gs_rt a, h3 a');
                title = t ? t.textContent.trim() : '';
            }
            cites[${index}].click();
            return { ok: true, title: title };
        })()`);

        if (!clicked?.ok) {
            throw new CommandExecutionError(clicked?.reason || `Could not find search result at index ${index + 1}`);
        }

        await page.wait(2);

        const formatMap = { bibtex: 'BibTeX', endnote: 'EndNote', refman: 'RefMan', refworks: 'RefWorks' };
        const formatLabel = formatMap[format] || 'BibTeX';

        const citeUrl = await page.evaluate(`(() => {
            var links = document.querySelectorAll('#gs_cit a.gs_citi');
            for (var i = 0; i < links.length; i++) {
                if (links[i].textContent.trim() === '${formatLabel}') return links[i].href;
            }
            return null;
        })()`);

        if (!citeUrl) {
            throw new CommandExecutionError(`Could not find ${formatLabel} citation link for result ${index + 1}`);
        }

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use a smaller/valid index within the returned result count
  2. Catch the error and retry after re-running the search and waiting for results
  3. Check for a CAPTCHA/unusual-traffic page and slow down request rate
  4. Ensure the search step succeeded and wait for results to render before citing

Example fix

// before
await scholarCite(page, 15, 'bibtex'); // assumes 16+ results
// after
const results = await scholarSearch(page, query);
if (15 < results.length) await scholarCite(page, 15, 'bibtex');
else console.warn('result index out of range');
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isInteger(index) || index < 0 || index >= results.length) throw new Error(`index ${index} out of range (0..${results.length - 1})`);

Type guard

const isValidIndex = (i, n) => Number.isInteger(i) && i >= 0 && i < n;

Try / catch

try { await scholarCite(page, i, fmt); }
catch (e) { if (e instanceof CommandExecutionError && /Could not find search result/.test(e.message)) { await page.wait(3); /* retry or skip */ } else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/4b23a967a2fce075. Report an issue: GitHub.