Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Pass parameters to inner arrow function used with Playwright's evaluate

I am using playwright at the moment, and want to write a function which contains an inner arrow function, something along the lines of

 async function setSearchDate(startDate='2021-12-07') {
    ....do something...

    const startDateAttribute = await page.$(searchStartDate);
    await startDateAttribute.evaluate(node => node.setAttribute('value', startDate));

but somehow the inner arrow function does not see startDate value. the error I'm getting is "elementHandle.evaluate: ReferenceError: startDate is not defined".

The code works well if I hardcode startDate value in the arrow function. How can I pass that value?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

evaluate evaluates your function in the page, not in your code's context. You can access everything that exist in the page's execution environment (e.g. window, document, etc.) but nothing that exists in your execution environment (e.g. startDate). That's because in the page's context, there is no variable startDate (unless the page defined its own window.startDate...).

To pass parameters, you must make use of the ...args - evaluate takes extra arguments which are all passed along to your function:

await startDateAttribute.evaluate(
  (node, startDate) => node.setAttribute('value', startDate),
//       ^^^^^^^^^  2) extra argument(s) arrive here
//vvvvvvvvv         1) extra argument(s) getting passed in here
  startDate
)

See docs: Evaluating JavaScript (explaining this exact pitfall) and ElementHandle#evaluate.

about 4 years ago · Juan Pablo Isaza Report

0

await startDateAttribute.evaluate(function(node) { node.setAttribute('value', startDate) });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!