let noMoreButtonTimes = 0; for (;;) { const button = await page.$('button') if (button) { await button.click() } else { noMoreButtonTimes++; if (noMoreButtonTimes > 3) break } await sleep(2000) }