page.click is not a function

Viewed 103

Hi I have the following code, I'm having a error saying sublink.click is not a function. I'm trying to get a URLs from a page(I Did it), and then click on each URL and inside those URL get others informations. Could someone help me?

'use strict';
const puppeteer = require('puppeteer');

crawlPage();

function crawlPage() {
  (async () => {

    const args = [
      "--disable-setuid-sandbox",
      "--no-sandbox",
      "--blink-settings=imagesEnabled=false",
    ];
    const options = {
      args,
      headless: true,
      ignoreHTTPSErrors: true,
    };

    const browser = await puppeteer.launch(options);
    const page = await browser.newPage();
    await page.goto("https://www.guiadacarreira.com.br/profissao/lista-de-profissoes/", {
      waitUntil: 'networkidle2',
      timeout: 30000
    });

    let hrefs = await page.$$eval('a', as => as.map(a => a.href));
    console.log("DOID");

    // peguei os urls




    //let's click on each sub click
    for (let sublink of hrefs) {
      console.log('██AAA');

      const [response] = await Promise.all([
        page.waitForNavigation(),
        sublink.click('guia', {delay: 100}),
      ]);
      console.log('██BBB');

      const resultado = await sublink.evaluate(() => {
        return document.querySelector("body > div.container.gc-master-container > div:nth-child(3) > main > article > div.gc-post-body").firstChild.value;
      });
      // await page.goBack() 
      break;
    }


  })().catch((error) => {
    console.error(error);
  });;

}
1 Answers

It doesn't click and returns the sublink.click is not a function because sublink is a string that contains the url. In the line let hrefs = await page.$$eval('a', as => as.map(a => a.href)); you're returning the array of strings, not the elements. If you want to have the elements you need to use something like let hrefElements = await page.$$('a');

Related