如何使用Puppeteer填写输入字段? [英] How to fill an input field using Puppeteer?

查看:1325
本文介绍了如何使用Puppeteer填写输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用木偶进行E2E测试,现在我试图用以下代码:

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', 'test@example.com');

它有效,但是我发现电子邮件地址在该字段中一个字符一个字符地键入

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?

推荐答案

只需设置以下输入值即可:

Just set value of input like this:

await page.$eval('#email', el => el.value = 'test@example.com');

以下是在Wikipedia上使用它的示例:

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.waitFor('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();
})();

这篇关于如何使用Puppeteer填写输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆