In this tutorial, we will explore how to use Puppeteer to automate RPA operations in Nstbrowser. By following this tutorial, you will:
So let's dive in and unlock the potential of RPA with Puppeteer and Nstbrowser.
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
Do you have any wonderful ideas and doubts about web scraping and Browserless?
Let's see what other developers are sharing on Discord and Telegram!
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.
Before you start, ensure you have the following:
npm install
. It will create a package.json
file containing all the dependencies.npm init -y
npm install puppeteer-core
Create a new project directory and navigate into it. Then, initialize a new Node.js
project and install puppeteer-core:
mkdir my-automation-project
cd my-automation-project
npm init -y
npm install puppeteer-core
Before starting our work, you have to get the API key and ProfileID.
After successfully obtaining the API Key and ProfileId, let's set up Puppeteer to connect with Nstbrowser using your API Key and ProfileId.
Create a JavaScript file (e.g., index.js) in your project directory and add the following code:
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
your api key
and your profile id
with your actual API Key and ProfileId.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:
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);
}
}
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.
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();
We hope you have shared three key points through this tutorial:
By leveraging Puppeteer and Nstbrowser, you can create efficient, automated workflows to handle repetitive tasks, manage multiple accounts, and perform sophisticated web automation tasks.