Playwright how to click a specific element if there's lot found?

I having a hard time clicking 3rd element found. I tried index, and .third() method still not working.

Test Result:
Test Result

    async clickAddToCart(page) {
        const element = commonEl.addToCart();
        await page.evaluate((el) => document.querySelector(el)[3].click(), element)
 addToCart() {
        return ('.bg-blue-500.text-white.font-semibold.rounded-md');

Trying to get this element
Trying to get this element

>Solution :

You can use the nth selector for this. For the third button you have to use the index value as 2.

await page.locator(' >> nth=2').click();

Leave a Reply