我正在尝试收集 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 浏览器时,在选项对象中,您应该设置 dumpio
至 true
:
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/