'Accessing nth table and counting rows with pupeteer in javascript error: "failed to find element matching selector "table:nth-of-type(2) > tr""

I am trying to programmatically count the rows in tables from the page https://rarity.tools/upcoming/ in javascript. Since the site loads through javascript, I've been using puppeteer. The site has multiple tables (4 total) and I would like to be able to reference each table and check how many rows they have.

I've tried to use nth-of-type to select the second, third, and fourth table, but I receive the error:

"Error: Error: failed to find element matching selector "table:nth-of-type(2) > tr""

Removing > tr returns a similar error:

"Error: Error: failed to find element matching selector "table:nth-of-type(2)""

I've tried using nth-child as well.

Code:

const puppeteer = require('puppeteer');

let fetch = async () => {

    try {
        // Puppeteer initialization
        const browser = await puppeteer.launch({ headless: true, defaultViewport: null });
        const [page] = await browser.pages();
        await page.goto('https://rarity.tools/upcoming/');
        await page.waitForTimeout(2500) // Timeout so page can load actual content 

        const numTables = await page.$$eval('table', el => el.length) - 1; // Rarity.tools has a table for each month of data. Each table must be looped through.
        // There is an extra table for upcoming "To Be Announced", there is a -1 to subtract that table from tables being looped through. 

        for (let j = 1; j < numTables + 1; j++) {
            const tableLength = await page.$eval(`table:nth-of-type(${j}) > tr`, el => el.length)
            console.log(tableLength)
        }

    }

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


fetch();

Utilizing the following instead doesn't return an error but instead returns 0 for the second and third table, even though I can see the HTML of the site I'm scraping has table rows under the second and third table.

const tableLength = await page.$$eval(`table:nth-of-type(${j}) > tr`, el => el.length)

However, the code above with $$eval returns the correct value for the first table.

Please let me know if I need to provide more information.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source