简体   繁体   English

如何使用 Puppeteer 进行自动值输入发送?

[英]How to make Auto Value Input Send with Puppeteer?

Sorry in advance for my bad language.. I've been working with the puppeteer for a while.提前为我的糟糕语言道歉..我已经和木偶师一起工作了一段时间。 what i want to do:我想做的事:

There is a login page.. Login Page有一个登录页面..登录页面

My goal is to autofill this login page How?我的目标是自动填充此登录页面如何? For example:例如:

Step - 1步骤1

const puppeteer = require("puppeteer");

(async () => {

const browser = await puppeteer.launch(
    {
        "headless": false,
        defaultViewport: null,
        args: ['--start-maximized'],
        "slowMo": 1
    });
const page = (await browser.pages())[0];
await page.goto('http://www.vbsca.ca/login/login.asp', { waitUntil: ['load', 'domcontentloaded', 'networkidle2'] }); // A website for example.

Step - 2第2步

let check;
try {

    const testAttribute = async (attributeName, selector) => {
        return await page.evaluate((attributeName, selector) => {
            const fieldList = Array.from(document.querySelectorAll(selector))
            return fieldList.every((field) => {
                const attributes = Array.from(field.attributes)
                return attributes.some((att) => { return att.name === attributeName })
            })
        }, attributeName, selector)
    }

// normal places up to this part // 这部分的正常位置

// The problem is below! // 下面是问题!

    console.log("Test", await testAttribute('type', 'input'));
    check = await testAttribute('type', 'input');

    if (check === true) {
        console.log("Next")

        await page.waitForTimeout(3000);    

        await page.evaluate(() => {
            const inputTest = Array.from(document.querySelectorAll('input'))
            console.log("İnputTest: ", inputTest)
            for (let key in inputTest) {
                inputTest[key].value = "Test";
                let elementTest = inputTest[key] // Result = <input type="password" placeholder="Enter Your Personal Password" value>               
            }
        });
    }
    else {
        console.log("Not Found")
    }
} catch (error) {
    console.log(error)
}

The problem is that this code is not working.问题是这段代码不起作用。 Why is that?这是为什么? Because;因为; (When I Send Data This data is not real) - I do not know the reason. (当我发送数据时这个数据不是真实的) - 我不知道原因。

It should not be like this(Photo)不应该是这样的(图)

This is how it should be (Photo)本该如此(图)

As a Result: My goal is to autofill entries and items like buttons.结果:我的目标是自动填充条目和按钮等项目。 all i have to do is give the url.我所要做的就是给 url。

Consider briefly the events of elements such as All Inputs and buttons for action.简要考虑元素的事件,例如所有输入和操作按钮。

I think I made the necessary explanation.我想我做了必要的解释。

(Can you help me?) (你能帮助我吗?)

If the question you are asking is how to add values to inputs you can try this.如果您要问的问题是如何向输入添加值,您可以试试这个。

await page.evaluate(() => {
  document.querySelectorAll('input').forEach(el => el.value = "Tesing");
  document.querySelector('input[type="submit"]').click();
});

await page.waitForNavigation();

I tried it on the link you sent and it worked.我在您发送的链接上试过了,它成功了。

  let browser, page;
  let url = 'http://www.vbsca.ca/login/login.asp';

  try {
    browser = await puppeteer.launch({ headless: true });
    page = await browser.newPage();
    await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 60000 });
    await page.waitForSelector('input', { visible: true, timeout: 60000 });

    await page.evaluate(() => {
      document.querySelectorAll('input').forEach(el => el.value = "Tesing");
      document.querySelector('input[type="submit"]').click();
    });
    
    await page.waitForNavigation();
  } catch (error) {
    console.log(error.message);
  } finally {
    if (browser) {
      await browser.close();
      console.log('closing browser');
    }
  }

Update you can also try this.更新你也可以试试这个。

await page.evaluate(() => {
  const inputs = document.querySelectorAll('input');
        
  for (let i = 0; i < inputs.length; i++) {
    if (i == 0) {
      inputs[i].value = 'Username';
    } else if (i == 1) {
      inputs[i].value = 'Password';
    }
  }

  document.querySelector('input[type="submit"]').click();
});

Last update.最后更新。 This works i have tried it on both site and it worked.这有效,我已经在两个站点上都尝试过并且有效。

 let browser, page;
  let url = 'http://test-sertifika.tga-certification.glomil.com';
//let url = 'http://www.vbsca.ca/login/login.asp';

  try {
    browser = await puppeteer.launch({ headless: true });
    page = await browser.newPage();
    await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 60000 });
    await page.waitForSelector('input', { visible: true, timeout: 60000 });

    await page.evaluate(() => {
      const inputs = document.querySelectorAll('input');

      for (let i = 0; i < inputs.length; i++) {
        if (i == 0) {
          inputs[i].value = 'Username';
        } else if (i == 1) {
          inputs[i].value = 'Password';
        }
      }

      if (document.querySelector('div[class="submit-wrapper"] > button')) {
        document.querySelector('div[class="submit-wrapper"] > button').click();
      } else if (document.querySelector('input[type="submit"]')) {
        document.querySelector('input[type="submit"]').click();
      }
    });
    
    await page.waitForNavigation();
  } catch (error) {
    console.log(error.message);
  } finally {
    if (browser) {
      await browser.close();
      console.log('closing browser');
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM