人偶错误:节点不可见或不是HTMLElement

时间:2020-06-05 00:38:04

标签: javascript node.js http puppeteer chromium

我想单击并输入以下网站的密码输入字段:https://undefeated.com

不幸的是,以下代码可用于输入名字,姓氏,电子邮件,但是在尝试访问密码字段时出现错误:

(node:17236) UnhandledPromiseRejectionWarning: Error: Node is either not visible or not an HTMLElement

如果您想重现该错误,请使用以下代码:


const RandExp = require('randexp');
const puppeteer = require('puppeteer-extra')

const stuff = async (siteURL) => {
    let args;


  args = [
    '--no-sandbox',
    "--ignore-certificate-errors",
    "--proxy-server='direct://",
    "--proxy-bypass-list=*",
    "--disable-setuid-sandbox",
    '--disable-web-security'
  ]

    // # spawn browser
    browser = await puppeteer.launch({
        headless: false,
        args,
        ignoreHTTPSErrors: true,
        devtools: false,
        defaultViewport: null
    });

    page = await browser.newPage();
    (await browser.pages())[0].close();
    await page.setViewport({ width: 0, height: 0 })
    await page.setDefaultNavigationTimeout(0);

   page.goto("https://"+siteURL+'/account/register', { waitUntil: 'networkidle2' });

    const resp = await waitForNavigation(page);

    if (resp.status() == 200) {
        // # wait for a selector
        let isFirstName = await page.waitForSelector('#FirstName');
        if(isFirstName){
          var firstName = new RandExp('[A-Z]{1}[a-z]{2,10}').gen();
          await page.click('#FirstName', { visible: true });
          await page.type('#FirstName', firstName, { delay: 50 });
        }

        let isLastName = await page.waitForSelector('#LastName');
        if(isLastName){
          var lastName = new RandExp('[A-Z]{1}[a-z]{2,10}').gen();
          await page.click('#LastName', { visible: true });
          await page.type('#LastName', lastName, { delay: 50 });
        }

        let isEmail = await page.waitForSelector('#Email');
        if(isEmail){
          let email = new RandExp('[A-Z]{1}[a-z0-9]{10}@gmail[.]com').gen()
          console.log(email)
          await page.click('#Email', { visible: true });
          await page.type('#Email', email, { delay: 50 });
        }

        let isPassword = await page.waitForSelector('#CreatePassword');
        if(isPassword){
          var password = new RandExp('[A-Z]{1}[A-Za-z0-9]{6,13}[0-9]{1}').gen();
          console.log(password)
          await page.click('#CreatePassword', { visible: true })
          await page.type('#CreatePassword', password, { delay: 50 });
        }

        // let agree = await page.waitForSelector('#account-create-check', {timeout: 1000});
        // if(agree){
        //   await page.click('#account-create-check', { visible: true });
        // }


    }
  };

    const waitForNavigation = async (page)  => {
      const [resp] = await Promise.all([
          page.waitForNavigation({
              timeout: 0,
              waitUntil: ['networkidle2', 'load', 'domcontentloaded']
          })
      ]);

      return resp;
  }

stuff("undefeated.com");

有人可以解释这个错误是什么意思,为什么我可以克服它?

感谢所有帮助:)

1 个答案:

答案 0 :(得分:1)

这似乎不正确:

await page.setViewport({ width: 0, height: 0 })

您的视口应该模仿真实浏览器的视口,因此请务必使用some plausible values进行设置。