pass multiple parameters into page.evaluate()

Viewed 1874

I am passing an additional parameter into the implicit map() function.

this is found in this part of the code

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

Instead of the Regex i would like to pass an additional parameter state into the outer functions so that the inner function map() could access it

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

I am not sure how the parameter could be cascaded down from the outer functions into the inner most function. Would appreciate any guidance

1 Answers

You can pass as many as necessary parameters into page.evaluate as long as they're serializable:

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