Use xpath inside a loop with Puppeteer

Viewed 29

I would like to get the items from a page using a config object to determine the bits of the item I want to output.

An example config item is:

const shop = {
  urlToScrape: "https://shop-address.com/",
  itemSelector: "//li[@class='itemclass']",
  itemTitle: "//h2",
  itemLink: "//h2/a/@href",
};

I then get each item from the page using XPath:

const itemsOnPage = await page.$x(shop.itemSelector);

Now I want to grab the itemTitle and the itemLink for each item

for(const item of itemsOnPage){
// Some solution
}

I have tried using item.$x but can't get it to work and I'm lost on what else to try.

0 Answers
Related