page.evaluate 或 page.$eval 始终 returns 在 Playwright 中未定义
page.evaluate or page.$eval always returns undefined in Playwright
我正在使用 Playwright 抓取一个网站。但我有一个问题。每次我做一个页面评估它总是 returns 未定义的数据。但是当我在评估页面中进行控制台登录时,数据出现在控制台页面上,但没有出现在 terminal/command 行
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);
请帮帮我
在剧作家中使用定位器更安全。你可以尝试类似的东西。
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
}
我正在使用 Playwright 抓取一个网站。但我有一个问题。每次我做一个页面评估它总是 returns 未定义的数据。但是当我在评估页面中进行控制台登录时,数据出现在控制台页面上,但没有出现在 terminal/command 行
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);
请帮帮我
在剧作家中使用定位器更安全。你可以尝试类似的东西。
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
}