需要能够打印在node.js无头浏览器中创建的数据

问题描述 投票:0回答:1

如何解决问题才能打印出任何数据,例如。在运行无头浏览器的 Node.js 中创建的即时字符串、变量等,因为

console.log('Text')
无法工作。
例如是

 const pup = require('puppeteer');
 const chrom = await pup.launch({headless:'new'});
 const page = await chrom.newPage();
 await page.goto(aURI);

 await page.evaluate(() => {

              // <= How the code line to achieve this need

});
javascript node.js puppeteer console.log headless-browser
1个回答
0
投票

来自官方 puppeteer 文档的调试相关页面

由于客户端代码在浏览器中运行,因此在客户端代码中执行

console.*
不会直接登录到 Node.js。但是,您可以监听
console
事件,该事件返回包含记录文本的有效负载。

page.on('console', msg => console.log('PAGE LOG:', msg.text()));

await page.evaluate(() => console.log(`url is ${location.href}`));
© www.soinside.com 2019 - 2024. All rights reserved.