Having an issue with this specific part of the code:
(async () => {
const browser = await puppeteer.launch({
headless: false,
// slowMo: 250 // slow down by 250ms
});
const page = await browser.newPage();
//some code
// CODE ABOVE WORKS, here is what is not working inside of it:
await page.evaluate(() => {
function delay(timeout) {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const farms = document.querySelectorAll(".startButton");
for (let i = 0; i < 3; i++) {
for (const farm of farms) {
farm.click();
}
delay(randomInteger(820000, 920000));
}
});
})();
What I'm trying to achieve is clicking all buttons with a certain class, waiting for some time, and then clicking them again. The delay function seems to be causing issues - it either doesn't work or isn't executed properly. Any help would be appreciated!
#EDIT
The code has been updated as suggested by @Sheun Aluko to include async and await. The issue was related to the website's security system, prompting the need for a workaround involving refreshing the page and changing the loop order. Although it may not be the most efficient solution, it guarantees functionality. Here's an example:
(async () => {
const browser = await puppeteer.launch({
headless: false,
// slowMo: 250 // slow down by 250ms
});
//SOME CODE THAT WORKS
//OUR PART BELOW
for (let i = 0; i < 150; i++) {
await page.goto("SOME_URL", {
waitUntil: "networkidle2",
});
await page.evaluate(async (page) => {
function delay(timeout) {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const farms = document.querySelectorAll('SOME_SELECTOR');
for (const farm of farms) {
farm.click();
}
await delay(randomInteger(840000, 960000));
});
}
})