0

Playwright 如何从参数中获取评估数据?page.evaluate: ReferenceError: d is not defined

async function testEv(d) {
  const data = await this.page.evaluate(function() {
    console.log('d from args is', d)
  })
}
4

1 回答 1

1

您必须将一个需要参数的函数传递给evaluate函数。为避免混淆,让我们调用它c,然后将d其作为第二个参数传递给evaluate.

async function testEv(d) {
  const data = await this.page.evaluate((c) {
    console.log('d from args is', c)
  }, d)
}

如果你需要传递多个参数,你可以传递一个对象

async function testEv(d) {
  const data = await this.page.evaluate((obj) {
    console.log('obj.foo from args is', obj.foo);
    console.log('obj.bar from args is', obj.bar);
  }, 
  {
    foo: d,
    bar: d,
  })
}
于 2022-02-03T12:45:30.030 回答