I am trying to get info about all the elements with a particular class name into an array.
The problem is this is a dynamically generated HTML page, and as long as I scroll down, new elements of that class name appear.
Fortunately, I know beforehand how many of these elements exist.
So my hypothetical solution is to check the number of elements with that particular class name, and as long as that number is less than the one I know, keep scrolling down.
The problem is I don't know exactly how to count elements of a particular class name inside puppeteer and the API was not very useful either.
I am trying to get info about all the elements with a particular class name into an array.
The problem is this is a dynamically generated HTML page, and as long as I scroll down, new elements of that class name appear.
Fortunately, I know beforehand how many of these elements exist.
So my hypothetical solution is to check the number of elements with that particular class name, and as long as that number is less than the one I know, keep scrolling down.
The problem is I don't know exactly how to count elements of a particular class name inside puppeteer and the API was not very useful either.
Share Improve this question edited Jan 1, 2020 at 20:24 Ahmed Ashour 5,54910 gold badges39 silver badges62 bronze badges asked Jun 1, 2018 at 14:34 user1584421user1584421 3,86312 gold badges56 silver badges97 bronze badges 2 |1 Answer
Reset to default 17I think this is what you are looking for
const puppeteer = require('puppeteer')
async function count () {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'})
await page.evaluate(_ => {
window.scrollBy(0, window.innerHeight)
})
console.log('how many?', (await page.$$('td.title')).length)
await browser.close()
}
count()
getElementsByClassName(".someclass").length
? – Jonas Wilms Commented Jun 1, 2018 at 14:36