google-chrome - 在 Puppeteer 中如何在控制台中捕获 Chrome 浏览器日志

标签 google-chrome google-chrome-devtools puppeteer

我正在尝试收集 Chrome 浏览器日志:浏览器发出的警告,例如弃用和干预。例如,对于站点 https://uriyaa.wixsite.com/corvid-cli2 :

A cookie associated with a cross-site resource at http://wix.com/ was set without the `SameSite` attribute.
A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`.
You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

我认为以下代码可以解决问题,但它只捕获页面代码生成的日志。
(async ()=> {
    const browser = await puppeteer.launch({dumpio: true});
    const page = await browser.newPage();
    page.on('console', msg => {
        for (let i = 0; i < msg._args.length; ++i)
            console.log(`${i}: ${msg._args[i]}`);
    });
    await page.goto('https://uriyaa.wixsite.com/corvid-cli2', {waitUntil: 'networkidle2', timeout: 20000});
    await page.screenshot({path: 'screenshot.png'});
    await browser.close();
})();

波纹管是不相关的,因为我认为报告观察者不会在没有 sameSite 的情况下捕获 cookie 上的 chrome 信息:
关于这个主题的阅读让我找到了 https://developers.google.com/web/updates/2018/07/reportingobserver但我不确定如何使用它,使用浏览器控制台中的示例不起作用。

我不确定应该在哪个上下文中使用观察者代码,或者浏览器是否需要一个标志来激活报告 API。或者,如果这是解决问题的方法。

欢迎帮助。

最佳答案

当您启动 Puppeteer 浏览器时,在选项对象中,您应该设置 dumpiotrue :

await puppeteer.launch({ dumpio: true });
这基本上将“将浏览器进程 stdout 和 stderr 导入 process.stdout 和 process.stderr”,这意味着它将浏览器日志重定向到您正在运行的任何主进程、服务器等。
您可以在此处查看启动 Puppeteer 时可以使用的此启动选项和其他启动选项:https://www.puppeteersharp.com/api/PuppeteerSharp.LaunchOptions.html

关于google-chrome - 在 Puppeteer 中如何在控制台中捕获 Chrome 浏览器日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61629590/

相关文章:

Excel 超链接需要不必要的额外登录

java - Applet java 用 chrome 抛出异常

google-chrome - 禁用 Google Chrome 开发者工具中的所有请求

google-chrome - 谷歌浏览器 headless 远程调试/json url无响应

google-chrome - 如何在 puppeteer 中发送 POST 请求?

javascript - 如何使用 Puppeteer 从输入中删除现有文本?

Facebook JS SDK 渐进式网络应用程序问题

javascript - ngStorage 在 Chrome iframe 中崩溃

javascript - 无法在 Chrome 开发者工具中实时编辑 Javascript

ubuntu - Ubuntu 和 Puppeteer 没有 srvflx 地址