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

Time:06-22

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

CodePudding user response:

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

await page.locator('button.py-4 >> nth=2').click();
  • Related