Puppeteer-web:不允许协议错误(目标:getBrowserContexts) [英] Puppeteer-web: Protocol error (Target: getBrowserContexts) not allowed

查看:28
本文介绍了Puppeteer-web:不允许协议错误(目标:getBrowserContexts)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个试图实现 puppeteer-web 的 chrome 扩展.我已按照以下代码尝试进行设置puppeteer-web:Puppeteer 不是构造函数"

I've got a chrome extension that is trying to implement puppeteer-web. I've followed the following code to try and get set up puppeteer-web: "Puppeteer is not a constructor"

这是我的代码:

const puppeteer = require("puppeteer");
async function initiatePuppeteer() {
  let browserWSEndpoint = '';
  await fetch("http://127.0.0.1:9222/json")
    .then(response => response.json())
    .then(function(data) {
        let filteredData = data.filter(tab => tab.type ==='page');
        browserWSEndpoint = filteredData[0].webSocketDebuggerUrl;
      })
    .catch(error => console.log(error));
    const browser = await puppeteer.connect({
      browserWSEndpoint: browserWSEndpoint
    });
    const page = await browser.newPage();
    ....etc
}

代码似乎没有超过这一点,因为当我在 const browser = await puppeteer.connect 上放置调试器时,我收到错误

It doesn't seem the code makes it past this point as when I put a debugger at const browser = await puppeteer.connect I get the error

Uncaught (in promise) Error: Protocol error (Target.getBrowserContexts): Not allowed.

使用 Chrome 版本 V76.0.3809.100有什么想法吗?

Using Chrome version V76.0.3809.100 Any ideas?

我的 webSocketDebuggerUrl 类似于 ws://127.0.0.1:9222/devtools/page/E1B62B356262B00C26A5D79D03745360

my webSocketDebuggerUrl is something like ws://127.0.0.1:9222/devtools/page/E1B62B356262B00C26A5D79D03745360

而且我怀疑这是因为它是 /page/ 而不是 /browser/ 但我无法从/json 路由中找到任何类型的浏览器.今晚我再看看.

And I suspect it's because it's /page/ and not /browser/ but I couldn't find any of type browser from the /json route. I'll give it another look at it tonight.

推荐答案

Ok 所以事实证明 puppeteer 只能连接到 browser 目标而不是 page 目标作为根据 https://github.com/GoogleChrome/puppeteer/issues/4579#issuecomment-511267351

Ok so it turns out puppeteer can only connect to a browser target and not a page target as per https://github.com/GoogleChrome/puppeteer/issues/4579#issuecomment-511267351

并阅读 API 的文档(我应该早点完成...)https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserwsendpoint 指出 browser 目标的 URL 是 http://${host}:${port}/json/version 而不是 /json

And reading the documentation for the API (which I should have done earlier...) https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#browserwsendpoint states that URL for the browser target is http://${host}:${port}/json/version instead of just /json

这篇关于Puppeteer-web:不允许协议错误(目标:getBrowserContexts)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆