I’m trying to simulate a click on an element using Puppeteer, but I want to avoid using CSS selectors. Instead, I would like to target the element with an xPath and perform the click action. I attempted a solution but it didn’t work as expected. For example, I tried something like:
const pathQuery = '//*[@id="menu"]/ul/li[2]/button';
const elements = await page.$x(pathQuery);
if (elements.length > 0) {
await elements[0].click();
} else {
console.log('No element found using xPath');
}
I need advice on how to correctly click an element with xPath. Any insights or alternative approaches would be greatly appreciated.