Javascript 运行时未执行Puppeter脚本中的求值函数
我遇到了一个我不理解的情况。我正在使用evaluate函数返回应用程序下拉列表中的链接。我传入一个变量数组,我想用它在下拉列表中从这些部分检索链接。 调用evaluate函数时不运行。我不明白为什么会这样 这是我的代码,任何帮助都将不胜感激Javascript 运行时未执行Puppeter脚本中的求值函数,javascript,puppeteer,Javascript,Puppeteer,我遇到了一个我不理解的情况。我正在使用evaluate函数返回应用程序下拉列表中的链接。我传入一个变量数组,我想用它在下拉列表中从这些部分检索链接。 调用evaluate函数时不运行。我不明白为什么会这样 这是我的代码,任何帮助都将不胜感激 const selectAccount = await page.evaluate((depositAndCreditAccountIdentifier) => { console.log(`depositAndCreditAccoun
const selectAccount = await page.evaluate((depositAndCreditAccountIdentifier) => {
console.log(`depositAndCreditAccountIdentifier ${depositAndCreditAccountIdentifier}`);
var returnLinks = [];
for(dn in depositAndCreditAccountIdentifier){
sectionIdentifier = depositAndCreditAccountIdentifier[dn];
var varContainer = $('div[ng-if="' + sectionIdentifier + '"]>div[class="multiselect-collection"]>a');
returnLinks.push(varContainer);
varContainer.each(function(elementIndex){
returnLinks.push(varContainer[elementIndex].getAttribute('data-test'));
});
}
console.log(`returnLinks ${returnLinks}`);
return returnLinks;
}, depositAndCreditAccountIdentifier);
console.log(`selectAccount: ${selectAccount}`);
好,
我发现它确实执行代码,并将信息记录在浏览器控制台中。由于这个问题:
看来我的问题也一样。基于此console.log('returnLinks${returnLinks}'),返回中没有任何内容代码>
我尝试了一种不同的方法。我要回报一个承诺,我要寻找的一切都被收集起来了,但什么也没有回报
exports.returnLinks = async function(page, sectionIdentifier) {
console.log(`sectionIdentifier: ${sectionIdentifier}`);
await page.evaluate(async (sectionIdentifier) => {
function grabAccountAttributes(sectionIdentifier){
return new Promise((resolve, reject)=> {
var returnLinks = [];
var varContainer = $('div[ng-if="' + sectionIdentifier + '"]>div[class="multiselect-collection"]>a');
console.log(`varContainer: ${varContainer}`);
varContainer.each(function(elementIndex){
var linkItem = varContainer[elementIndex].getAttribute('data-test');
console.log(`linkItem: ${linkItem}`);
returnLinks.push(linkItem);
});
setTimeout(()=>{
resolve(true)
}, 3000);
});
}
var linksAttributes = await grabAccountAttributes(sectionIdentifier);
// console.log(`returnLinks ${returnLinks}`);
return linksAttributes;
}, sectionIdentifier);
grabAccountAttributes
解析为true,则不返回returnLinks。而且你不需要承诺。你说退货中没有任何东西是什么意思?是未定义的数组还是空数组?@mbit在我的第一个示例中,selectAccount返回未定义的。确保returnLinks为,page。如果是,则evaluate
解析为未定义not@mbit,我不相信它是可序列化的。我在函数调用中添加了JSON.stringify,它返回为未定义。我仍在继续学习JS,所以我可能做错了。txt=JSON.stringify(等待getPageContent.returnLinks(页面、存款和信用账户标识符[i]);如果它不可序列化,则可以重写该函数,并使用page.$()
在傀儡程序上下文中执行它。您也可以尝试page.evaluateHandle
而不是page.evaluate
,因为它在页面对象中返回。