Puppeteer: How can I delay actions until an element appears?

I am using the Puppeteer automation library and need to ensure that my script interacts with elements only after they are visibly rendered. For example, I want to perform a click on a button only once it becomes visible on the page. I attempted to use a helper function, similar to the example below, but it didn’t function as expected:

const checkbox = await page.$('input[data-check="confirm"]');
await checkbox.click();

// Intend to wait until the nextButton is displayed
await waitForVisibility('.nextButton');

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

Could someone provide guidance or alternate methods to reliably wait for an element to be visible before proceeding with further actions?

I encountered similar issues where waitFor functions didn’t guarantee the element was interactable. In my experience using Puppeteer, a more reliable approach was to use page.waitForSelector with the visible option enabled. This method ensured Puppeteer would pause execution until the element was both present in the DOM and visible. I was carefully testing against different conditions to ensure that any CSS transitions or animations wouldn’t disrupt the detection process. Additionally, validating the selector accuracy and including error handling helped mitigate any unexpected script failures.