在 puppeteer 中获取具有 class 名称的独家 class 名称列表
Getting a list of exclusive class name with a class name in puppeteer
我正在尝试获取 class 个名称并检查是否至少有一个标签具有 red
class。所以,如果其中至少有一个包含red
class,函数必须returntrue
,否则,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
。
我会尝试解决 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)
我正在尝试获取 class 个名称并检查是否至少有一个标签具有 red
class。所以,如果其中至少有一个包含red
class,函数必须returntrue
,否则,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
。
我会尝试解决 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)