Sowam
Sowam

Reputation: 1736

Node.js Puppeteer problem with clicking button inside parent elements

I am using puppeteer library for scraping a website. I get all elements of exact class by:

let items = await page.$$("li.item");
for (let item of items) {
    let elementText = await page.evaluate(
        (element) => element.textContent,
    item);
    console.log(elementText );    
}

And I get all elements text. It works. The problem is that I need to click a button that is inside these elements. There is a box with li.item class and inside there is a button. I need to press each button inside each box. How can I do it? I tried element.children or even console logging element but it shows only undefinded. What do I do wrong here?

Upvotes: 0

Views: 143

Answers (1)

i-Guru
i-Guru

Reputation: 184

Hi Sowam,

let buttons = await page.$$("//li//button");
for (let button of buttons) {
    await button.click();   
}

Or

let buttons = await page.$$("//li.item//button");
for (let button of buttons) {
    await button.click();   
}

You click the button with page.click like this.

Upvotes: 1

Related Questions