Puppeteer 等待所有图像加载然后截取屏幕截图
Puppeteer wait for all images to load then take screenshot
我正在使用 Puppeteer 尝试在所有图像都已加载但无法正常工作后截取网站的屏幕截图。
这是我目前得到的代码,我使用 https://www.digg.com 作为示例网站:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.digg.com/');
await page.setViewport({width: 1640, height: 800});
await page.evaluate(() => {
return Promise.resolve(window.scrollTo(0,document.body.scrollHeight));
});
await page.waitFor(1000);
await page.evaluate(() => {
var images = document.querySelectorAll('img');
function preLoad() {
var promises = [];
function loadImage(img) {
return new Promise(function(resolve,reject) {
if (img.complete) {
resolve(img)
}
img.onload = function() {
resolve(img);
};
img.onerror = function(e) {
resolve(img);
};
})
}
for (var i = 0; i < images.length; i++)
{
promises.push(loadImage(images[i]));
}
return Promise.all(promises);
}
return preLoad();
});
await page.screenshot({path: 'digg.png', fullPage: true});
browser.close();
})();
我面临着完全相同的问题。
我觉得解决方案将涉及使用:
await page.setRequestInterceptionEnabled(true);
page.on('request', interceptedRequest => {
//some code here that adds this request to ...
//a list and checks whether all list items have ...
//been successfully completed!
});
有一个built-in option:
await page.goto('https://www.digg.com/', {"waitUntil" : "networkidle0"});
networkidle0 - consider navigation to be finished when there are no more than 0 network connections for at least 500 ms
networkidle2 - consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
P.S。当然,如果您正在使用像 Twitter 这样的无限滚动单页应用程序,它就不会工作。
另一种选择,当所有图片都加载完毕时,实际评估以获得回调
此选项也适用于不支持等待 networkidle0
选项的 setContent
await page.evaluate(async () => {
const selectors = Array.from(document.querySelectorAll("img"));
await Promise.all(selectors.map(img => {
if (img.complete) return;
return new Promise((resolve, reject) => {
img.addEventListener('load', resolve);
img.addEventListener('error', reject);
});
}));
})
等待图像延迟加载
您可能需要考虑先使用 Element.scrollIntoView()
等方法向下滚动以解决延迟加载图像的问题:
await page.goto('https://www.digg.com/', {
waitUntil: 'networkidle0', // Wait for all non-lazy loaded images to load
});
await page.evaluate(async () => {
// Scroll down to bottom of page to activate lazy loading images
document.body.scrollIntoView(false);
// Wait for all remaining lazy loading images to load
await Promise.all(Array.from(document.getElementsByTagName('img'), image => {
if (image.complete) {
return;
}
return new Promise((resolve, reject) => {
image.addEventListener('load', resolve);
image.addEventListener('error', reject);
});
}));
});
我使用 page.setViewPort(...) 方法找到了适用于多个站点的解决方案,如下所示:
const puppeteer = require('puppeteer');
async(() => {
const browser = await puppeteer.launch({
headless: true, // Set to false while development
defaultViewport: null,
args: [
'--no-sandbox',
'--start-maximized', // Start in maximized state
],
});
const page = await = browser.newPage();
await page.goto('https://www.digg.com/', {
waitUntil: 'networkidle0', timeout: 0
});
// Get scroll width and height of the rendered page and set viewport
const bodyWidth = await page.evaluate(() => document.body.scrollWidth);
const bodyHeight = await page.evaluate(() => document.body.scrollHeight);
await page.setViewport({ width: bodyWidth, height: bodyHeight });
await page.waitFor(1000);
await page.screenshot({path: 'digg-example.png' });
})();
我正在使用 Puppeteer 尝试在所有图像都已加载但无法正常工作后截取网站的屏幕截图。
这是我目前得到的代码,我使用 https://www.digg.com 作为示例网站:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.digg.com/');
await page.setViewport({width: 1640, height: 800});
await page.evaluate(() => {
return Promise.resolve(window.scrollTo(0,document.body.scrollHeight));
});
await page.waitFor(1000);
await page.evaluate(() => {
var images = document.querySelectorAll('img');
function preLoad() {
var promises = [];
function loadImage(img) {
return new Promise(function(resolve,reject) {
if (img.complete) {
resolve(img)
}
img.onload = function() {
resolve(img);
};
img.onerror = function(e) {
resolve(img);
};
})
}
for (var i = 0; i < images.length; i++)
{
promises.push(loadImage(images[i]));
}
return Promise.all(promises);
}
return preLoad();
});
await page.screenshot({path: 'digg.png', fullPage: true});
browser.close();
})();
我面临着完全相同的问题。 我觉得解决方案将涉及使用:
await page.setRequestInterceptionEnabled(true);
page.on('request', interceptedRequest => {
//some code here that adds this request to ...
//a list and checks whether all list items have ...
//been successfully completed!
});
有一个built-in option:
await page.goto('https://www.digg.com/', {"waitUntil" : "networkidle0"});
networkidle0 - consider navigation to be finished when there are no more than 0 network connections for at least 500 ms
networkidle2 - consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
P.S。当然,如果您正在使用像 Twitter 这样的无限滚动单页应用程序,它就不会工作。
另一种选择,当所有图片都加载完毕时,实际评估以获得回调
此选项也适用于不支持等待 networkidle0
选项的 setContent
await page.evaluate(async () => {
const selectors = Array.from(document.querySelectorAll("img"));
await Promise.all(selectors.map(img => {
if (img.complete) return;
return new Promise((resolve, reject) => {
img.addEventListener('load', resolve);
img.addEventListener('error', reject);
});
}));
})
等待图像延迟加载
您可能需要考虑先使用 Element.scrollIntoView()
等方法向下滚动以解决延迟加载图像的问题:
await page.goto('https://www.digg.com/', {
waitUntil: 'networkidle0', // Wait for all non-lazy loaded images to load
});
await page.evaluate(async () => {
// Scroll down to bottom of page to activate lazy loading images
document.body.scrollIntoView(false);
// Wait for all remaining lazy loading images to load
await Promise.all(Array.from(document.getElementsByTagName('img'), image => {
if (image.complete) {
return;
}
return new Promise((resolve, reject) => {
image.addEventListener('load', resolve);
image.addEventListener('error', reject);
});
}));
});
我使用 page.setViewPort(...) 方法找到了适用于多个站点的解决方案,如下所示:
const puppeteer = require('puppeteer');
async(() => {
const browser = await puppeteer.launch({
headless: true, // Set to false while development
defaultViewport: null,
args: [
'--no-sandbox',
'--start-maximized', // Start in maximized state
],
});
const page = await = browser.newPage();
await page.goto('https://www.digg.com/', {
waitUntil: 'networkidle0', timeout: 0
});
// Get scroll width and height of the rendered page and set viewport
const bodyWidth = await page.evaluate(() => document.body.scrollWidth);
const bodyHeight = await page.evaluate(() => document.body.scrollHeight);
await page.setViewport({ width: bodyWidth, height: bodyHeight });
await page.waitFor(1000);
await page.screenshot({path: 'digg-example.png' });
})();