Puppeteer: How can I ensure an element is visible before proceeding?

I am looking for a method to instruct Puppeteer to pause execution until a specific element appears on the page. For example, I want to ensure visibility for the next button before clicking it. Here is an example structure I envision:

const validateButton = await page.$('input[value=validate]');
await validateButton.click();

// I need a function that does something similar to this:
await checkElementVisibility('.btnNext');

const nextButton = await page.$('.btnNext');
await nextButton.click();

Can someone guide me on how to effectively implement this?

You can use Puppeteer’s page.waitForSelector to ensure an element is visible. Here's how you could implement it:

await page.waitForSelector('.btnNext', { visible: true });
const nextButton = await page.$('.btnNext');
await nextButton.click();

This waits for the .btnNext element to be visible before proceeding. Just replace '.btnNext' with your selector.