Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

evaluate evaluates your function in the page, not in your code’s context. 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:

await startDateAttribute.evaluate((node, startDate) => node.setAttribute('value', startDate), startDate)

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading