Javascript 木偶演员-无限滚动场景
我想一直向下滚动,直到在动态HTML环境中加载具有特定类名的所有元素 这是我使用的代码:Javascript 木偶演员-无限滚动场景,javascript,node.js,async-await,puppeteer,Javascript,Node.js,Async Await,Puppeteer,我想一直向下滚动,直到在动态HTML环境中加载具有特定类名的所有元素 这是我使用的代码: while ((await page.$$('.xj7')).length < counter) { await page.evaluate(() => window.scrollBy(0, window.innerHeight)); } 这些值从12增加到24,然后被困在30!然后当页面加载了所有元素后,该值下降到28 这就是为什么它会阻塞这个循环。我不明白,虽然页面上有数百个这样的元
while ((await page.$$('.xj7')).length < counter) {
await page.evaluate(() =>
window.scrollBy(0, window.innerHeight));
}
这些值从12增加到24,然后被困在30!然后当页面加载了所有元素后,该值下降到28
这就是为什么它会阻塞这个循环。我不明白,虽然页面上有数百个这样的元素,但它的范围是这些值。什么是
计数器
,为什么您希望xj7
类的数量随着页面向下滚动而减少?。我不希望它减少。计数器是具有这个特定类名的元素数(我事先知道)。所以我一直向下滚动,这样动态HTML网页就可以为我提供所有带有类名的元素。那么,为什么你希望循环结束,你的基本意思是。。当类xj7
的元素数小于类xj7
的元素数时,请保持滚动。。当然它不会结束,但当所有元素都存在时,类xj7的元素等于计数器。“小于”条件是不正确的。因此,它应该停止。当然,简单的方法就是跟踪窗口。滚动,如果它在循环中没有改变,那么在循环完成时。。
(node:5708) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.cal
lFunctionOn): Session closed. Most likely the page has been closed.
at CDPSession.send (C:\node_modules\pupp
eteer\lib\Connection.js:187:29)
at ExecutionContext.evaluateHandle (C:\node_modules\puppeteer\lib\ExecutionContext.js:73:75)
at ExecutionContext.evaluate (C:\node_modules\puppeteer\lib\ExecutionContext.js:46:31)
at Frame.evaluate (C:\node_modules\puppeteer\lib\FrameManager.js:326:20)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:5708) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejection
id: 4)
(node:5708) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
while ((await page.$$('.xj7')).length < counter) {
const read = (await page.$$('.xj7')).length;
console.log(read);
await page.evaluate(() =>
window.scrollBy(0, window.innerHeight));
}