javascript - 带有 Chrome 可执行文件的 Puppeteer 中的外部资源无法加载 (net::ERR_EMPTY_RESPONSE)

标签 javascript google-chrome puppeteer

我在使用完整 Chrome 可执行文件(不是默认 Chromium)运行的 Puppeteer 作业中使用外部资源时遇到问题。任何帮助将不胜感激!
因此,例如,如果我使用公共(public) URL 加载视频,即使我在浏览器中手动点击它也能正常工作,它也会失败。

const videoElement = document.createElement('video');
videoElement.src = src;
videoElement.onloadedmetadata = function() {
  console.log(videoElement.duration);
};
这是我的 Puppeteer 电话:
(async () => {
  const browser = await puppeteer.launch({
    args: [
      '--remote-debugging-port=9222',
      '--autoplay-policy=no-user-gesture-required',
      '--allow-insecure-localhost',
      '--proxy-server=http://localhost:9000',
      '--proxy-bypass-list=""',
      '--no-sandbox', 
      '--disable-setuid-sandbox',
    ],
    executablePath:
      '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
  });

  const page = await browser.newPage();
  logConsole(page);
  
  await page.goto(`http://${hostname}/${path}`, {
    waitUntil: 'networkidle2',
  });
  
  await page.waitForSelector('#job-complete');
  console.log('Job complete!');

  await browser.close();
})();
与许多 Puppeteer 示例不同,这里的问题不在于我的测试没有等待足够长的时间。资源几乎无法立即加载/返回空响应。
它似乎也不是身份验证问题 - 我可以很好地访问自己的服务器。
虽然我这里没有在 https 上运行,但我直接在浏览器中尝试的 URL 可以在没有 SSL 的情况下工作。
我还应该提到这是一个 React (CRA) 网站,我正在使用 Node.js 调用 Puppeteer。
我可以看到至少 3 个其他外部资源(非视频)也失败了。是否有我遗漏的标志或我应该使用的东西?非常感谢您的帮助!

最佳答案

就我而言,我不得不使用 puppeteer-extra 和 puppeteer-extra-plugin-stealth:

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
我还发现以下标志很有用:
const browser = await puppeteer.launch({
    args: [
      '--disable-web-security',
      '--autoplay-policy=no-user-gesture-required',
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--remote-debugging-port=9222',
      '--allow-insecure-localhost',
    ],
    executablePath:
      '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
  });
最后,我发现在某些情况下有必要绕过 CSP:
await page.setBypassCSP(true);
请小心使用这些相当不安全的设置😬

关于javascript - 带有 Chrome 可执行文件的 Puppeteer 中的外部资源无法加载 (net::ERR_EMPTY_RESPONSE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64507895/

相关文章:

javascript - 使用 Chrome 扩展动态重新加载页面重新注入(inject)内容脚本

javascript - 通过管道而不是 websocket 连接 Puppeteer 的优点和缺点是什么

javascript - 网页宽度与屏幕宽度不匹配

javascript - 在 DOM 交互期间暂停布局

javascript - 匹配不包含特定模式的特定字符串

android - 在 google chrome 浏览器中 React Native Android 深度链接

JavaScript - 如何获得 PHP 服务器的正确时间?

javascript - Puppeteer 没有正常工作(浏览器未聚焦时暂停/卡住)

c# - PuppeteerSharp 和页面级代理

javascript - 使用appendchild将一个div添加到另一个div