Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
page.evaluate or page.$eval always returns undefined in Playwright

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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda