🎉All features are free and unlimited! Enjoy unlimited profiles and teams for $0
Started Free
Back to Blog
Nst Blogger
Web Scraping
RPA
How to use Puppeter to automate RPA operations in Nstbrowser
In traditional workflow automation tools, automation tasks are typically generated by programmers...
Apr 02, 2024

RPA (Robotic Process Automation) is a technology for automating business processes based on software robots and artificial intelligence.

In traditional workflow automation tools, automation tasks are typically generated by programmers, and internal application interfaces (APIs) or specialized scripting languages are used as interfaces with backend systems. Some software may not have APIs for such purposes, and RPA can lower the barrier to automation.

In this tutorial, we will explore how to use Puppeteer, a popular automation framework, to automate RPA operations in Nstbrowser. Nstbrowser is a client that facilitates the integration of RPA with various applications, allowing seamless automation even when traditional APIs or scripting languages are not available. By following this tutorial, you will gain a fundamental understanding of Puppeteer's capabilities for RPA automation and learn how to harness its power to enhance your business processes. So let's dive in and unlock the potential of RPA with Puppeteer and Nstbrowser.

Prerequisites:

  • You need to start and log in to the Nstbrowser client.
  • You need to have Node.js (or nvm) and npm installed to make the code work correctly.
  • Afterward, install all necessary libraries by running npm install. It will create a package.json file containing all the dependencies.
npm init -y
npm install puppeteer-core

Connecting Puppeteer with Nstbrowser

First, we need you to obtain your API Key and the ProfileId you want to connect to.

How to obtain the API Key? Log in to Nstbrowser and navigate to the API menu.


How to get the ProfileId? Go to the Profiles menu:

After successfully obtaining the API Key and ProfileId, we start connecting to Nstbrowser using Puppeteer:

import puppeteer from 'puppeteer-core';

async function execPuppeteer(browserWSEndpoint) {
  try {
    const browser = await puppeteer.connect({
      browserWSEndpoint: browserWSEndpoint,
      defaultViewport: null,
    });
    await browser.newPage();
  } catch (err) {
    console.error('launch', err);
  }
}

async function launchAndConnectToBrowser(profileId) {
  const host = 'localhost:8848';
  const apiKey = 'your api key';
  const config = {
    headless: false, // support: true, 'new'
    autoClose: true,
  };
  const query = new URLSearchParams({
    'x-api-key': apiKey, // required
    config: encodeURIComponent(JSON.stringify((config))),
  });
  const browserWSEndpoint = `ws://${host}/devtool/launch/${profileId}?${query.toString()}`;
  await execPuppeteer(browserWSEndpoint);
}

launchAndConnectToBrowser('your profile id').then();

Automatically logging in to your Amazon account

After connecting to the Nstbrowser browser, you can perform some actions that you want to implement RPA, such as automatically logging into your Amazon account:

async function execPuppeteer(browserWSEndpoint) {
  try {
    const browser = await puppeteer.connect({
      browserWSEndpoint: browserWSEndpoint,
      defaultViewport: null,
    });
    const page = await browser.newPage();
    await page.goto('https://www.amazon.com');
 
    // go to sign in page
    const signInSelector = '#nav-link-accountList';
    await page.waitForSelector(signInSelector);
    await page.click(signInSelector);
    
    // type your account
    const accountInputSelector = '#ap_email';
    await page.waitForSelector(accountInputSelector);
    await page.type(accountInputSelector, 'your account');
    
    // click continue button
    const continueSelector = 'input#continue';
    await page.waitForSelector(continueSelector);
    await page.click(continueSelector);
    
    // type your password
    const passowrdInputSelector = '#ap_password';
    await page.waitForSelector(passowrdInputSelector);
    await page.type(passowrdInputSelector, 'your password');
    
    // click submit button
    const signInSubmitSelector = '#signInSubmit';
    await page.waitForSelector(signInSubmitSelector);
    await page.click(signInSubmitSelector);
  } catch (err) {
    console.error('launch', err);
  }
}

This way you have successfully logged into your Amazon account automatically. This is just a very common and basic operation of RPA function, more RPA scenarios are waiting for you to discover.

Full Code

import puppeteer from 'puppeteer-core';

async function execPuppeteer(browserWSEndpoint) {
  try {
    const browser = await puppeteer.connect({
      browserWSEndpoint: browserWSEndpoint,
      defaultViewport: null,
    });
    const page = await browser.newPage();
    await page.goto('https://www.amazon.com');
 
    // Go to sign-in page
    const signInSelector = '#nav-link-accountList';
    await page.waitForSelector(signInSelector);
    await page.click(signInSelector);
    
    // Type your account
    const accountInputSelector = '#ap_email';
    await page.waitForSelector(accountInputSelector);
    await page.type(accountInputSelector, 'your account');
    
    // Click continue button
    const continueSelector = 'input#continue';
    await page.waitForSelector(continueSelector);
    await page.click(continueSelector);
    
    // Type your password
    const passwordInputSelector = '#ap_password';
    await page.waitForSelector(passwordInputSelector);
    await page.type(passwordInputSelector, 'your password');
    
    // Click submit button
    const signInSubmitSelector = '#signInSubmit';
    await page.waitForSelector(signInSubmitSelector);
    await page.click(signInSubmitSelector);
  } catch (err) {
    console.error('launch', err);
  }
}

async function launchAndConnectToBrowser(profileId) {
  const host = 'localhost:8848';
  const apiKey = 'your api key';
  const config = {
    headless: false, // support: true, 'new'
    autoClose: true,
  };
  const query = new URLSearchParams({
    'x-api-key': apiKey, // required
    config: encodeURIComponent(JSON.stringify((config))),
  });
  const browserWSEndpoint = `ws://${host}/devtool/launch/${profileId}?${query.toString()}`;
  await execPuppeteer(browserWSEndpoint);
}

launchAndConnectToBrowser('your profile id').then();

Conclusion

We hope you have shared three key points through this tutorial:

  1. Understand the basics of Puppeteer automation
  2. Understand how to use Puppeteer to connect to Nstbrowser.
  3. Be able to discover more about RPA scenarios.
More