木偶页面。评估querySelectorAll始终为空

时间:2020-05-02 19:11:44

标签: node.js

我有一个使用puppeteer的节点项目。 我在控制台中运行以下代码,并获得170个结果

circlenod = window.document.querySelectorAll('area[templateid="ucChart_pnlip"]');
    for (var i = 0; i < circlenod.length; i++) {
        console.log('circlenod --> : ', circlenod[i]);
    }

但是,当我尝试使用人偶或$$方法进行页面评估时,却没有返回结果

let test;
let list = await page.$$('area[templateid="ucChart_pnlip"]');
console.log('list ===> ', list); ===> this is empty
await page.evaluate(() => {
    console.log('coming in ??'); ==> never see this 
    test = Array.from(document.querySelectorAll('area[templateid="ucChart_pnlip"]'));
    for (var i = 0; i < test.length; i++) {
        console.log('circlenod --> : ', test[i]); ==> never see this
    }
 })
 console.log('test', test); ==> undefined 

这是元素的示例。遍历所有<area /> fields="date|5/1/2020|14"

后,如何从属性中提取此信息
<area shape="poly" coords="802,235,807,233,807,241,802,243" fields="date|5/1/2020|14" templateid="ucChart_pnlip" href="../../../../../../UserControls/History/Single/#" onclick="charttip_Show(this, event);return false" alt="">

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我把它放在那里供任何需要的人使用。当获得元素数组时,需要具体化所需元素的属性

let test= await page.evaluate((sele) =>{

    const elements = Array.from(document.querySelectorAll(sele))
    let links = elements.map(element=>{
        return element.getAttribute('fields');
    })
    return links;
    },'area[templateid="ucChart_pnlip"]')

console.log(test)