Home > Software engineering >  page.evaluate or page.$eval always returns undefined in Playwright
page.evaluate or page.$eval always returns undefined in Playwright

Time:06-04

I'm scraping a website using Playwright. but i have a problem. every time I do a page evaluate it always returns undefined data. but when I do console log in the evaluate page, the data appears on the console page, but doesn't appear in the terminal/command line

  const table= await page.$eval("#txnHistoryList > tbody", (table) => {
    let arrayData = [];
    if (table != null) {
      const rows = Array.from(table.querySelectorAll("tr"));
      rows.forEach((row, rowIndex) => {
        if (rowIndex > 1) {
          const td = row.querySelectorAll("td");
          let arrays = {};
          if (td.length > 2) {
            td.forEach((item, tdIndex) => {
              if (tdIndex == 1) {
                const date = item.innerText;
                arrays.date = date ? date.toString().trim() : "";
              }
              if (tdIndex == 2) {
                const transaction = item.innerText;
                arrays.transaction = transaction
                  ? transaction.toString().trim()
                  : "";
              }
              if (tdIndex == 4) {
                const type = item.innerText;
                arrays.type = type ? type.toString().trim() : "";
              }
              if (tdIndex == 5) {
                const paymentAmount = item.innerText;
                arrays.payment_amount = paymentAmount
                  ? paymentAmount.toString().trim()
                  : "";
              }
              if (tdIndex == 6) {
                const balance = item.innerText;
                arrays.balance = balance ? balance.toString().trim() : "";
              }
            });
            arrayData.push(arrays);
          }
        }
      });
    }
    console.log(arrayData);
    return arrayData;
  });

  console.log(tableMutasi);

  const data = {
    data: tableMutasi,
  };
  res.send(data);

Please help me

CodePudding user response:

It's more safety to use locator in playwright. You can try something like that.

const someFunc = async () => {
    const table = await page.locator('#txnHistoryList > tbody')
    const arrayData = []
    const isVisibleTable = await table.isVisible()
    if (isVisibleTable) {
        const rows = table.locator('tr')
        const rowsCount = await rows.count()

        for (let i = 0; i < rowsCount; i  = 1) {
            const row = rows.nth(i)
            const tdLocator = row.locator('td')
            const tdLocatorCount = await tdLocator.count()
            const arrays = {}
            if (tdLocatorCount > 2) {
                for (let j = 0; j < tdLocatorCount; j  = 1) {
                    switch (j) {
                        case 1: {
                            const date = await tdLocator.nth(j).innerText()
                            arrays.date = date ? date.toString().trim() : ''
                            break
                        }
                        case 2: {
                            const transaction = await tdLocator.nth(j).innerText()
                            arrays.transaction = transaction ? transaction.toString().trim() : ''
                            break
                        }
                        case 4: {
                            const type = await tdLocator.nth(j).innerText()
                            arrays.type = type ? type.toString().trim() : ''
                            break
                        }
                        case 5: {
                            const paymentAmount = tdLocator.nth(j).innerText()
                            arrays.payment_amount = paymentAmount ? paymentAmount.toString().trim() : ''
                            break
                        }
                        case 6: {
                            const balance = tdLocator.nth(j).innerText()
                            arrays.balance = balance ? balance.toString().trim() : ''
                            break
                        }
                        default:
                            continue
                    }

                    arrayData.push(arrays)
                }
            }
        }
    }
    return arrayData
}

Docs: https://playwright.dev/docs/api/class-locator

  • Related