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

172
Visualizações
Failed to Assign Value to InputElement using Puppeteer

I am trying to set a value to an Input/TextArea element in html using puppeteer without success. The code is transpiled with typescript.

Here I use YouTube as an example.

First it should navigate to youtube, then select the input box with pseudo-class css selector, and set the value according to the variables: prefix and caption.

The script shuts down as soon as it starts to perform page.eval. I am not sure if I am using the wrong css selector. There might be an issue when I am try to assign string to "value" property as well.

(async ()=>{
const searchbox='html>body>ytd-app>div>div>ytd-masthead>div:nth-of-type(3)/div:nth-of-type(2)/ytd-searchbox/form/div:first-of-type/div:first-of-type/input'
const puppeteer  = require("puppeteer")
const browser = await puppeteer.launch({headless:false, defaultViewport: null, slowMo: 100});
const page = await browser.newPage();

    await page.goto('https://www.youtube.com/');
    await uploadPhoto("Some", " Video");
    async function uploadPhoto(prefix:string, caption:string) {
        await page.evaluate( (searchbox:string, prefix:string, caption:string) => {
            (document.querySelector(searchbox) as HTMLInputElement).value = `${prefix} ${caption}`},
            searchbox, prefix, caption)
    }
    console.log("finish")
})();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you should remove all types from your evaluate function as this function will be executed directly in the brwoser and I believe the types would make it fail:

async function uploadPhoto(prefix:string, caption:string) {
    await page.evaluate( (searchbox, prefix, caption) => {
            document.querySelector(searchbox).value = `${prefix} ${caption}`
    }, searchbox, prefix, caption)
}

An easier solution is to use the method type from ElementHandle class:

const searchInput: ElementHandle<Element> = await page.waitForSelector(searchbox, { timeout: 5000 });
await searchInput.type("Some text to search");

or type from Page class:

await page.type(searchbox, "Some text to search");
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