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

116
Views
Multiple selector search via puppeteer library

I use puppeteer to get data about the store. I search using the p.shop-page-content__text_large, span.shop-list-item__address selectors, but I ran into such a problem that only one of them can be present on the page. I tried to solve the problem in the following way, but it does not work. Tell me how can this be fixed?

const puppeteer = require('puppeteer');

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

const cities = [{'CITY': 'Town1', 'LINK': '/shops/town1/'}, {'CITY': 'Town2', 'LINK': '/shops/town2/'}];

async function getData(page, selector) {
    return await page.$$eval(selector, info => info.map((data) => {
        let str     = data.textContent.trim(),
            from    = str.search(','),
            to      = str.length;
    
        return {
            'COUNTRY': 'unknow',
            'STREET' : str.substring(from, to)
        }
    }));
}

const result = [];

for (let val of cities) {
    console.log(val.LINK, val.CITY);

    const page = await browser.newPage();
    await page.goto('https://www.example-site.ru' + val.LINK);

    data = await page.waitForFunction('.shop-page-content').then(async() => {
        console.log('ok');
        return await getData(page, 'p.shop-page-content__text_large');
    }).catch(async (e) => {
        console.log('fail');
        
        await page.waitForSelector('.shops-info__section');
        return await getData(page, 'span.shop-list-item__address');
        // result.push(data);
    });

    result.push(data);
    await browser.close();
}

console.log(result);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It turned out like this:

const browser = await puppeteer.launch({
    headless: false,
    slowMo: 150,
});
const cities = [{'CITY': 'Town1', 'LINK': '/shops/town1/'}, {'CITY': 'Town2', 'LINK': '/shops/town2/'}];
const page   = await browser.newPage();
const result = [];

for (let val of cities) {
    await page.goto('https://www.example-site.ru' + val.LINK);

    const list = await page.evaluate(() => {
        const data     = []; 
        const elements = document.querySelectorAll('p.shop-page-content__text_large').length 
                        ? document.querySelectorAll('p.shop-page-content__text_large')
                        : document.querySelectorAll('span.shop-list-item__address'); 

        for (const element of elements) {
            data.push(element.innerText);
        }

        return data;
    });

    result.push({
        link: val.LINK,
        city: val.CITY,
        list
    })
}

await browser.close();
about 4 years ago · Juan Pablo Isaza Report
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!