最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Puppeteer : Timeout or sleep or wait inside Page.Evaluate() - Stack Overflow

matteradmin5PV0评论

I need a way to add some delay between clicking elements inside a page.evaluate function

below is what ive tried and it does not work

const result = await page.evaluate(async () => {
        let variants = document.querySelectorAll(".item-sku-image a");


        variants.forEach(async variant => {   
           await new Promise(function(resolve) { 
           setTimeout(resolve, 1000)
           });         
            await variant.click()
        });
        return data
    });

Update:

the for loop below works fine for one element when i try to use another for loop inside it - it goes crazy

const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
const sizes = [...document.querySelectorAll(".item-sku-size a")]; // Turn nodelist into an array

for (let variant of variants){
  // wait one second
  await new Promise(function(resolve) {setTimeout(resolve, 1000)});
  await variant.click()
  for (let size of sizes){
   // wait one second
   await new Promise(function(resolve) {setTimeout(resolve, 1000)});
   await size.click()
 }
}

I need a way to add some delay between clicking elements inside a page.evaluate function

below is what ive tried and it does not work

const result = await page.evaluate(async () => {
        let variants = document.querySelectorAll(".item-sku-image a");


        variants.forEach(async variant => {   
           await new Promise(function(resolve) { 
           setTimeout(resolve, 1000)
           });         
            await variant.click()
        });
        return data
    });

Update:

the for loop below works fine for one element when i try to use another for loop inside it - it goes crazy

const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
const sizes = [...document.querySelectorAll(".item-sku-size a")]; // Turn nodelist into an array

for (let variant of variants){
  // wait one second
  await new Promise(function(resolve) {setTimeout(resolve, 1000)});
  await variant.click()
  for (let size of sizes){
   // wait one second
   await new Promise(function(resolve) {setTimeout(resolve, 1000)});
   await size.click()
 }
}
Share Improve this question edited May 14, 2019 at 15:01 Limpfro asked May 13, 2019 at 21:09 LimpfroLimpfro 1776 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

.forEach will not work with the promises or async...await the way you want.

Use for..of instead.

const variants = [...document.querySelectorAll(".item-sku-image a")]; // Turn nodelist into an array
for (let variant of variants){
  // wait one second
  await new Promise(function(resolve) {setTimeout(resolve, 1000)});
  await variant.click()
}

It's easy to read, understand and implement.

Here are some references:

  • https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for...of
  • https://stackoverflow./a/37576787/6161265
Post a comment

comment list (0)

  1. No comments so far