Estoy raspando un sitio web usando Playwright. pero tengo un problema. cada vez que evalúo una página, siempre devuelve datos indefinidos. pero cuando inicio sesión en la consola en la página de evaluación, los datos aparecen en la página de la consola, pero no aparecen en la terminal/línea de comando
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);por favor, ayúdame
Es más seguro usar localizador en dramaturgo. Puedes intentar algo así.
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 }Documentos: https://playwright.dev/docs/api/class-locator