Reputation: 3843
I am trying to check if there are specific number of elements with a specific class name. In case they are less than the number, keep scrolling down.
This is the code i used.
await page.evaluate(() => {
while (await page.$$('.Ns6lhs9 _gfh3').length < counter) {
window.scrollBy(0, window.innerHeight);
}
});
This snippet of code is contained inside an async function.
Unfortuantely i get this error message:
C:\pup\test.js:27
while (await page.$$('.Ns6lhs9 _gfh3').length < counter) {
^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Upvotes: 1
Views: 2225
Reputation: 276266
You need to mark the function you're passing inside as async
, an async
function is only lexically scoped to the function itself.
Any functions you call inside it (like .forEach
or evaluate
in this case) need to be marked as async
themselves (assuming they support it):
// Here
page.evaluate(async () => {
In your specific case, you should extract the loop outside of the evaluate to avoid timeouts:
while(await page.$$('.Ns6lhs9 _gfh3').length < counter) {
await page.evaluate(() => window.scrollBy(0, window.innerHeight);
}
If your evaluate
takes too long puppeteer will time out and it is generally preferable to not do expensive stuff inside evaluate
.
Upvotes: 4
Reputation: 82
try this:
await page.evaluate( async () => {
while (await page.$$('.Ns6lhs9 _gfh3').length < counter) {
window.scrollBy(0, window.innerHeight);
}
});
The callback of evaluate needs async prefixed.
Upvotes: 0