You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Puppeteer报错Execution context was destroyed求助:下拉框选择失败

Fixing "Execution context was destroyed" Error in Puppeteer Dropdown Selection

This error occurs when the page navigates (reloads or redirects) while Puppeteer is trying to interact with the dropdown element. Here are actionable fixes:

  • Ensure the dropdown is fully ready before interaction
    Wait for the dropdown to be visible and interactive before calling page.select:

    // Wait for the dropdown to load and become usable
    await page.waitForSelector('#ClienteMonitor', { visible: true, enabled: true });
    // Now select the target option
    await page.select('#ClienteMonitor', 'VIVO VENDER B2C');
    
  • Handle navigation triggered by dropdown selection
    If choosing an option causes the page to navigate, wrap the selection and navigation wait in Promise.all to avoid context loss:

    await Promise.all([
      page.waitForNavigation(), // Wait for the navigation to complete
      page.select('#ClienteMonitor', 'VIVO VENDER B2C') // Trigger the selection
    ]);
    

    This ensures Puppeteer doesn’t attempt to run code on the old page context once navigation starts.

  • Stabilize the page before interaction
    If the page has pending network requests or auto-reloads, wait for it to stabilize first:

    // Wait for network activity to stop
    await page.waitForNetworkIdle({ idleTime: 1000 });
    // Wait for the dropdown to be visible
    await page.waitForSelector('#ClienteMonitor', { visible: true });
    // Proceed with selection
    await page.select('#ClienteMonitor', 'VIVO VENDER B2C');
    
  • Eliminate race conditions
    If you performed other actions (like clicking buttons) before selecting the dropdown, ensure those actions don’t trigger unhandled navigation. Wait for any resulting network activity or page changes to finish first.

内容的提问来源于stack exchange,提问作者reimannsur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 04:12:10