Puppeteer and Nodejs: tasks automations

This manual describes how to automate a task that accesses the Access System web application, logs in, navigates to a configuration section and executes actions such as “Read” and “Save”, using puppeteer and nodejs, in addition to the Windows Task Scheduler. This is essential because due to network connection problems the panels are constantly disconnected and it is necessary to reactivate them manually, but with this script all this is automated.

What is Nodejs?

Node.js, often simply called Node, is a runtime environment that enables developers to run JavaScript outside of a web browser. Instead of being limited to client-side scripting, JavaScript can be executed on servers or local machines, making it possible to build backend applications, APIs, and various system tools. By using the same programming language on both the front end and back end, Node.js simplifies development workflows and allows for more efficient, scalable application design.

Node.js Installation and Configuration

Step 1:

Installing and Configuring the environment variable for Node.js on Windows.

puppeteer and nodejs

Why is this important?

When you install Node.js, the system needs to know where to find the node.exe executable. If it is not in the PATH environment variable, you will not be able to run commands like node or npm from the terminal. Steps to set the environment variable, this prevents interruptions of your access control Miami configuration

Install Node.js

When you install Node.js, the system needs to know where to find the node.exe executable. If it is not in the PATH environment variable, you will not be able to run commands like node or npm from the terminal. Steps to set the environment variable

Download the installer from nodejs.org

• Check if it is already configured
• Open a terminal (CMD or PowerShell) and write:

node -v

npm -v

If you see the version numbers, you are ready! If not, continue with the next steps.

• Manually add the path to the environment variables.
• Right click on “This computer” or “My Computer” and select “Properties”.
• Click on “Advanced system settings”.
• In the window that opens, click on “Environment variables…”

• In the “System Variables” section, find the variable named Path and click “Edit.”
• Click “New” and add the path where Node.js was installed, for example:C:\Program Files\nodejs\, Click on OK in all windows to save changes.

puppeteer and nodejs

Check again, open a new terminal and type:

node -v

npm -v

You should now see the installed versions.

Step 2:

Then, lets Install puppeteer and nodejs.

• Open a terminal (CMD o PowerShell) Run the following command in your project folder: npm install puppeteer.

puppeteer and nodejs

2. Creation of the automation script

• Create a folder called AutomatorRead.
• Inside it, create a file named leer.js.

Copy and paste the code shown in the next section:

3. Complete script code

• This is an example and the script should be modified according to your needs.

const puppeteer = require(‘puppeteer’);
(async () => {
let browser;
try {
console.log(” Iniciando navegador…”);
browser = await puppeteer.launch({
headless: ‘new’,
defaultViewport: { width: 1280, height: 800 }
});
const page = await browser.newPage();
console.log(” Abriendo página de login…”);
await page.goto(‘http://enlace directo a la aplicacion web’);
await page.waitForSelector(‘#Code’);
await page.waitForSelector(‘#Password’);
await page.type(‘#Code’, ‘admin’); // ← tu usuario
await page.type(‘#Password’, ‘colocar password’); // ← tu contraseña
await Promise.all([
page.click(‘#btnLogin’),
page.waitForNavigation({ waitUntil: ‘networkidle0’ })
]);
console.log(” Login exitoso.”);
await new Promise(resolve => setTimeout(resolve, 3000));
console.log(” Abriendo página principal…”);
await page.goto(‘ http://enlace directo a la aplicacion web’);
console.log(” Buscando menú ‘monitor configuration’…”);
await page.waitForSelector(“a[onclick*=’/Config/Config1′]”, { timeout: 10000 });
await page.evaluate(() => {
const menuItem = Array.from(document.querySelectorAll(“a”)).find(a =>
a.getAttribute(“onclick”)?.includes(“/Config/Config1”)
);
if (menuItem) menuItem.click();
});
console.log(” Menú clickeado.”);
await new Promise(resolve => setTimeout(resolve, 3000));
const frames = page.frames();
const monitorFrame = frames.find(f => f.url().includes(‘/Config/Config1’));
if (!monitorFrame) {
console.error(” No se encontró el iframe de monitor configuration.”);
return;
}
console.log(” Cargando contenido de monitor configuration…”);
await monitorFrame.waitForSelector(“span[data-lang=’read’]”, { timeout: 10000 });
await monitorFrame.evaluate(() => {
const readButton = document.querySelector(“span[data-lang=’read’]”);
if (readButton) readButton.click();
});
console.log(” Botón ‘Read’ clickeado.”);
await new Promise(resolve => setTimeout(resolve, 2000));
await monitorFrame.waitForSelector(“span[data-lang=’save’]”, { timeout: 10000 });
await monitorFrame.evaluate(() => {
const saveButton = document.querySelector(“span[data-lang=’save’]”);
if (saveButton) saveButton.click();
});
console.log(” Botón ‘Save’ clickeado.”);
await new Promise(resolve => setTimeout(resolve, 3000));
console.log(” Proceso completado.”);
} catch (error) {
console.error(” Error detectado:”, error.message);
} finally {
if (browser) {
console.log(” Cerrando navegador…”);
await browser.close();
}
}
})();

4. Open the terminal in the folder AutomatorRead

• Execute the script with the command: node leer.js

5. Task Scheduler Configuration

• Open the Task Scheduler from the Start menu.
• Click on Add Task.
• In the General tab:
o Name: AutomatizadorRead
or check “Execute whether the user is logged in or not”
▪ Check “Run with the highest privileges”
o Marca “Hidden”

puppeteer and nodejs

6. Schedule execution every 2 minutes

1. Go to the Triggers tab.
2. Click on New… Configure:
• Start the task: “On a schedule”.
• Repeat every: 2 minutes.
• During: “Indefinitely”.

puppeteer and nodejs

7. Automated Script Execution

By running this script every 2 minutes, this prevents the Access Control systems Miami panels from continually disconnecting while errors in the network are being resolved.

Share:

Facebook
Twitter
Pinterest
LinkedIn
On Key

Related Posts