I want to click through a gallery. Since I don't know how many pictures in the gallery are, I think I need a while loop. When I got to the last image, the next picture's button's classname changes to next-photo is-hidden until then it's simply next-photo.
So my goal is to have a while loop and when this button becomes hidden, break out from the loop, until then it should be called repeatedly.
Here I stand now:
async function openGallery(page) {
await page.evaluate(async () => {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
let randomTime = 250;
let clickOnImage = document.querySelector('div.image');
let nextbutton = document.querySelector('button[class="next-photo"]');
let closeGallery = document.querySelector('button[class="close-gallery"]');
let lastImage = document.querySelector('button[class="next-photo is-hidden"]');
if (clickOnImage) {
clickOnImage.click();
}
while (nextbutton) {
nextbutton.click();
if (lastImage) {
closeGallery.click();
break;
}
await delay(randomTime);
}
});
}
But now, when I reach the last picture, the program won't break out of loop, or in other words, other codes after this one won't execute. And the gallery won't close.
What am I missing?