【问题标题】:puppeteer page.evaluate() randomly fails with: Execution context was destroyed, most likely because of a navigationpuppeteer page.evaluate() 随机失败:执行上下文被破坏,很可能是因为导航
【发布时间】:2019-08-27 11:52:18
【问题描述】:

以下代码随机失败:

执行上下文被破坏,很可能是因为导航。

这是为什么呢? 有什么解决方法吗?

我使用 puppeteer 版本 1.19.0

注意:我正在寻找一种通用的解决方案,它允许导航到有重定向的页面和没有重定向的页面。

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch();

  try {
    const page = await browser.newPage();

    await page.setCacheEnabled(false);

    const response = await page.goto("https://docs.cypress.io/", {
      waitUntil: "networkidle0",
      timeout: 60000
    });

    const pageUrls = await page.evaluate(() => {
      const links = Array.from(document.querySelectorAll("a"));

      return links.map(link => link.href);
    });

    console.log({ pageUrls });
  } catch (error) {
    console.log(error.message);
  }

  await browser.close();
})();


【问题讨论】:

    标签: node.js puppeteer serverless


    【解决方案1】:

    作为解决方法 - 添加以下代码

    await page.waitForNavigation()

    调用 page.goto() 或者如果 page.click() 出现同样的问题,您可以使用上述方法等待导航。

    const puppeteer = require("puppeteer");
    
    (async () => {
      const browser = await puppeteer.launch();
    
      try {
        const page = await browser.newPage();
    
        await page.setCacheEnabled(false);
    
        const response = await page.goto("https://docs.cypress.io/", {
          waitUntil: "networkidle0",
          timeout: 60000
        });
    
        await page.waitForNavigation();
    
        const pageUrls = await page.evaluate(() => {
          const links = Array.from(document.querySelectorAll("a"));
    
          return links.map(link => link.href);
        });
    
        console.log({ pageUrls });
      } catch (error) {
        console.log(error.message);
      }
    
      await browser.close();
    })();
    

    输出:

    { pageUrls:
       [ 'https://twitter.com/amirrustam',
         'https://www.componentsconf.com.au/workshops',
         'https://www.cypress.io/',
         'https://docs.cypress.io/guides/overview/why-cypress.html',
         'https://docs.cypress.io/api/api/table-of-contents.html',
         'https://docs.cypress.io/plugins/',
         'https://docs.cypress.io/examples/examples/recipes.html',
         'https://docs.cypress.io/faq/questions/using-cypress-faq.html',.................]
    

    编辑

    const puppeteer = require("puppeteer");
    
    (async () => {
        const browser = await puppeteer.launch();
        try {
            const page = await browser.newPage();
            await page.setCacheEnabled(false);
    
            await Promise.all([
                page.waitForNavigation({ timeout: 60000 }),
                page.goto("https://www.google.com/", {
                    waitUntil: "networkidle0",
                    timeout: 60000
                })
            ])
    
            const pageUrls = await page.evaluate(() => {
                const links = Array.from(document.querySelectorAll("a"));
                return links.map(link => link.href);
            });
            console.log({ pageUrls });
        } catch (error) {
            console.log(error.message);
        }
        await browser.close();
    })();
    

    输出:

    { pageUrls:
       [ 'https://mail.google.com/mail/?tab=wm&ogbl',
         'https://www.google.co.in/imghp?hl=en&tab=wi&ogbl',
         'https://www.google.co.in/intl/en/about/products?tab=wh',
         'https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/',
         'https://www.google.com/#',............
    

    【讨论】:

    • await page.waitForNavigation() 的问题是你需要提前知道有重定向到另一个页面。如果您尝试使用不重定向的其他 URL(例如 google.com),waitForNavigation 最终会超时。我正在寻找一个通用的解决方案,如果存在重定向,但不会在没有重定向的页面上超时。
    • 可以参考github.com/GoogleChrome/puppeteer/blob/master/docs/…可以增加超时时间。万一点击可以试试。 await Promise.all([ page.waitForNavigation(), // The promise resolves after navigation has finished page.click('a.my-link'), // Clicking the link will indirectly cause a navigation ]);
    • 如果您在 waitfornavigation 中观察到超时,那么可能是因为竞态条件github.com/GoogleChrome/puppeteer/issues/…
    • @RahulL ,我使用相同的代码,但出现错误, TypeError: Cannot read property 'request' of undefined.
    猜你喜欢
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2020-11-03
    相关资源
    最近更新 更多