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

153
Views
How to make puppeteer return something different if a specific element gets loaded

I'm trying to get values from a page using puppeteer, the main goal is to extract a URL if the content is valid, however the page might return an error inside of a p tag.

async function example() {
    const browser = await puppeteer.launch({
        headless: true,
    });
    const page = await browser.newPage();
    await page.goto('https://example.com');
    
    // The page might load an error inside of a p element
    // I don't want to block the rest of the script using await
    page.waitForSelector('p', {timeout: 5000})
    .then( async () => {
        const pString = await page.evaluate(() => {
            return document.querySelector('p').innerHTML;
        });
        console.log(pString);
        if(pString.includes('error')) {
            console.log('Page contains an error');

            //i need to make the main function 'return' if this happens
        }
    })

    // Wait for the link to load if everything is ok
    await page.waitForSelector('a', {timeout: 5000});
    const link = await page.$('a');

    // other stuff
}

I can't figure out a proper way of doing this without having to wait for the timeout

about 4 years ago · Juan Pablo Isaza
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!