-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
171 lines (144 loc) · 6.42 KB
/
main.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
def printX(color="white", text=""):
code = "\033[1;37;40m"
if color == "red":
code = "\033[1;31;40m"
elif color == "green":
code = "\033[1;32;40m"
elif color == "yellow":
code = "\033[1;33;40m"
elif color == "blue":
code = "\033[1;34;40m"
else:
code = "\033[1;37;40m"
print(f"{code} {text}")
class Main(webdriver.Edge):
def __init__(self):
super(Main, self).__init__(options=options)
self.implicitly_wait(20)
self.maximize_window()
def start(self, url):
printX("yellow", "Step 1/7: Opening Website")
try:
self.get(url)
except:
printX("!! Failed to open the URL in the browser", "red")
self.quit()
printX("green", ">> Opened Website Successfully")
def login(self, username, password):
printX("yellow", "Step 2/7: Trying to Log-In")
try:
usernameField = self.find_element(By.NAME, "j_username")
passwordField = self.find_element(By.NAME, "j_password")
submitBtn = self.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
usernameField.send_keys(username)
passwordField.send_keys(password)
submitBtn.click()
except:
printX("red", "!! Could not find the required elements on the web page")
self.quit()
printX("green", ">> Successfully Logged in!")
def openTestsPage(self):
printX("yellow", "Step 3/7: Opening Online tests page")
try:
acedemicFnsBtn = self.find_element(By.CSS_SELECTOR, 'a[pid="20009"]')
acedemicFnsBtn.click()
onlineAssessmentBtn = self.find_element(
By.CSS_SELECTOR, 'a[href="stu_studentTest.htm"]'
)
wait = WebDriverWait(self, timeout=5)
wait.until(lambda pause: onlineAssessmentBtn.is_displayed())
onlineAssessmentBtn.click()
except:
printX("red", "!! Failed to Open the Online tests page")
self.quit()
printX("green", ">> Done!")
def getTests(self):
printX("yellow", "Step 4/7: Getting all the Tests Info")
try:
container = self.find_element(By.ID, "availableTestDiv")
self.testsData = container.find_elements(By.TAG_NAME, "tr")
except:
printX("red", "!! Failed to gather data of all the tests")
self.quit()
printX("green", ">> Got the Data of all the test")
def getSpecificTest(self, testNumber):
printX("yellow", "Step 5/7: Get the specified test")
try:
self.targetTest = self.testsData[testNumber]
self.targetTestCols = self.targetTest.find_elements(By.TAG_NAME, "td")
except:
printX("red", "!! Could not find the specified test")
self.quit()
printX("green", ">> Got the tests")
printX("blue", f">> :: Test Name: {self.targetTestCols[0].text}")
printX("blue", f">> :: Subject: {self.targetTestCols[1].text}")
def startTest(self, passwordConfig=[False]):
printX("yellow", "Step 6/7: Starting the test")
try:
startBtn = self.targetTestCols[-1].find_element(By.TAG_NAME, "button")
startBtn.click()
if passwordConfig[0]:
passwordField = self.find_element(By.ID, "txtKeyVerification")
passwordField.send_keys(passwordConfig[1])
verifyBtn = self.find_element(By.ID, "btnSubmitKeyVerification")
verifyBtn.click()
time.sleep(1)
bigStartBtn = self.find_element(By.ID, "idStartTestImg")
# wait = WebDriverWait(self, timeout=5)
# wait.until(lambda pause: bigStartBtn.is_displayed())
bigStartBtn.click()
except:
printX("red", "!! Failed to start the test")
self.quit()
printX("green", ">> Started the test")
def collectQuestionsAndOptions(self):
printX("yellow", "Step 7/7: Now collecting all the questions")
try:
quesNumTable = self.find_element(By.ID, "questionImg")
questionsChips = quesNumTable.find_elements(By.TAG_NAME, "span")
except:
printX("red", "!! Could not find the required elements on the web page")
self.quit()
# Create file or clear it if pre-existing
f = open("result.txt", "w")
f.close()
for chip in questionsChips:
chip.click()
try:
parentContainer = self.find_element(By.ID, "questionDiv")
questionContainer = self.find_element(By.ID, "questionTd")
questionNumber = questionContainer.find_element(By.TAG_NAME, "span")
question = questionContainer.find_element(By.TAG_NAME, "b")
optionBoxes = parentContainer.find_elements(
By.CLASS_NAME, "opDivSelectionBox"
)
except:
printX("red", "!! Could not find the required elements on the web page")
self.quit()
try:
with open("result.txt", "a", encoding="utf-8") as file:
file.write(
"\n- - - x - - - x - - - x - - - x - - - x - - - x - - -\n\n"
)
file.write(f"{questionNumber.text}\n")
file.write(f"{question.text}\n\n")
file.write("Options are:\n\n")
for optionBox in optionBoxes:
option = optionBox.find_element(By.TAG_NAME, "label")
file.write(f"{option.text}\n")
except:
printX("red", "!! Unknown exeception occured during writing the file.")
self.quit()
# Give some times to chips to be clickable
time.sleep(1.4)
printX("green", ">> Collected all the questions")
printX("white", "Script finished!")
# self.quit()
def holdBrowser(self):
enter = input("Press enter to close the browser: ")