How to Capture .PNG Screen Shot using 0Browser, a Headless Browser as a Service

How to Capture PNG

In this example we will learn how to capture a .PNG screenshot of Y-Combinator news page using JavaScript, Puppeteer and 0Browser.

Prerequisites

Make sure you have Node.js installed and Puppeteer added.
Also keep your 0Browser Account API Key handy.
If something doesn't add up here, please check out the 0Browser Getting Started guide for all details on this.

Setup

Now that we have the prerequisites in order, let's get to the fun part!

Use your favorite IDE to create a new JS file called index.js.
Following code snippet shows how to connect to 0Browser proxy to navigate to the y-combinator news page and capture a .PNG screenshot.

const puppeteer = require('puppeteer-core');

(async () => {
    const browser = await puppeteer.connect({
        browserWSEndpoint: 
        'wss://proxy.0browser.com?token={YOUR-API-KEY}&timeout=60000',
    });

    const page = await browser.newPage();
    await page.goto('https://news.ycombinator.com');
    await page.screenshot({ path: 'capture.png' });

    await browser.close();
})();

Run

Use NodeJS to run the script using below command.

 > node index.js

Then you will find a new file called capture.png in the same directory as index.js script lives.
Thats it, Great job!


...
0Browser