javascript - 将多个参数传递给 page.evaluate()

标签 javascript puppeteer

我正在将一个附加参数传递到隐式 map() 函数中。

这可以在这部分代码中找到

{ s: elementPath.match(RegExp(/=(.*?)]/))[1] }

我想将一个额外的参数状态传递到外部函数中,而不是正则表达式,以便内部函数 map() 可以访问它

let res = await page.evaluate(elementPath => {
  return Array.from(
    document.querySelectorAll(elementPath),
    function(cin, index) {
      const result = {
        cinemaState: this.s, 
        cinemaIndex: index,
        cinemaId: cin.getAttribute("data-id"),
        cinemaName: cin.getAttribute("data-name"),
        cinemaURL: cin.getAttribute("data-url")
      };
      return result;
    },
    { s: elementPath.match(RegExp(/=(.*?)]/))[1] } // extracts state from the selector //TODO pass as additional argument 
  );
}, `div[data-state=${state}] div.top-select-option a.eccheckbox`);

我不确定参数如何从外部函数级联到最内部的函数。希望得到任何指导

最佳答案

您可以将尽可能多的必要参数传递到 page.evaluate 中,只要它们是可序列化的即可:

await page.evaluate( (selector, state) => {
    console.log(selector)
    console.log(state)
}, "div.top-select-option a.eccheckbox", state);

关于javascript - 将多个参数传递给 page.evaluate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53187725/

相关文章:

node.js - Jasmine 不与 puppeteer 师合作

node.js - 使用 puppeteer(库)进行网页抓取时克服分页

javascript - 如何使用Puppeteer获取指定元素

javascript - 比较缓冲区

javascript - 如何捕捉点击Flash播放器?

javascript - Jest : How to mock a promise on the same file for resolve and reject options?

Firefox 中的 JavaScript 问题

javascript - jQuery - 需要添加 setTimeout 函数将每 5 秒运行一次以检查 jQuery 是否已定义,最多 1 分钟

javascript - Angular 2 : Component doesn't render

javascript - 在 Node 中遍历 page.evaluate