你如何安装和 运行 puppeteer for firefox

how do you install and run puppeteer for firefox

您好,我正在做一些网络自动化。我正在尝试打开 url,但在 chrome 控制台中收到数据 URL 错误,因此我将转到 firefox 控制台以避开无数据 url 的打开在 chrome 控制台问题中。问题是“npm install puppeteer-firefox”无法为 firefox 安装 puppeteer。如何为 firefox 安装 puppeteer 并将其包含在代码中以便我可以使用它?

由于数据 url 错误,代码无法在 chrome 中运行

const url = await page.evaluate(async () => {

    
        document.querySelector('.n3VNCb').src;
        
    });
    
    url.toString();
    
    await page.goto(url);

我在 node.js 命令提示符中输入的内容来安装 puppeteer firefox。这没有用

npm i puppeteer-firefox

我从 node.js 命令提示符收到错误

npm WARN deprecated puppeteer-firefox@0.5.1: Firefox support is gradually transitioning to the puppeteer package. As of puppeteer v2.1.0 you can interact with Firefox Nightly. The puppeteer-firefox > package will remain available until the transition is complete, but it is no longer actively maintained. For more information visit https://wiki.mozilla.org/Remote

puppeteer-firefox@0.5.1 install C:\Users\user\Desktop\filename\filename\node_modules\puppeteer-firefox node install.js

ERROR: Failed to download Firefox rv0.0.1! Error: Download failed: server returned code 404. URL: https://github.com/puppeteer/juggler/releases/download/v0.0.1/firefox-win64.zip

我也试过这些作为一个错误说要做,但它们没有用

(节点:14348)UnhandledPromiseRejectionWarning:错误:找不到最新的浏览器修订版。 运行“PUPPETEER_PRODUCT=firefox npm install”或“PUPPETEER_PRODUCT=firefox yarn install”下载受支持的 Firefox 浏览器二进制文件。

PUPPETEER_PRODUCT=firefox npm 安装

PUPPETEER_PRODUCT=firefox 纱线安装

请不要再使用 puppeteer-firefox 包。它已被弃用。相反,只需安装 puppeteerFirefox as selected product. Here an example from the puppeteer repository 即可显示如何从您的测试脚本启动 Firefox。

我有一个类似的问题,你必须运行的命令实际上是下面的:

PUPPETEER_PRODUCT=firefox npm i puppeteer

来源:https://github.com/puppeteer/puppeteer/blob/v5.2.1/docs/api.md#puppeteerlaunchoptions

键入以下命令以找到您的浏览器

whereis firefox
OR
whereis google-chrome

我用过 chrome 所以,我的是。可以换成firefox路径。

/usr/bin/google-chrome

因此,最后一步。

export PUPPETEER_EXECUTABLE_PATH='/usr/bin/google-chrome'

就是这样:)

注:

您应该将此变量添加到您的 shell 配置中,如 ~/.bashrc~/.zshrc 否则在重新启动后,您将丢失此变量值。或在 /etc/environment

的全球范围内

更新:

以上答案过去对我有用,但以下是最合适的解决方案,仍然适用于 firefox。

为 puppeteer 安装 firefox。

npm i puppeteer-firefox

代码示例经过测试并适用于 chrome 和 firefox。

const puppeteerChrome = require('puppeteer');
const puppeteerFirefox = require('puppeteer-firefox');

(async () => {

    const test = async browser => {
        const page = await browser.newPage();
        await page.setViewport({
            width: 1280,
            height: 800
        });
        await page.goto('https://www.bbc.com/news');   
        await page.hover('#nw-c-most-read-heading__title');
        await page.screenshot({ path: 'bcc-most-read.png' })
        
        await browser.close();
    }

    const chrome = await puppeteerChrome.launch({
        headless: false,
        slowMo: 50
    });
    await test(chrome);

    const firefox = await puppeteerFirefox.launch({
        headless: false,
        slowMo: 50
    });
    await test(firefox);

})();

您也可以尝试从 node_modules 文件夹和

中删除 puppeteer
npm install

对我有用

个人对 Puppeteer 有疑问。无法在 ubuntu 上安装它。只是空.local-firefox。 Playwright 里面有 webkit、chromium 和 firefox。完全没有问题。很管用