const getLastCarData = async (page, make) => {
let sort = ascending;
let url = `https://www.xxxxxxxxxxxxx/xxxxxxxxx?make=${make}&postcode=XXXXXXX&sort=${sort}`;
// await page.goto(url, { waitUntil: 'networkidle0' });
await page.goto(url, { waitUntil: 'domcontentloaded' });
await goToPageWithRetries(page, url); // more robust
// ERROR FINDING LAST CAR
// Returning false value
// const html = await page.content();
// console.log(html);
console.log("Before page.evaluate");
const lastCar = await page.evaluate( () => {
console.log("inside page.evaluate");
const carPods = Array.from(document.querySelectorAll('[data-testid="trader-seller-listing"], [data-testid="private-seller-listing"]'));
const nonPromotedCars = carPods.filter(car =>
!car.querySelector('[data-testid="leasing-listing-details"]') &&
!car.querySelector('[data-testid="LEASING_LISTING"]') &&
!car.querySelector('a[href*="journey=PROMOTED_LISTING_JOURNEY"]')
);
if (nonPromotedCars.length === 0) {
console.log("No non-promoted cars found on this page.");
return null;
}
const convertPrice = (priceString) => {
return parseFloat(priceString.replace('£', '').replace(/,/g, ''));
};
let lowestPriceCar = null;
let lowestPrice = Infinity;
nonPromotedCars.forEach(car => {
const priceElement = car.querySelector('div div div section section div p span span');
if (priceElement) {
const price = convertPrice(priceElement.innerText);
if (price < lowestPrice) {
lowestPrice = price;
lowestPriceCar = {
listingId: car.getAttribute('id'),
price: price
};
}
}
});
return lowestPriceCar;
});
if ( lastCar) {
const finalListingPath = path.join('tempData', `finalListing${make}.json`);
await fs.writeFile(finalListingPath, JSON.stringify(lastCar, null, 2));
console.log(`Final listing for ${make} saved to ${finalListingPath}. Lowest price: ${lastCar.price}`);
} else {
console.log(`No valid cars found for ${make}`);
}
return lastCar;
};
The code within the page.evaluate function is not being executed at all.
Expected: to return the price value of the last item on the page
Instead, the code is not executed and therefore the lastCar variable stays null which leads to the logic error of printing “No valid cars” when there are.
A1X1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I suspect that you can’t do things like:
document.querySelectorAll(...)
inside page.evaluate
: the function can accept one or two arguments where the first is the “analyze” function – a function that handles the data, and the second (optional) argument contains the data: it can be the body/innerHTML of an element and such.
It’s probably a better practice to prepare all the data before calling it (it’s also more readable and easier to debug & maintain).
One thing that you can try though: modify:
await page.evaluate( () => {
to:
await page.evaluate( async () => {
I don’t see in the documentation that passing async function is supported but you just as well may try.
Good luck!
2