我想继续向下滚动,直到所有具有特定类名的元素都加载到动态HTML环境中。
这是我使用的代码:
while ( (await page.$$('.xj7')).length < counter) {
await page.evaluate( () =>
window.scrollBy(0, window.innerHeight));
}
问题是在加载所有元素后,它不会停止滚动。我不知道为什么会这样,因为它应该退出while循环。
当我终止申请时,我收到此错误:
(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循环中引入的新异步函数有关?
编辑: 我决定测试页面的值。$$('。v1Nh3.kIKUG._bz0w'))。length
我写了这个测试代码:
while ( (await page.$$('.xj7')).length < counter) {
const read = (await page.$$('.xj7')).length;
console.log(read);
await page.evaluate( () =>
window.scrollBy(0, window.innerHeight));
}
价值从12增加到24,然后他们被困在30!然后当页面加载了所有元素时,值降为28!
这就是它坚持这个循环的原因。我不明白,当页面包含数百个这些元素时,它会达到这些值。