Rvfvl
Rvfvl

Reputation: 383

Receiving ReferenceError when trying to access function parameter in Puppeteer call

I have a function that select filters on the website using Puppeteer.

The problem is when I pass function parameters and console log them before they are passed to page.select or page.$eval then they are logged correctly, but the function says that the variable is undefined.

private static async selectFilterOptions( { skillFilter, skillMinValue }) {
    try {
      console.log({ skillFilter, skillMinValue }); // { skillFilter: '1', skillMinValue: '30' }

      await this.page.waitForSelector(".containerMain");
      await this.page.select("select[name='skill']", skillFilter);
      await this.page.$eval("input[name='range']", el => el.value = skillMinValue); // Evaluation failed: ReferenceError: skillMinValue is not defined
      
      await this.page.click("input[name='Search']");

    } catch (error) {
      console.log(error.message)
    }
  }

Upvotes: 1

Views: 68

Answers (1)

Vaviloff
Vaviloff

Reputation: 16838

The docs describe page.$eval function this way:

page.$eval(selector, pageFunction[, ...args])

Args here are any number of comma-separated arguments to use inside of page.evaluate (optional, must be serializeable).

So in your case here's how to pass the value from node context to browser context:

await this.page.$eval("input[name='range']", el => el.value = skillMinValue, skillMinValue);

Upvotes: 1

Related Questions