首页 > 解决方案 > 在 Puppeteer 中对 Firefox 的 setRequestInterception 支持

问题描述

setRequestInterception在 Puppeteer 上适用于 Firefox 吗?

在我的测试中,每当使用 Firefox 启动时,我都会得到:

Protocol error (Fetch.enable): Fetch.enable RemoteAgentError@chrome://remote/content/Error.jsm:25:5

await page.setRequestInterception(true);

我知道目前 Firefox 处于 Puppeteer 的实验模式。所以我预计这个功能可能不被支持。在 Firefox 上运行时,有没有其他方法可以处理请求(如更改请求、模拟响应)?

这就是我启动浏览器的方式(我可以看到 Firefox 窗口,并且所有其他测试都通过了):

browser = await puppeteer.launch({
  product: "firefox", 
  headless: false,
  slowMo: 50,
});

标签: firefoxpuppeteer

解决方案


Firefox 的 Puppeteer 尚不支持setRequestInterception您可以在https://puppeteer.github.io/ispuppeteerfirefoxready/跟踪通过测试

以下是我们启动 Firefox 的方式:

在安装过程中,选择 Puppeteer 版本 ^3.0.0

PUPPETEER_PRODUCT=firefox npm install

选项:

puppeteerOptions: {
        args: args,
        //ignoreDefaultArgs: ['--enable-automation'],
        headless: false,
        //slowMo: 17,
        //executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
        executablePath: './node_modules/puppeteer/.local-firefox/mac-79.0a1/Firefox Nightly.app/Contents/MacOS/firefox'
    },

推荐阅读