Home > Back-end >  how to put a delay in this evaluate with puppeteer
how to put a delay in this evaluate with puppeteer

Time:12-02

I am trying to put a delay in this evaluate with puppeteer but I tried everything and didn't works, such as:

await page.waitForTimeout(1000);

or with:

setTimeout(() => { }, 1000);
                await page.evaluate(() => {
                    let elements = document.getElementsByClassName('button-text');
                    for (let element of elements) if (element.innerText == 'Add') element.click();
                })

I wanted it to be after every click like this example but it never works:

await page.evaluate(() => {
    let elements = document.getElementsByClassName('button-text');
    for (let element of elements)
        if (element.innerText == 'Add'){
            await page.waitForTimeout(1000);
            element.click();   
        }
})

CodePudding user response:

You want to do that outside of the browser context:

let elements = await page.$$eval('.button-text', els => els.filter(el => el.innerText === 'Add'))
for(let element of elements){
  await page.waitForTimeout(1000)
  await element.click()
}
  • Related