Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill an input field using Puppeteer?

I'm using Puppeteer for E2E test, and I am now trying to fill an input field with the code below:

await page.type('#email', '[email protected]');

It worked, but I found the email address was typed into the field one character by one character as if a real human being was typing.

Is it possible to fill the input field with the email address all at one time?

like image 686
choasia Avatar asked Dec 25 '17 07:12

choasia


People also ask

How do you fill the input field in a puppeteer?

To fill an input field using Puppeteer and JavaScript, we can use the page. keyword. type method.

How do you fill a puppeteer?

Submitting a form with attachments async function uploadFile() { const browser = await puppeteer. launch({ headless: false }); const page = await browser. newPage(); await page. goto('https://www.w3schools.com/howto/howto_html_file_upload_button.asp'); const element = await page.

How do you change the input value of a puppeteer?

Just set value of input like this: await page. $eval('#email', el => el. value = '[email protected]');

How do you use puppeteer to text?

How to enter data in an input field using puppeteer. type(“Selector”, “Value”) is used to send value to any input fields in puppeteer.


4 Answers

Just set value of input like this:

await page.$eval('#email', el => el.value = '[email protected]');

Here is an example of using it on Wikipedia:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://en.wikipedia.org', {waitUntil: 'networkidle2'});

    await page.waitForSelector('input[name=search]');

    // await page.type('input[name=search]', 'Adenosine triphosphate');
    await page.$eval('input[name=search]', el => el.value = 'Adenosine triphosphate');

    await page.click('input[type="submit"]');
    await page.waitForSelector('#mw-content-text');
    const text = await page.evaluate(() => {
        const anchor = document.querySelector('#mw-content-text');
        return anchor.textContent;
    });
    console.log(text);
    await browser.close();
})();
like image 165
Everettss Avatar answered Oct 27 '22 06:10

Everettss


another way doing

await page.focus('#email')
await page.keyboard.type('test54')
like image 36
Sarath Ak Avatar answered Oct 27 '22 07:10

Sarath Ak


To extend the accepted answer above, you can use $eval with locally scoped variables too,

const myLocalValue = 'Adenosine triphosphate';    
await page.$eval('input[name=search]', (el, value) => el.value = value, myLocalValue);

This will take 'myLocalValue' from the local scope, and pass it into the browser scope as 'value'

like image 84
Andrew Avatar answered Oct 27 '22 05:10

Andrew


page.evaluate()

You can use page.evaluate() to assign the email string to the value attribute of the element:

await page.evaluate(() => {
  const email = document.querySelector('#email');
  email.value = '[email protected]';
});
like image 7
Grant Miller Avatar answered Oct 27 '22 07:10

Grant Miller