我正在使用 Puppeteer对于 E2E 测试,我现在正在尝试使用以下代码填充输入字段:
await page.type('#email', 'test@example.com');
它起作用了,但我发现电子邮件地址是一个字符一个字符地输入到字段中的,就好像真人在打字一样。
是否可以一次用电子邮件地址填写输入字段?
最佳答案
只需像这样设置输入值:
await page.$eval('#email', el => el.value = 'test@example.com');
以下是在 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();
})();
关于javascript - 如何使用 Puppeteer 填充输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47966510/