使用puppeteer在page.waitForFunction()内传递函数 [英] Pass a function inside page.waitForFunction() with puppeteer

查看:51
本文介绍了使用puppeteer在page.waitForFunction()内传递函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码:

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); 
  } )

这篇关于使用puppeteer在page.waitForFunction()内传递函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆