0

Here at first I login to linkedin and create else one tab which I can open the linkedin link I am taking from incubators json looks like; [{linkeding: "url"}], And I need put them to Array.from() but there said:

Error: Evaluation failed: ReferenceError: json is not defined

How I can fix this

const puppeteer = require("puppeteer");
const json = require("./../incubators.json");

(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto("https://www.linkedin.com/");
  await page.type("#session_key", "gamerlex2004@gmail.com");
  await page.type("#session_password", "bakai987");
  await Promise.all([
    page.click(
      "#main-content > section.section.hero > div > div > form > button"
    ),
    page.waitForNavigation({ waitUntil: "networkidle0" }),
  ]);

  const page2 = await browser.newPage();
  await page2.setViewport({ width: 1200, height: 720 });

  const urls = await page2.evaluate(() =>
    Array.from(json, (element) => element.linkedin_url)
  );

  for (let i = 0, total_urls = urls.length; i < total_urls; i++) {
    await page2.goto(urls[i]);
    await page2
      .evaluate(async () => {
        let data = [];
        let elements = document.querySelectorAll(
          "body > div.application-outlet > div.authentication-outlet > div > div.scaffold-layout.scaffold-layout--breakpoint-xl.scaffold-layout--main-aside.scaffold-layout--reflow > div"
        );

        for (const element of elements) {
          data.push({
            linkedinUrl: page2.url().toString() || "",
            linkedinProfileUrl:
              element.querySelector("#ember45")?.getAttribute("src") || "",
          });
        }

        return data; // Return our data array
      })
      .then((data) => {
        fs.appendFile("./image.json", JSON.stringify(data), (err) =>
          err ? console.log(err) : null
        );
      });
  }
})();
t.niese
  • 36,631
  • 8
  • 65
  • 95
  • The function passed to `evaluate` is converted to a string and then injected into the context of the website loaded in puppeteer. So closures that would work otherwise don't apply here, and you can't access any variables that are not in the scope of the function due to that. The linked duplicate explains how you can pass data into the `evaluate` function. – t.niese Oct 21 '21 at 13:02

0 Answers0