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

295
Visualizações
How to get property js content edit it and run with puppeteer

im trying to use puppeteer to get property content of an element, edit it and run the edited version. For example: There is this element:

<a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt58'),{'j_idt58:j_idt201:0:j_idt203':'j_idt58:j_idt201:0:j_idt203'},'_blank');return false" class="btn btn-info"></a>

What I need is to get the onclick content, remove the _blank parameter and run the rest of the function... Any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

maybe not the most powerful solution out there but if you only need to do this on this specific tag you could set onclick's attribute with JavaScript within page.evalauate like this:

await page.evalauate(() => {
  document
    .querySelector(".btn.btn-info")
    .setAttribute(
      "onclick",
      document
        .querySelector(".btn.btn-info")
        .onclick.toString()
        .split("\n")[1]
        .replace(",'_blank'", "")
    );
});
await page.click(".btn.btn-info");

what's going on here?

  1. we run plain JavaScript within the page's scope with page.evaluate
  2. we select the tag with document.querySelector
  3. and set its onclick attribute (not its property!)
  4. getting the node value of onclick as string:
mojarra.jsfcljs(document.getElementById('j_idt58'),{'j_idt58:j_idt201:0:j_idt203':'j_idt58:j_idt201:0:j_idt203'},'_blank');return false
  1. using the 2nd line of the function (as we don't need the 1st 'function onclick(event) {' line when we reassign it as the attribute)
  2. and replacing ,'_blank' parameter from the original function (string).
  3. the result will be:
mojarra.jsfcljs(document.getElementById('j_idt58'),{'j_idt58:j_idt201:0:j_idt203':'j_idt58:j_idt201:0:j_idt203'});return false
  1. finally clicking the button with page.click executes the new function

alternatively, you can use attributes.onclick.nodeValue if you are not comfortable with toString().split("\n")[1] above:

document.querySelector(".btn.btn-info").attributes.onclick.nodeValue.replace(",'_blank'", "")
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