Why isnt page.evaluate() function code being executed within async function? – Puppeteer

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.

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật