在Puppeteer中如何在控制台中捕获Chrome浏览器的日志。

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

我正在尝试收集Chrome浏览器的日志:浏览器发出的警告,如废弃和干预。例如,对于网站 https:/uriyaa.wixsite.comcorvid-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();
})();

bellow是不相关的,因为我想,因为reportingobserver不捕捉chrome信息的cookie没有sameSite:阅读上的主题,导致我 https:/developers.google.comwebupdates201807reportingobserver。 但我不知道如何使用它,在浏览器控制台中使用这个例子没有工作。

我不确定观察者代码应该在什么情况下使用,或者浏览器是否需要一个标志来激活报告API。或者说,这是否是得到它的方式。

欢迎帮助。

google-chrome google-chrome-devtools puppeteer
1个回答
1
投票

大概是 'console' 事件只抓 console.log() 和类似的页面调用。但似乎你可以通过浏览器的 CDPSession日志域. 不幸的是,它只对我的头部浏览器工作。

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch({ headless: false });
    const [page] = await browser.pages();
    const cdp = await page.target().createCDPSession();

    await cdp.send('Log.enable');

    cdp.on('Log.entryAdded', async ({ entry }) => {
      console.log(entry);
    });

    await page.goto('https://uriyaa.wixsite.com/corvid-cli2');
  } catch (err) {
    console.error(err);
  }
})();

而其中一个条目:

{
  source: 'other',
  level: 'warning',
  text: 'A cookie associated with a cross-site resource at http://www.wix.com/ was set without the `SameSite` attribute. It has been blocked, as Chrome now only delivers 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.',
  timestamp: 1589058118372.802,
  url: 'https://uriyaa.wixsite.com/corvid-cli2'
}
© www.soinside.com 2019 - 2024. All rights reserved.