Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
Evalute method doesn't works on puppeteer code

I have a puppeteer js code which works fine and i would like to handle error when elements doenst exists cause i get the error below :

  • Error: failed to find element matching selector "div > div > span > div > h1"

so i would like to save null value if the selector doesn't exist

1 : this is the first code that works

for (var i = 0; i < urls.length; i++) {

const name2 = await page.$eval('div > div > span > div > h1', ele => ele.textContent);
console.log(name2);

        if (name2 !== "Null") {
            fs.appendFile(
                "files/data.csv",
                ${name2}\n`,
                function (err) {
                    if (err) throw err;
                }
            );
        }

}

2 : second method name value keep null

for (var i = 0; i < urls.length; i++) {

        let name = 'Null';
        try {
            name = await page.evaluate(
                (el) => el.querySelector("div > div > span > div > 
                h1").textContent,
                i

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

        if (name !== "Null") {
            fs.appendFile(
                "files/data.csv",
                ${name}\n`,
                function (err) {
                    if (err) throw err;
                }
            );
        }

}

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!