I’m trying to extract text from a website using Puppeteer. So far I wrote the following code which is working but the “if” within $$eval doesn’t.
What I’m trying to do is to check if order_total is empty and in that case extract it from a different position but it doesn’t work.
let orders = await page.$$eval('.order-item', elements => elements.map(item => {
let order_date = item.innerText.split('n')[1];
let order_ID = (item.innerText.split('n')[2]).replace('Order ID: ', '');
let store_name = item.innerText.split('n')[5];
let order_total = item.innerText.split('n')[9];
if (item.innerText.split('n')[9] == '') {
order_total = item.innerText.split('n')[7]
}
return {
order_date,
order_ID,
store_name,
order_total
}
}));
console.log(JSON.stringify(orders))
My problem is that sometimes “order_total” with element 9 is empty and I would like to extract it from element 7 which in that case would have what I’m looking for.
Could you please help me to understand what I’m doing wrong?
Thanks a lot in advance for your help!