jackwener/OpenCLI · error · CliError

SELECTOR

SELECTOR

Error message

Could not find element: window.lib.mtop

What it means

selectorError thrown when the in-page mtop bridge (window.lib.mtop) is unavailable during a Xianyu search, mapped from the evaluate result error 'mtop-not-ready'. Same family as 5432 but in the search flow (clis/xianyu/search.js).

Source

Thrown at clis/xianyu/search.js:231

        if (minPrice != null && maxPrice != null && minPrice > maxPrice) {
            throw new ArgumentError('xianyu search min-price cannot be greater than max-price', `Received --min-price ${minPrice} and --max-price ${maxPrice}`);
        }
        const province = String(kwargs.province || '').trim();
        const city = String(kwargs.city || '').trim();
        const searchFilter = buildSearchFilter(minPrice, maxPrice);
        const extraFilterValue = buildExtraFilterValue(province, city);
        const fromFilter = Boolean(searchFilter) || extraFilterValue !== '{}';
        await page.goto(buildSearchUrl(query));
        await page.wait(2);
        const result = await page.evaluate(buildSearchEvaluate({ keyword: query, searchFilter, extraFilterValue, fromFilter, maxItems: limit }));
        if (result?.error === 'auth-required') {
            throw new AuthRequiredError('www.goofish.com', 'Xianyu search requires a logged-in browser session');
        }
        if (result?.error === 'blocked') {
            throw new CommandExecutionError('Xianyu returned a verification page or blocked the current browser session');
        }
        if (result?.error === 'mtop-not-ready') {
            throw selectorError('window.lib.mtop', '闲鱼页面未完成初始化,无法调用搜索接口');
        }
        if (!result || typeof result !== 'object') {
            throw new CommandExecutionError('Xianyu search returned a malformed response');
        }
        const errorCode = String(result?.error_code || '');
        const errorMessage = String(result?.error_message || '');
        if (/FAIL_SYS_SESSION_EXPIRED|SESSION_EXPIRED|FAIL_SYS_TOKEN/.test(errorCode) || /FAIL_SYS_SESSION_EXPIRED|SESSION_EXPIRED/.test(errorMessage)) {
            throw new AuthRequiredError('www.goofish.com', 'Xianyu search requires a logged-in browser session');
        }
        if (result?.error) {
            throw new CommandExecutionError(errorMessage || `Xianyu search request failed: ${result.error}`);
        }
        if (!Array.isArray(result.items)) {
            throw new CommandExecutionError('Xianyu search response did not include an items array');
        }
        const items = result.items;
        if (!items.length) {
            throw new EmptyResultError('xianyu search', '没有匹配的商品(筛选条件可能过窄,或当前关键词无结果)');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Wait for window.lib.mtop to exist (waitForFunction) before running the search evaluate
  2. Reload the search page and retry after initialization
  3. Handle any verification/blocked interstitial that prevents JS bootstrap
  4. Update the evaluate's mtop access path if Goofish changed internals

Example fix

// before
await opencli xianyu search --q 'iphone'
// after: warm up the page first
await page.waitForFunction(() => window.lib && window.lib.mtop);
await opencli xianyu search --q 'iphone'
Defensive patterns

Strategy: retry

Validate before calling

await page.waitForFunction(() => window.lib && typeof window.lib.mtop === 'object', { timeout: 15000 });

Type guard

const mtopReady = () => page.evaluate(() => Boolean(window.lib && window.lib.mtop));

Try / catch

try {
  await opencli xianyu search --q 'iphone';
} catch (e) {
  if (e.code === 'SELECTOR' && e.message.includes('window.lib.mtop')) {
    await page.reload();
    await page.waitForFunction(() => window.lib && window.lib.mtop);
    await opencli xianyu search --q 'iphone';
  } else throw e;
}

Prevention

When it happens

Trigger: Running a Xianyu search while the Goofish page has not initialized window.lib.mtop, so the search API call cannot be dispatched.

Common situations: Search page still bootstrapping due to slow network; anti-bot verification page intercepted JS init; Goofish changed the mtop global path.

Related errors


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