javascriptpuppeteer

Set text into input Field


I have a string which of value changes, and I need to put this string into an input field.

(async () => {
  let pageNum = 1004327;

  let browser = await puppeteer.launch({
    headless: true,
  });

  let page = await browser.newPage();
    
  while (1) {
      await page.goto(`${link}${pageNum}`);
      page.setDefaultTimeout(0);
      let html = await page.evaluate(async () => {
        let mail = document.getElementById(
          "ctl00_phWorkZone_DbLabel8"
        ).innerText;
        let obj = {
          email: mail,
          link: window.location.href,
        };
        return obj;
      });
      if (Object.values(html)[0]) {
        await staff.type(
          "textarea[name=mail_address]",
          Object.values(html).join("\n"),
          {
            delay: 0,
          }
        );
        console.log(pageNum);
        pageNum++;
        staff.click("input[name=save]", { delay: 0 });
      }
  }
})();

I used .type() method, and it works, but I need something faster.


Solution

  • .type() method will allow you to fill the input like human typing. Instead of using that, just try .keyboard.sendCharacter() method (source). It will allow you to fill in the input instantly without typing.

    Example how to use it :

    const puppeteer=require('puppeteer');
    const browser=await puppeteer.launch({ headless: false });
    
    const page=await browser.newPage()
    await page.goto("https://stackoverflow.com/q/75395199/12715723")
    
    let input=await page.$('input[name="q"]');
    await input.click();
    await page.keyboard.sendCharacter('test');