伪造的page.evaluate()随机失败,原因:执行上下文被破坏,很可能是由于导航引起的

时间:2019-08-21 01:46:32

标签: node.js puppeteer serverless

以下代码随机失败:

  

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

那是为什么? 有任何解决方法吗?

我使用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();
})();


1 个答案:

答案 0 :(得分:2)

解决方法-添加以下代码

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/#',............