-
Notifications
You must be signed in to change notification settings - Fork 0
/
puppetScript.js
131 lines (99 loc) · 3.61 KB
/
puppetScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const puppeteer = require("puppeteer");
require("dotenv").config();
const puppetScript = async (username) => {
// Launch the browser and open a new blank page
var jsonData = {};
// delete this ASAP
const browser = await puppeteer.launch({
args: ["--no-sandbox"],
// executablePath:
// process.env.NODE_ENV === "production"
// ? process.env.PUPPETEER_EXECUTABLE_PATH
// : puppeteer.executablePath(),
headless: false,
});
const page = await browser.newPage();
// Navigate the page to a URL
await page.goto("https://srmgroup.dhi-edu.com/srmgroup_srmeec/");
// const pageTitle = await page.title();
//
// jsonData["title"] = pageTitle;
//
// return jsonData;
// Set screen size
await page.setViewport({ width: 1080, height: 1024 });
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
await page.waitForSelector("#username");
await page.type("#username", username);
await page.waitForSelector("#password");
await page.type("#password", "dhi001");
await page.waitForSelector("#kc-login");
await page.hover("#kc-login");
await page.click("#kc-login");
// const element = await page.waitForSelector("div > ");
// console.log(element);
// const kumar = await page.waitForSelector(
// "sidebar-menu.admin.d-md-block.d-none",
// );
//
await page.waitForSelector("#mainlevel3");
// await delay(8000);
const attdSidebar = await page.$$("#mainlevel3");
await attdSidebar[1].click();
// await delay(8000);
await page.waitForSelector("select");
// Select the multi-select element
const selectElements = await page.$$("select"); // Replace 'select' with the appropriate selector
const selectElement = selectElements[0];
if (selectElement) {
// Get all options within the select element
const options = await selectElement.$$eval("option", (options) =>
options.map((option) => option.value),
);
const optionName = await selectElement.$$eval("option", (options) =>
options.map((option) => option.textContent.trim()),
);
// Iterate over each option and select it
var optNumber = 0;
for (const optionValue of options) {
if (optNumber > optionName.length) {
return;
}
await selectElement.select(optionValue); // Select the option
// Show the optionName
await page.waitForSelector(".text-center.summary-card-item");
await delay(1500);
getPercentage(optionName[optNumber]);
optNumber++;
}
} else {
console.error("Select element not found");
}
// NOTE:
// Need to do better way of getting the attendance percentages.
// Now we can actually login and go to the attendance page and get the attendance details via the selector until that everything works fine.
// We only need to improve the way of getting the attendance percentages. need to do only that. Thanks.
async function getPercentage(Kumaruu) {
// Use evaluate function to execute JavaScript code on the page
const percentage = await page.evaluate(() => {
// Select the element and return its innerText
const element = document.querySelector(".text-center.summary-card-item");
if (!element) {
return "No data available";
}
return element.innerText; // Return the innerText of the element if found
});
const optionName = Kumaruu.toString();
jsonData[optionName] = percentage;
}
await delay(1000);
await browser.close();
// the return of jsonData is not working.
console.log("usernames Used: ", username);
return jsonData;
};
module.exports = puppetScript;