Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

RPA (Robotic Process Automation) using JavaScript

RPA (Robotic Process Automation) using JavaScript typically involves automating web-based tasks such as filling forms, scraping data, or interacting with a website. JavaScript can be used alongside browser automation tools like Puppeteer or Playwright.

 

Below is a sample project to demonstrate RPA using JavaScript:


Project Overview: Automating Google Search

Objective: Create a JavaScript-based RPA script that opens Google, searches for a term, and extracts the titles of the first few search results.

Tools:

  1. Node.js: To run JavaScript outside the browser.
  2. Puppeteer: A Node.js library for controlling a headless browser.

Steps to Build the RPA Script

1. Prerequisites

  • Install Node.js.
  • Create a project folder and navigate to it:
    mkdir rpa-js && cd rpa-js
    
  • Initialize a Node.js project:
    npm init -y
    
  • Install Puppeteer:
    npm install puppeteer
    

2. JavaScript Code

Create a file named

rpa_google_search.js
and add the following code:

const puppeteer = require('puppeteer');

(async () => {
 try {
 // Launch the browser
 const browser = await puppeteer.launch({ headless: true }); // headless:true means no UI
 const page = await browser.newPage();

 // Navigate to Google
 await page.goto('https://www.google.com');

 // Wait for the search box to load
 await page.waitForSelector('input[name="q"]');

 // Type the search query and press Enter
 const searchQuery = 'RPA using JavaScript';
 await page.type('input[name="q"]', searchQuery);
 await page.keyboard.press('Enter');

 // Wait for the results page to load
 await page.waitForSelector('#search');

 // Extract search result titles
 const results = await page.evaluate(() => {
 const titles = [];
 const elements = document.querySelectorAll('h3'); // Get all result titles
 elements.forEach(element => {
 if (element.innerText) titles.push(element.innerText);
 });
 return titles;
 });

 console.log(`Search Results for "${searchQuery}":`);
 console.log(results);

 // Close the browser
 await browser.close();
 } catch (error) {
 console.error('Error:', error);
 }
})();

3. Run the Script

  • Execute the script in the terminal:
    node rpa_google_search.js
    
  • The script will log the titles of the search results to the console.

How It Works

  1. Browser Automation: Puppeteer launches a headless version of Chrome.
  2. Navigation: The script navigates to Google and performs a search.
  3. Interaction: It types the query into the search bar and simulates pressing "Enter."
  4. Data Extraction: The script uses DOM selectors to extract and log search result titles.

Extending the Script

  1. Scrape URLs:

    • Modify the
      evaluate
      block to extract URLs:
      const results = await page.evaluate(() => {
       const links = [];
       const elements = document.querySelectorAll('a'); // Get all links
       elements.forEach(element => {
       if (element.href) links.push(element.href);
       });
       return links;
      });
      
  2. Save Data:

    • Save the extracted data to a file:
      const fs = require('fs');
      fs.writeFileSync('results.json', JSON.stringify(results, null, 2));
      
  3. Automate Login:

    • Use Puppeteer to log in to a website:
      await page.type('#username', 'your-username');
      await page.type('#password', 'your-password');
      await page.click('#login-button');
      
  4. Schedule Automation:

    • Use a task scheduler like
      cron
      (Linux/macOS) or Task Scheduler (Windows) to run the script periodically.
  5. Handle Complex Workflows:

    • Use Playwright if you need multi-browser support or advanced features:
      npm install playwright
      

Use Cases for RPA with JavaScript

  1. Web Scraping: Extract data from websites.
  2. Form Filling: Automate filling and submitting online forms.
  3. Testing: Automate end-to-end testing of web applications.
  4. Data Entry: Input data into web-based applications.
  5. Price Monitoring: Track prices on e-commerce websites.

 

caa November 27 2024 31 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 3
Members Online 0

Total Members: 11
Newest Member: Jhilam