Upravit

Sdílet prostřednictvím


Puppeteer overview

The Puppeteer library provides a high-level API to control Chromium-based browsers, including Microsoft Edge, by using the DevTools Protocol.

Puppeteer launches headless browsers by default. Headless browsers don't display a user interface (UI), so you must use the command line. You can also configure Puppeteer to run full (non-headless) Microsoft Edge.

By default, when you install Puppeteer, the installer downloads a recent version of Chromium, the open-source browser that Microsoft Edge is also built upon.

If you have Microsoft Edge installed, you can use puppeteer-core. puppeteer-core is a lightweight version of Puppeteer that launches an existing browser installation, like Microsoft Edge. To download Microsoft Edge, go to Download Microsoft Edge Insider Channels.

Puppeteer is a Node library.

Installing puppeteer-core

You can add puppeteer-core to your website or app by using one of the following commands:

npm i puppeteer-core
yarn add puppeteer-core

Launch Microsoft Edge with puppeteer-core

puppeteer-core is similar to other browser-testing-frameworks, such as WebDriver. You create an instance of the browser, open a webpage, and then manipulate the webpage by using the Puppeteer API.

To use puppeteer-core to launch Microsoft Edge:

  1. puppeteer-core requires Node v8.9.0 or later. Make sure you have a compatible version of Node.js. To do this, run node -v from the command line. Also, the example below uses async/await, which is only supported in Node v7.6.0 or later.

  2. In the following code sample, puppeteer-core launches Microsoft Edge, goes to https://www.microsoft.com/edge/download/insider, and saves a screenshot as example.png. Copy the following code snippet and save it as example.js:

    const puppeteer = require('puppeteer-core');
    
    (async () => {
      const browser = await puppeteer.launch({
        executablePath: 'C:\\Program Files (x86)\\Microsoft\\Edge Dev\\Application\\msedge.exe'
      });
      const page = await browser.newPage();
      await page.goto('https://www.microsoft.com/edge/download/insider');
      await page.screenshot({path: 'example.png'});
    
      await browser.close();
    })();
    
  3. Follow the next steps to find the executable path and then change executablePath to point to your installation of Microsoft Edge. For example, on macOS, the executablePath for Microsoft Edge Canary should be set to /Applications/Microsoft\ Edge\ Canary.app/.

  4. To find the executablePath, a simple manual approach is to go to edge://version and copy the Executable path on that page.

  5. Or, to programmatically find the executable path, first install the edge-paths package by running one of the following commands:

    npm i edge-paths
    
    yarn add edge-paths
    
  6. Then, if you're using edge-paths to find the executable path, run code like the following sample. It uses the edge-paths package to programmatically find the path to your installation of Microsoft Edge on your OS:

    const edgePaths = require("edge-paths");
    
    const EDGE_PATH = edgePaths.getEdgePath();
    
  7. Now that you've found the executable path (either manually or programmatically), in example.js, set executablePath: EDGE_PATH. Save your changes.

  8. Run example.js from the command line:

    node example.js
    

    puppeteer-core launches Microsoft Edge, goes to https://www.microsoft.com/edge/download/insider, and saves a screenshot of the webpage. You can customize the screenshot size by calling page.setViewport().

    The following example.png file is produced by example.js:

    The example.png file produced by example.js

The preceding example demonstrates basic automation and testing scenarios that you can cover using Puppeteer and puppeteer-core. For more information about Puppeteer and how it works, check out Puppeteer.

See also

Local articles

Archive

Blog posts

Tools

Protocols

Info