Home > Software design >  I am trying to get an h4 element text which is a child of divs using puppeteer
I am trying to get an h4 element text which is a child of divs using puppeteer

Time:12-05

I am trying this code to extract an h4 which a child of 7 parent divs. like parent div, grandparent div from this website. The h4 is Claimed.

It doesnt work because h4 isnt received.

const puppeteer = require('puppeteer')
async function run() {

    const browser = await puppeteer.launch({
        headless: false,
        ignoreHTTPSErrors: true,
    })

    var x = 1101;
    while (x !== 0) {
        const page = await browser.newPage();
        await page.setRequestInterception(true);

        page.on('request', (req) => {
            if (req.resourceType() == 'image' || req.resourceType() == 'font') {
                req.abort();
            }
            else {
                req.continue();
            }
        });
        page.setDefaultTimeout(0);
        await page.goto(`https://play.projectnebula.com/planet/${x}`);
        await page.waitForSelector('h4');
        const elements = await page.$$("h4");

        let text = await (await elements[elements.length - 2].getProperty("innerText")).jsonValue()
        text = text.toLowerCase().trim();
        if (text == 'claimed' || text == 'on sale') {
            console.log(text, x)
            x  = 1;
            await page.close();
        }
        else {
            console.log(x, text)
            x = 0
        }
    }
}
run();

I am trying to find an unclaimed planet, the unclaimed and claimed both are in h4. After working on 1 or 2 URLs. The code stops even though the planet is claimed because the h4 of claimed isnt fetched.there are almost 13 h4 in these pages. For first or second url all 13 are fetched but for next URL only 11 are fethced

CodePudding user response:

You are probably missing await before page.waitForSelector('h4').

This:

page.waitForSelector('h4')

should be:

await page.waitForSelector('h4')

CodePudding user response:

use async promise after waitforselector()

 await page.waitForSelector('h4').then(async() => {
            await page.waitForNetworkIdle();
            const elements = await page.$$("h4");
            let text = await(await elements[elements.length - 2].getProperty("innerText")).jsonValue()
            text = text.toLowerCase().trim();
            if (text == 'claimed' || text == 'on auction' || text == 'on sale') {
                console.log(text, x)
                x  = 1;
                await page.close();
            }
            else {
                console.log(x, text)
                x = 0
            }
        })
    }
  • Related