木偶超时错误:超出 30000 毫秒的导航超时



我正在使用Node.js和Google Puppeteer开发Web屏幕捕获应用程序。 现在我必须捕获 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();
})();

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

相关内容

最新更新