-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomate.py
136 lines (107 loc) · 4.71 KB
/
automate.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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import datetime
import platform
import time
def automateLogin(STUDENT_ID: str, PASSWORD: str, GYM_ID: str, ITEM_ID: str):
# Linux
if platform.system() == "Linux":
chrome_driver_path = "./chromedriver_linux64"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("--headless=new")
chrome_options.binary_location = chrome_driver_path
chrome_options.add_argument("log-level=2")
chrome_options.add_experimental_option(
"excludeSwitches", ["enable-logging"]
)
driver = webdriver.Chrome(options=chrome_options)
# Windows
elif platform.system() == "Windows":
chrome_driver_path = "./chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("--headless=new")
# chrome_options.add_argument("--disable-dev-shm-usage") # //!!!should be enabled for Jenkins
# chrome_options.add_argument("--window-size=1920x1080") # //!!!should be enabled for Jenkins
chrome_options.add_argument("log-level=2")
chrome_options.add_experimental_option(
"excludeSwitches", ["enable-logging"]
)
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
# TODO: "Darwin" for macOS arm64 and x64
driver.get("https://50.tsinghua.edu.cn/dl.jsp")
username_field = driver.find_element(By.NAME, "un")
password_field = driver.find_element(By.NAME, "pw")
username_field.send_keys(STUDENT_ID)
password_field.send_keys(PASSWORD)
login_button = driver.find_element(
By.XPATH, "//input[@src='images/login.jpg']"
)
login_button.click()
courtBookLink = driver.find_element(
By.XPATH,
"//a[@href='/gymsite/gymBookViewAction.do?ms=viewBook_site&fromPage=index']",
)
courtBookLink.click()
bookCourtLink = driver.find_element(
By.XPATH, "//a[@href='javascript:viewBook()']"
)
bookCourtLink.click()
confirmButton = driver.find_element(
By.XPATH, '//button[contains(@onclick,"saveAttentionState()")]'
)
driver.execute_script("arguments[0].click();", confirmButton)
gymButton = driver.find_element(
By.XPATH,
f"//a[@href='/gymbook/gymBookAction.do?ms=viewGymBook&gymnasium_id={GYM_ID}&item_id=&time_date=&userType=']",
)
gymButton.click()
curDate = str(datetime.date.today())
curGymButton = driver.find_element(
By.XPATH,
f"//a[@href='/gymbook/gymBookAction.do?ms=viewGymBook&gymnasium_id={GYM_ID}&item_id={ITEM_ID}&time_date={curDate}&userType=']",
)
curGymButton.click()
cookies = driver.get_cookies()
for cookie in cookies:
if cookie["name"] == "serverid":
serverid = cookie["value"]
elif cookie["name"] == "JSESSIONID":
jsessionid = cookie["value"]
username = driver.find_element(
By.XPATH, "//div[@class='member-box round-all']//strong"
).text
return serverid, jsessionid, username, driver
def automatePay(driver, session, serverid, jsessionid, paymentmethod):
orderButton = driver.find_element(
By.XPATH, "//a[@href='/pay/payAction.do?ms=getOrdersForNopay']"
)
orderButton.click()
if paymentmethod == 1:
payNowButton = driver.find_element(
By.XPATH, '//a[contains(@onclick, "payNow")]'
)
driver.execute_script("arguments[0].click();", payNowButton)
noReceiptOption = driver.find_element(By.XPATH, '//input[@id="xm4"]')
driver.execute_script("arguments[0].click();", noReceiptOption)
submitButton = driver.find_element(
By.XPATH, '//a[contains(@onclick,"savePayFrmWithPay()")]'
)
driver.execute_script("arguments[0].click();", submitButton)
proceedButton = driver.find_element(
By.XPATH, '//button[@id="proceed-button"]'
)
proceedButton.click()
time.sleep(2)
proceedButton.click()
alipayButton = driver.find_element(
By.XPATH, "//img[@src='/zjjsfw/img/zfb.png']"
)
alipayButton.click()
# TODO: AliPay Automatic Payment
# driver.get("http://fa-online.tsinghua.edu.cn/zjjsfw/zjjs/api.do")
# loginAliPayButton = < 登录账户付款
# loginAliPayButton.click()