使用puppeteer在page.waitForFunction()内传递函数

2022-03-11 00:00:00 web-scraping puppeteer javascript

以下是我的代码:

function hasDataBeenRefreshed(pastAvgGain, currentAvgGain) {
  if (pastAvgGain!== currentAvgGain) {
      return true
  } else {
      return false
  }
}

async function getInfos(paire, page) {

  let pastAvgGain = C.AVG_GAIN.textContent

  await page.click(paire)

  let currentAvgGain = C.AVG_GAIN.textContent

  await page.waitForFunction(hasDataBeenRefreshed(pastAvgGain, currentAvgGain))

  ...

}

但是如果我这样做,我会收到此错误:

Error: Evaluation failed: TypeError: true is not a function

有没有办法达到这样的效果?


解决方案

page.waitForFunction()接受回调,现在您传入的是一个布尔值。要解决此问题,您可以执行以下操作:

  await page.waitForFunction((pastAvgGain, currentAvgGain) => {
  if (pastAvgGain!== currentAvgGain) {
      return true
  } else {
      return false
  }
} , {} , pastAvgGain, currentAvgGain )

https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagewaitforfunctionpagefunction-options-args有关详细信息,请参阅文档

第三个参数是要传递给回调的参数

在您的评论之后:

  await page.waitForFunction(() => {
      return hasDataBeenRefreshed(pastAvgGain, currentAvgGain); 
  } )

相关文章