-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
123 lines (98 loc) · 4.46 KB
/
test.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
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
import ddddocr
import base64
mainUrl = "https://course.fcu.edu.tw"
import configparser
import random
from selenium.webdriver.support import expected_conditions as EC
config = configparser.ConfigParser()
config.read('config.ini')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
#chrome_options.add_argument('--headless')這行註解拿掉即可使程式在背景執行
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.maximize_window()
# 帳號及密碼
username = 'd0948511'
password = ''
# 選課代號
classID = []
line=''
def login():
browser.get(mainUrl)
ocr = ddddocr.DdddOcr()
checkCode = 0
cookies = browser.get_cookies()
img_base64 = browser.execute_script("""
var ele = arguments[0];
var cnv = document.createElement('canvas');
cnv.width = ele.width; cnv.height = ele.height;
cnv.getContext('2d').drawImage(ele, 0, 0);
return cnv.toDataURL('image/jpeg').substring(22);
""", browser.find_element_by_xpath("//*[@id='ctl00_Login1_Image1']"))
with open("captcha_login.png", 'wb') as image:
image.write(base64.b64decode(img_base64))
with open('captcha_login.png', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
browser.find_element_by_xpath(
'//*[@id="ctl00_Login1_UserName"]').send_keys(config['data']['user'])
browser.find_element_by_xpath(
'//*[@id="ctl00_Login1_Password"]').send_keys(config['data']['pass'])
browser.find_element_by_xpath(
'//*[@id="ctl00_Login1_vcode"]').send_keys(res)
browser.find_element_by_xpath(
'//*[@id="ctl00_Login1_LoginButton"]').click()
print('登入成功')
def grab():
currentValue=0
print("如果想要1234、2256兩門課就輸入1234 2256後enter,課別之間用空白分開\n")
line=input('輸入想要的課程,用空白分開,enter結束讀取:')
classID=line.split()
print('以下是你選的課程代號')
for i in classID:
print(i)
while len(classID):
random.shuffle(classID)
try:
# WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="ctl00_MainContent_TabContainer1_tabSelected_Label3"]')))
browser.find_element_by_xpath('//*[@id="ctl00_MainContent_TabContainer1_tabSelected_Label3"]').click()
except:
print('連結找不到')
try:
#WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="ctl00_MainContent_TabContainer1_tabSelected_tbSubID"]')))
browser.find_element_by_xpath('//*[@id="ctl00_MainContent_TabContainer1_tabSelected_tbSubID"]').send_keys(classID[len(classID)-1])
except:
print('連結找不到')
try:
browser.find_element_by_xpath(
'//*[@id="ctl00_MainContent_TabContainer1_tabSelected_gvToAdd"]/tbody/tr[2]/td[8]/input').click()
sleep(0.7)
alert = browser.switch_to_alert()
alertInfo = alert.text
currentValue = int(alertInfo[10:13].strip())
openValue = int(alertInfo[14:18].strip())
alert.accept()
print('剩餘名額:', currentValue)
print('開放名額:', openValue)
except:
print('連結找不到')
# 選課
# WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="ctl00_MainContent_TabContainer1_tabSelected_gvToAdd"]/tbody/tr[2]/td[1]/input')))
browser.find_element_by_xpath('//*[@id="ctl00_MainContent_TabContainer1_tabSelected_gvToAdd"]/tbody/tr[2]/td[1]/input').click()
#classID.remove(classID[len(classID)-1])
sleep(2.1)
print(browser.find_element_by_xpath('//*[@id="ctl00_MainContent_TabContainer1_tabSelected_lblMsgBlock"]/span').text)
if browser.find_element_by_xpath('//*[@id="ctl00_MainContent_TabContainer1_tabSelected_lblMsgBlock"]/span').text.find('加選成功')!=-1:
print('成功')
if __name__ == "__main__":
login()
grab()
browser.quit()