最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer - counting elements by class name - Stack Overflow

programmeradmin4浏览0评论

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
  • Somethig like getElementsByClassName(".someclass").length ? – Jonas Wilms Commented Jun 1, 2018 at 14:36
  • Will this work in my case in puppeteer? – user1584421 Commented Jun 1, 2018 at 18:39
Add a comment  | 

1 Answer 1

Reset to default 17

I 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()
发布评论

评论列表(0)

  1. 暂无评论