javascript - return value from puppeteer page.evaluate() - Stack Overflow

So, I have a function that returning page that needed by next function:async function browser(){const b

So, I have a function that returning page that needed by next function:

async function browser(){
        const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox'], headless: false, devtools : true});
        const incog = await browser.createIncognitoBrowserContext();
        const page = await incog.newPage();

            await page.goto('web')
            .then(function(){
       page.evaluate(function(){
            $(document).ready(function(){
                $('input[name ="username"]').val("a");
                $('input[name ="password"]').val("b");
                $(document).ready(function(){
                    $('#loginbtn').click();
                });
            });
        });
      });

      await page.waitForNavigation({waitUntil : 'load'});
      return page;

So, I pass the result value browser() by doing browser().then(result => nextFunction(result) that eventually passing page into nextFunction()


async function nextFunction(page){
    await page.goto('web')
       .then(function(){
        var msg = "Test : \n\n";
        page.evaluate(function(){
            var num = 1;
            $('.card').each(function(i, e){
                msg += "======= Activity "+num+" ========\n";
                msg += "Subject : " + $(this).find('.name').text() + "\n";
                msg += "Due : " + $(this).find('.date').text() + "\n";
                msg += "===== End Activity "+num+" ======\n\n";
                num++;
            });
        });
        console.log(msg);
       });
}

I tried to print msg from nextFunction(), but it only print Test:

What am I try to achieve is : Get msg result or assign variable from return value of nextFunction()

Is there any solution or better way to do this?

So, I have a function that returning page that needed by next function:

async function browser(){
        const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox'], headless: false, devtools : true});
        const incog = await browser.createIncognitoBrowserContext();
        const page = await incog.newPage();

            await page.goto('web')
            .then(function(){
       page.evaluate(function(){
            $(document).ready(function(){
                $('input[name ="username"]').val("a");
                $('input[name ="password"]').val("b");
                $(document).ready(function(){
                    $('#loginbtn').click();
                });
            });
        });
      });

      await page.waitForNavigation({waitUntil : 'load'});
      return page;

So, I pass the result value browser() by doing browser().then(result => nextFunction(result) that eventually passing page into nextFunction()


async function nextFunction(page){
    await page.goto('web')
       .then(function(){
        var msg = "Test : \n\n";
        page.evaluate(function(){
            var num = 1;
            $('.card').each(function(i, e){
                msg += "======= Activity "+num+" ========\n";
                msg += "Subject : " + $(this).find('.name').text() + "\n";
                msg += "Due : " + $(this).find('.date').text() + "\n";
                msg += "===== End Activity "+num+" ======\n\n";
                num++;
            });
        });
        console.log(msg);
       });
}

I tried to print msg from nextFunction(), but it only print Test:

What am I try to achieve is : Get msg result or assign variable from return value of nextFunction()

Is there any solution or better way to do this?

Share Improve this question asked Apr 8, 2020 at 5:01 sinyo1015sinyo1015 351 silver badge9 bronze badges 2
  • page.evaluate(function(){ must be awaited or .thenned. Using .then() syntax with Puppeteer is going to be painful. I suggest async/await. – ggorlen Commented Nov 24, 2022 at 3:30
  • Since msg isn't defined in the browser, msg += "======= Activity "+num+" ========\n"; should throw an error. How can I pass variable into an evaluate function? shows how to pass it in in general, but as this answer describes, in your case you can simply define it in the evaluate browser context and return it. – ggorlen Commented Jul 13, 2024 at 15:25
Add a ment  | 

1 Answer 1

Reset to default 8
  1. For a cleaner code and easier troubleshooting, pick a lane, either async/await or chaining with then. Using both makes the code difficult to read. async/await is more readable and less tricky for error handling. Read more here
  2. To see console.log in evaluate, listen on 'console' event:
const page = await browser.newPage();
page.on('console', msg => console.log(msg.text()));
  1. page.evaluate context is separate from Puppeteer, so evaluate's msg will be undefined. Move the msg to evaluate and then return the result back to puppeteer.
let msg = await page.evaluate(function(){
   let msg = "Test : \n\n";
   let num = 1;
   $('.card').each(function(i, e){
      msg += "======= Activity "+num+" ========\n";
      msg += "Subject : " + $(this).find('.name').text() + "\n";
      msg += "Due : " + $(this).find('.date').text() + "\n";
      msg += "===== End Activity "+num+" ======\n\n";
      num++;
   });
   return msg;
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313759a4420419.html

相关推荐

  • javascript - return value from puppeteer page.evaluate() - Stack Overflow

    So, I have a function that returning page that needed by next function:async function browser(){const b

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信