I'm trying to use Puppeteer to find prices from websites, and the way I'm doing it is running a $$eval and a set of if statements. I'm checking that there is a "$" in the innerText as well as a children.length of 0. This works decently, but there are sometimes children for just part of the string (i.e. a bold $ but a regular number). Is there any way to find out what tag the children are easily? I would like to check for no children having <div> tags. Below is my code:
var prices = await page.$$eval("div, span, p", (priceElements) => {
var tmpPrices = [];
for (var i = 0; i < priceElements.length; i++) {
const priceIndex = priceElements[i].innerText.toLowerCase().indexOf("$");
if (
priceIndex > -1 &&
priceElements[i].children.length == 0 &&
priceElements[i].innerText.substring(priceIndex + 1).indexOf("$") == -1
) {
tmpPrices.push(priceElements[i].innerText);
}
}
return tmpPrices;
});
And here's an example of when it doesn't work:
<div class="pricevalue1 accent-color1" style="font-size: 20px;">
<b>
<span class="currency-symbol">$</span>
92,950
</b>
</div>