How to check if an element exists on a page with Puppeteer?

To check if an element exists on a page with Puppeteer, you can use the Page.$ method to select the element and then check if the returned value is null or not. Here is an example code snippet to do this:

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); const element = await page.$('your-element-selector'); if(element) { console.log('Element exists on the page.'); } else { console.log('Element does not exist on the page.'); } await browser.close(); })();

Replace 'your-element-selector' with the CSS selector of the element you want to check for. This code will launch a Puppeteer browser, navigate to a page, check if the specified element exists on the page, and log the result to the console.