Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

await startDateAttribute.evaluate(function(node) { node.setAttribute('value', startDate) });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda