-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.py
218 lines (197 loc) · 7.29 KB
/
Driver.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from Login import Login
from selenium import webdriver
import time
import os
class Driver:
def __init__(self):
self.Login = Login()
if os.name == 'posix':
self.phantom = './phantomjs'
self.chrome = './chromedriver'
if os.name == 'nt':
self.phantom = './phantomjs.exe'
self.chrome = './chromedriver.exe'
self.timer = 0
self.login_count = 2
self.homework_table = []
self.driver_list = []
def create_driver(self, name):
if name == 'chrome':
driver = webdriver.Chrome(self.chrome)
self.driver_list.append(driver)
return driver
if name == 'phantom':
driver = webdriver.PhantomJS(self.phantom)
self.driver_list.append(driver)
return driver
def delete_driver(self, driver):
try:
driver.quit()
self.driver_list.remove(driver)
except Exception:
return False
return True
def terminate_all_drivers(self):
while len(self.driver_list) > 0:
self.driver_list.pop().quit()
def login_homepage(self, driver):
try:
driver.get("http://e-learn.cnu.ac.kr/")
j = 0
while True:
try:
driver.find_element_by_xpath('// *[ @ id = "pop_login"]').click()
break
except Exception:
j += 1
if j < 200:
time.sleep(0.1)
else:
return False
driver.find_element_by_xpath('//*[@id="id"]').send_keys(self.Login.login_data[0])
driver.find_element_by_xpath('//*[@id="pass"]').send_keys(self.Login.login_data[1] + '\n')
time.sleep(self.login_count)
driver.get('http://e-learn.cnu.ac.kr/lms/myLecture/doListView.dunet')
j = 0
while True:
try:
subject_number = len(
str(driver.find_element_by_xpath('// *[ @ id = "rows1"] / table').text).split('\n'))
break
except Exception:
j += 1
if j < 200:
time.sleep(0.1)
else:
return False
except Exception:
return False
return True
def select_subject_name(self, driver, i):
j = 0
while True:
try:
self.homework_table[i].append(str(driver.find_element_by_xpath('//*[@id="rows1"]/table/tbody/tr[' + str(i+1)
+ ']/td[4]/span[1]/a').text).split()[0])
break
except Exception:
j += 1
if j < 200:
time.sleep(0.1)
else:
return False
driver.find_element_by_xpath('// *[ @ id = "rows1"] / table / tbody / tr[' + str(i+1)
+ '] / td[4] / span[1] / a').click()
return True
@staticmethod
def find_submit_btn(driver):
j = 0
while True:
try:
temp = str(driver.find_element_by_xpath('//*[@id="leftSnb"]').text).split('\n')
index = 0
while True:
if temp[index] == "과제제출":
index += 1
break
index += 1
driver.find_element_by_xpath('//*[@id="leftSnb"]/li[' + str(index) + ']/a').click()
break
except Exception:
j += 1
if j < 200:
time.sleep(0.1)
else:
return False
return True
@staticmethod
def check_homework_name(driver, name):
i = 1
j = 0
while True:
try:
while True:
if name == driver.find_element_by_xpath(
'// *[ @ id = "con"] / table[2] / tbody / tr[' + str(
i) + '] / td[1] / strong / a').text:
driver.find_element_by_xpath(
'// *[ @ id = "con"] / table[2] / tbody / tr[' + str(i) + '] / td[1] / strong / a').click()
break
i += 1
break
except Exception:
j += 1
if j < 200:
time.sleep(0.1)
else:
return False
return True
def save_homework_table(self, driver, i):
j = 0
while True:
try:
self.homework_table[i - 1].append(
str(driver.find_element_by_xpath('// *[ @ id = "con"] / table[2] ').text).split('\n'))
break
except Exception:
j += 1
if j < 200:
time.sleep(0.1)
else:
return False
return True
def read_homework_table(self, driver):
try:
subject_number = len(str(driver.find_element_by_xpath('// *[ @ id = "rows1"] / table').text).split('\n'))
subject_number = int(subject_number / 2)
self.homework_table.clear()
i = 0
while i < subject_number:
self.homework_table.append([])
i += 1
i = 1
while i < subject_number:
driver.execute_script("window.open()")
i += 1
i = 1
while i < subject_number:
driver.switch_to_window(driver.window_handles[i])
driver.get('http://e-learn.cnu.ac.kr/lms/myLecture/doListView.dunet')
i += 1
i = 0
while i < subject_number:
driver.switch_to_window(driver.window_handles[i])
if not self.select_subject_name(driver, i):
return False
i += 1
if not self.find_submit_btn(driver):
return False
if not self.save_homework_table(driver, i):
return False
except Exception:
return False
return True
@staticmethod
def wait_terminate(driver):
while len(driver.get_window_size()) == 2:
time.sleep(5)
# 제출하기 기능
def submit_homework(self, driver, name_list):
subject_name = name_list[0]
homework_name = name_list[1]
i = 1
try:
while True:
if subject_name == str(driver.find_element_by_xpath(
'//*[@id="rows1"]/table/tbody/tr[' + str(i) + ']/td[4]/span[1]/a').text).split()[0]:
driver.find_element_by_xpath('// *[ @ id = "rows1"] / table / tbody / tr[' + str(i)
+ '] / td[4] / span[1] / a').click()
if not self.find_submit_btn(driver):
return False
break
i += 1
except Exception:
return False
if not self.check_homework_name(driver, homework_name):
return False
return True