In Puppeteer, you can use CSS selectors to locate elements in the DOM just like you would in a browser's developer tools.
Here's an example of how you can use CSS selectors to locate elements in Puppeteer:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Your code here
await browser.close();
})();
page.$()
method to locate an element using a CSS selector. This method accepts a CSS selector as a parameter and returns the first element that matches the selector:const element = await page.$('h1');
page.$$()
method to locate multiple elements that match a CSS selector. This method returns an array of all elements that match the selector:const elements = await page.$$('p');
await element.click();
Here's an example of locating an element by its class name and clicking on it:
const element = await page.$('.my-element-class');
await element.click();
Keep in mind that using CSS selectors to locate elements in Puppeteer is similar to using JavaScript in a browser's developer tools, so you can use any valid CSS selector syntax to locate elements on a page.