Huckleberry Carignan
Huckleberry Carignan

Reputation: 2318

Need to click second of elements with the same selector using Puppeteer

Goal: I have several checkboxes and I need to select the second one using Puppeteer

Problem: I tried getting the ElementHandles via the page.$$ and page.click the second element [1] of the returned array, but that's failing. I'm not are why. The elements/checkboxes have a HTML data page of data-test="bar".

let foo = await page.$$('[data-test="bar"]');
await page.click(foo[1]);

Why isn't this working?!?

Upvotes: 1

Views: 5094

Answers (2)

Alpha and Omega
Alpha and Omega

Reputation: 104

One-liner: await (await page.$$('[data-test="bar"]'))[1].click();

This is the only way to click some controls created by React, when first element is a shadow DOM element and is unclickable (page.click throws Puppeteer "Error: No element found for selector ..."). This does not work in this case: await page.click('[data-test="bar"]:nth-child(1)')

Upvotes: 0

falinsky
falinsky

Reputation: 7428

Instead of performing

await page.click(foo[1]);

you could try

await foo[1].click();

If you want you use page.click instead - you should use a selector, not an element.

Upvotes: 4

Related Questions