【问题标题】:Puppeteer TimeoutError: Navigation timeout of 30000 ms exceededPuppeteer TimeoutError:超过 30000 毫秒的导航超时
【发布时间】:2023-03-04 10:59:01
【问题描述】:

我正在使用 Node.js 和 Google Puppeteer 开发网络截屏应用程序。 现在我必须捕获 38000 个页面,并且大部分功能都是可以找到的,但它在某些方面有错误,我不知道错误来自哪里。

我有两个假设。首先,我使用无头选项来检查问题,我发现某些页面有很多 GIF 文件,因此加载时间过长,因此显示超时错误。 其次,网站有时会加载失败,所以会显示错误。

这是我的完整代码

const puppeteer = require("puppeteer");
const fs = require('fs');

let galleryName = "frozen"; // Enter gallery name

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  // Adjustments particular to this page to ensure we hit desktop breakpoint.
  page.setViewport({
    width: 1000,
    height: 10000000,
    deviceScaleFactor: 1
  });

  fs.readFile('db.txt', async function (err, data) {
    if (err) throw err;
    let array = data.toString().split("\n");
    for (i in array) {
      console.log(`Now Processing : ${array[i]} | ${array.length - i -1} items left`);
      await page.goto(`https://gall.dcinside.com/${galleryName}/${array[i]}`), {
        waitUntil: "networkidle2",
        // timeout: 0
      };
      await page.waitForSelector(".view_content_wrap"), {
        waitUntil: 'networkidle2'
      }
      /* ScreenShot Functions */
      async function screenshotDOMElement(opts = {}) {
        const padding = "padding" in opts ? opts.padding : 0;
        const path = "path" in opts ? opts.path : null;
        const selector = opts.selector;

        if (!selector) throw Error("Please provide a selector.");

        const rect = await page.evaluate(selector => {
          const element = document.querySelector(selector);
          if (!element) return null;
          const {
            x,
            y,
            width,
            height
          } = element.getBoundingClientRect();
          return {
            left: x,
            top: y,
            width,
            height,
            id: element.id
          };
        }, selector);

        if (!rect)
          throw Error(
            `Could not find element that matches selector: ${selector}.`
          );

        return await page.screenshot({
          path,
          clip: {
            x: rect.left - padding,
            y: rect.top - padding,
            width: rect.width,
            height: rect.height + padding * 2
          }
        });
      }

      await screenshotDOMElement({
        path: `./result/${array[i]}.png`,
        selector: ".view_content_wrap",
        padding: 10
      });
    }
  });
  //   // await browser.close();
})();

【问题讨论】:

    标签: javascript node.js puppeteer


    【解决方案1】:

    在等待 browser.goto 之前尝试await page.setDefaultNavigationTimeout(0) 或将 { waitUntil: 'load', timeout: 0 } 放入 .goto 选项中。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2022-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多