I’m using Puppeteer for E2E test, and I am now trying to fill an input field with the code below:
JavaScript
x
2
1
await page.type('#email', 'test@example.com');
2
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?
Advertisement
Answer
Just set value of input like this:
JavaScript
1
2
1
await page.$eval('#email', el => el.value = 'test@example.com');
2
Here is an example of using it on Wikipedia:
JavaScript
1
22
22
1
const puppeteer = require('puppeteer');
2
3
(async () => {
4
const browser = await puppeteer.launch();
5
const page = await browser.newPage();
6
await page.goto('https://en.wikipedia.org', {waitUntil: 'networkidle2'});
7
8
await page.waitForSelector('input[name=search]');
9
10
// await page.type('input[name=search]', 'Adenosine triphosphate');
11
await page.$eval('input[name=search]', el => el.value = 'Adenosine triphosphate');
12
13
await page.click('input[type="submit"]');
14
await page.waitForSelector('#mw-content-text');
15
const text = await page.evaluate(() => {
16
const anchor = document.querySelector('#mw-content-text');
17
return anchor.textContent;
18
});
19
console.log(text);
20
await browser.close();
21
})();
22