问题描述
我处于向下滚动时会创建新内容的情况.新内容具有特定的类名称.
I am in a situation where new content is created when i scroll down.The new content has a specific class name.
我如何继续向下滚动直到所有元素加载完毕?换句话说,我想达到这样的阶段:如果我继续向下滚动,则不会加载任何新内容.
How can i keep scrolling down until all the elements has loaded?In other words, i want to reach the stage where if i keep scrolling down, nothing new will load.
我正在使用代码向下滚动,加上
I was using code to scroll down, coupled with an
await page.waitForSelector('.class_name');
这种方法的问题是,在所有元素加载完之后,代码继续向下滚动,没有创建新元素,最终出现超时错误.
The problem with this approach is that after all the elements have loaded, the code keeps on scrolling down, no new elements are created and eventually i get a timeout error.
这是代码
await page.evaluate( () => {
window.scrollBy(0, window.innerHeight);
});
await page.waitForSelector('.class_name');
推荐答案
试一下:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto('https://www.yoursite.com');
await page.setViewport({
width: 1200,
height: 800
});
await autoScroll(page);
await page.screenshot({
path: 'yoursite.png',
fullPage: true
});
await browser.close();
})();
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 100;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 100);
});
});
}
来源: https://github.com/chenxiaochun/blog/issues/38
这篇关于木偶-向下滚动,直到无法播放为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!