-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.js
102 lines (76 loc) · 2.59 KB
/
local.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
const express = require("express");
const puppeteer = require("puppeteer");
const PUPPETEER_OPTIONS = {
// headless: false,
};
const app = express();
app.get("/scrape", async (req, res) => {
var jsonData = {};
var username = req.query.name;
console.log("running user: ", username);
const browser = await puppeteer.launch(PUPPETEER_OPTIONS);
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 720 });
await page.goto("https://srmgroup.dhi-edu.com/srmgroup_srmeec/");
const pageTitle = await page.title();
page.on("requestfailed", (request) => {
console.error("Request failed:", request.url());
});
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");
await page.waitForSelector("#mainlevel3");
const attdSidebar = await page.$$("#mainlevel3");
await attdSidebar[1].click();
await page.waitForSelector("select");
const selectElements = await page.$$("select");
const selectElement = selectElements[0];
if (selectElement) {
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()),
);
var optNumber = 0;
for (const optionValue of options) {
if (optNumber > optionName.length) {
return;
}
await selectElement.select(optionValue);
await page.waitForSelector(".text-center.summary-card-item");
await delay(1500);
await getPercentage(optionName[optNumber], page, jsonData);
optNumber++;
}
} else {
console.error("Select element not found");
}
await delay(1000);
await browser.close();
console.log("usernames Used: ", username);
res.send(`Hello ${req.query.name} \nData: ${JSON.stringify(jsonData)} !`);
});
async function getPercentage(Kumaruu, page, jsonData) {
const percentage = await page.evaluate(() => {
const element = document.querySelector(".text-center.summary-card-item");
if (!element) {
return "No data available";
}
return element.innerText;
});
const optionName = Kumaruu.toString();
jsonData[optionName] = percentage;
}
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
app.listen(8080, () => {
console.log("Local server listening on port 8080");
});