最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How in Playwright get in evaluate data from arguments? - Stack Overflow

programmeradmin0浏览0评论

How in Playwright get in evaluate data from arguments? 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)
  })
}

How in Playwright get in evaluate data from arguments? 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)
  })
}
Share Improve this question asked Feb 3, 2022 at 11:18 rd100rd100 11 silver badge2 bronze badges 1
  • Does this answer your question? Playwright pass variable into eval with JavaScript (Node) – ggorlen Commented Jul 18, 2022 at 15:09
Add a ment  | 

1 Answer 1

Reset to default 4

You have to pass to the evaluate function a function that expects an argument. To avoid the confusion lets call it c, and then you pass d as a second argument to the evaluate.

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

If you need to pass more than one argument you can pass an object

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,
  })
}
发布评论

评论列表(0)

  1. 暂无评论