0

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);
Joe
  • 743
  • 5
  • 10
  • 26
  • `grabAccountAttributes` resolves to true, you're not returning the returnLinks. plus you don't need a promise here. what do you mean return does not have anything in it? is it undefined or empty array? – mbit Jan 02 '20 at 20:51
  • @mbit In my first example *selectAccount* returns undefined. – Joe Jan 03 '20 at 13:28
  • make sure returnLinks is [serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description), `page.evaluate` resolves to undefined if it's not – mbit Jan 03 '20 at 18:47
  • @mbit, I do not believe it is serializable. I added *JSON.stringify* to the call to the function and it returned as *undefined*. I am still getting back into JS so I maybe do this wrong. *txt = JSON.stringify(await getPageContent.returnLinks(page, depositAndCreditAccountIdentifier[i]));* – Joe Jan 06 '20 at 13:58
  • if it is not serializable, you can rather rewrite the function and do it in the puppeteer context using `page.$()`. you can also try `page.evaluateHandle` instead of `page.evaluate` as it returns in-page objects. – mbit Jan 06 '20 at 18:46

0 Answers0