在puppeteer中获取具有类名的专有类名列表

时间:2019-02-20 17:18:37

标签: javascript google-chrome puppeteer webautomation

我正在尝试获取类名称,并检查至少一个标签是否具有red类。因此,如果其中至少有一个包含red类,则该函数必须返回true,否则返回false

最接近的是这个

const nodeList = await page.evaluate(() => {
    const arrynodeList = document.querySelectorAll('.an_panel_list')
    return arrynodeList
})

console.log('nodeList:', nodeList)

然后我得到

nodeList: { '0': {}, '1': {} }

例如,html看起来像

<div class="an_panel_list red">
<div class="an_panel_list">
<div class="an_panel_list">
<div class="an_panel_list">

然后我得到true

1 个答案:

答案 0 :(得分:1)

我会尝试解决evaluate函数中的所有问题:

const nodeList = await page.evaluate(() => {
    const arrynodeList = document.querySelectorAll('.an_panel_list');
    const redList = Array.prototype.slice.call(arrynodeList).filter(e => e.classList.contains("red"))
    return {
      divs: arrynodeList.length,
      reds: redList.length
    }
})

console.log(nodeList)