如何解决问题才能打印出任何数据,例如。在运行无头浏览器的 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
});
来自官方 puppeteer 文档的调试相关页面。
由于客户端代码在浏览器中运行,因此在客户端代码中执行
不会直接登录到 Node.js。但是,您可以监听console.*
事件,该事件返回包含记录文本的有效负载。console
page.on('console', msg => console.log('PAGE LOG:', msg.text()));
await page.evaluate(() => console.log(`url is ${location.href}`));