I've run into a situation that I do not understand. I am using the evaluate function to return links within a drop down in our application. I pass in an variable array that I would like to use to retrieve links from these sections in a drop down. The evaluate function does not run when called. I do not understand why this is happening.
Here is my code and any help would be appreciated.
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}`);
Okay,
I have found that it does execute the code logs the information in the browser console. Thanks to this question: Puppeteer: pass variable in .evaluate()
And it seems my issue is the same. The return does not have anything in it based on this console.log('returnLinks ${returnLinks}');
.
I tried a different approach to this. I am returning a Promise() which again everything is gathered that I am looking for but nothing is being returned.
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);