Back to Blog
RPA
How to Use Puppeteer to Automate RPA Operations in Nstbrowser?
What is Nstbrowser? Is it difficult to configure Puppeteer to Nstbrowser RPA? Begin reading and enjoy an effective automation.
Apr 02, 2024Carlos Rivera

In this tutorial, we will explore how to use Puppeteer to automate RPA operations in Nstbrowser. By following this tutorial, you will:

  • Gain a fundamental understanding of Puppeteer's capabilities for RPA automation
  • 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.

What Is RPA?

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 use internal application program interfaces (APIs) or specialized scripting languages as interfaces to back-end systems. Some software may not have an API for such purposes, and RPA can lower the bar for automation.

Searching for a way to manage multiple accounts effectively?
The RPA of Nstbrowser can help

What Is Nstbrowser?

Nstbrowser is a Free Anti-Detection Browser that simplifies Web Scraping and Automation. It integrates with Anti-detect, Web Unblocker, and Intelligent Proxies.

Besides, Nstbrowser also supports Cloud Container Clusters, Browserless, and an enterprise-grade cloud browser solution compatible with Windows/Mac/Linux. Obviously, it allows a seamless automation even when traditional APIs or scripting languages are not available.

Keep scrolling!

Time to use Puppeteer to automate RPA operations.

Prerequisites:

Before you start, ensure you have the following:

  • Nstbrowser client: You need to start and log in to it.
  • Node.js (or nvm) and npm: You have to install them 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.
bash Copy
npm init -y
npm install puppeteer-core

Connecting Puppeteer with Nstbrowser

Step 1: Initialize your project

Create a new project directory and navigate into it. Then, initialize a new Node.js project and install puppeteer-core:

bash Copy
mkdir my-automation-project
cd my-automation-project
npm init -y
npm install puppeteer-core

Step 2: Obtain your API key and ProfileID

Before starting our work, you have to get the API key and ProfileID.

  • How to obtain the API Key? Log in to Nstbrowser and navigate to the API menu.
API key in Nstbrowser
  • How to get the ProfileID? Go to the Profiles menu:
ProfileID in Nstbrowser

After successfully obtaining the API Key and ProfileId, let's set up Puppeteer to connect with Nstbrowser using your API Key and ProfileId.

Step 3. Configure Puppeteer

Create a JavaScript file (e.g., index.js) in your project directory and add the following code:

javascript Copy
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'; // Replace with your actual 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(); // Replace with your actual ProfileId
  • Note: Replace your api key and your profile id with your actual API Key and ProfileId.

Automatically logging in to your Amazon account

Step 4: Automate Amazon login

After connecting to the Nstbrowser browser, you can perform some actions that you want to implement in RPA, such as automatically logging into your Amazon account. Update the execPuppeteer function to include steps:

javascript Copy
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);
  }
}
  • Note: Replace your account and your password with your actual Amazon login credentials.

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

javascript Copy
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.

By leveraging Puppeteer and Nstbrowser, you can create efficient, automated workflows to handle repetitive tasks, manage multiple accounts, and perform sophisticated web automation tasks.

More