1

i'm trying to use puppeteer to log into a url, but it cannot find the inputs elements and the submit button, i believe the elements are generated dynamic by javascript, even tho i'm using the waitForSelector it doesn't work, what i'm missing?

here's my code:

const puppeteer = require('puppeteer');

(async () => {

    try {

        const browser = await puppeteer.launch({
            headless: false
        });

        const page = await browser.newPage();
        await page.goto('http://contatoplus.com/#!login', { waitUntil: 'networkidle0' });


            await page.waitForFunction("document.querySelector('#gwt-uid-3') && document.querySelector('#gwt-uid-3').clientHeight != 0");
            // or wait until "visibility" not hidden
            await page.waitForFunction("document.querySelector('#gwt-uid-3') && document.querySelector('#gwt-uid-3').style.visibility != 'hidden'");

            const btnNext = await page.$('#gwt-uid-3');
            await btnNext.keyboard.type('loginnn');


} catch (error) {
    console.log(error);
}

})();

i followed this: https://stackoverflow.com/a/54103671/5309671

Thiago
  • 111
  • 1
  • 4

2 Answers2

1

For me, the script is timed out in page.goto() stage.

  1. Try to delete the , { waitUntil: 'networkidle0' } option, the page.waitForFunction() should suffice.
  2. page.$() returns elementHandle which has not keyboard property. Just use await btnNext.type('loginnn');.

A variant:

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch({ headless: false });
    const [page] = await browser.pages();

    await page.goto('http://contatoplus.com/#!login');

    await page.waitForFunction(() => {
      const selectors = ['#gwt-uid-3', '#gwt-uid-5', 'div[role="button"]'];
      return selectors.every(selector => document.querySelector(selector)?.clientHeight > 0);
    });

    await page.type('#gwt-uid-3', 'login');
    await page.type('#gwt-uid-5', 'password');
    await page.click('div[role="button"]');
  } catch (err) {
    console.error(err);
  }
})();
vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26
0

I just solved an issue with a solution found here I wasn't able to find and click on an anchor tag and the solution was:

await page.focus('what-ever-the-selector-is')
await page.keyboard.type('\n');

this then clicked me into the next page

jawn
  • 851
  • 7
  • 10