1

The following question is similar but the answer are useless: How to get children of elements by Puppeteer. In these answers, users show how to get an attribute of an element. I need to access list of child nodes.

In the page I'm scraping there are 30 boxes with really complex and different content.

My goal was to - obtain the external boxes (DIVs) - check if some spceific nested html elements are present - retrieve content of nested element

For example - first box has image with nested link - second box has 4 <P>s

I am able, thanks to an answer to my previous question to retrieve all DIVs

I am doing this

  const boxes = await page.$$("DIV.a-row DIV.spinnerDeal DIV.box DIV.details")

  boxes.forEach((item) => {
       ... now, for example how to check if the item contains a P with a specific class=
  })

So my question is (for example) how to check if the item contains a P with a specific class.

More general question is how to browser nested elements of each item ?

I tried

boxes.forEach((item) => {
  console.log(item.getChildNodes())
  console.log("---")
})

But getChildNodes() is not a function

realtebo
  • 23,922
  • 37
  • 112
  • 189

1 Answers1

0

I think this works for you.

const pTags = await page.$x( '//DIV.a-row//DIV.spinnerDeal//DIV.box//DIV.details//p' );

After get all ptags and you can check every ptags with parent node, so you can get where it included.

i-Guru
  • 164
  • 4
  • 25