Skip to content

Commit 1613d79

Browse files
committed
Update deprecated code
1 parent b8a25e8 commit 1613d79

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

daily_reporter.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from selenium import webdriver
22
from selenium.common import exceptions
33
from selenium.webdriver.chrome import options
4+
from selenium.webdriver.common.by import By
5+
from selenium.webdriver.chrome.service import Service
46
from random import randint
57
from random import normalvariate
68
import requests
@@ -33,8 +35,9 @@ def report(user_index, try_times):
3335

3436
print('[Info] Launching Browser')
3537
if (config.driver_path != 'auto'):
38+
service = Service(config.driver_path)
3639
browser = webdriver.Chrome(
37-
config.driver_path, options=chrome_options)
40+
service=service, options=chrome_options)
3841
else:
3942
browser = webdriver.Chrome(options=chrome_options)
4043

@@ -45,19 +48,19 @@ def report(user_index, try_times):
4548
browser.get('https://id.sspu.edu.cn/cas/login')
4649
time.sleep(1)
4750

48-
username_box = browser.find_element_by_id('username')
51+
username_box = browser.find_element(By.ID, 'username')
4952
username_box.send_keys(config.users[user_index]['username'])
50-
password_box = browser.find_element_by_id('password')
53+
password_box = browser.find_element(By.ID, 'password')
5154
password_box.send_keys(config.users[user_index]['password'])
5255
time.sleep(0.5)
5356

54-
login_button = browser.find_element_by_class_name('submit_button')
57+
login_button = browser.find_element(By.CLASS_NAME, 'submit_button')
5558
login_button.click()
5659
time.sleep(1)
5760

5861
# Detect login status
5962
try:
60-
browser.find_element_by_class_name('success')
63+
browser.find_element(By.CLASS_NAME, 'success')
6164
except exceptions.NoSuchElementException:
6265
print('[Error] Login failed')
6366
if config.users[user_index]['use_wechat']:
@@ -96,36 +99,36 @@ def report(user_index, try_times):
9699
print('[Info] Auto generated temperature:', temperature)
97100

98101
# Fill temperature
99-
temperature_box = browser.find_element_by_id('p1_TiWen-inputEl')
102+
temperature_box = browser.find_element(By.ID, 'p1_TiWen-inputEl')
100103
temperature_box.clear()
101104
temperature_box.send_keys(str(temperature))
102105
time.sleep(0.5)
103106

104107
# Health Condition
105-
condition_good = browser.find_element_by_id('p1_DangQSTZK') \
106-
.find_element_by_xpath("//*[contains(text(), 'Good')]")
108+
condition_good = browser.find_element(By.ID, 'p1_DangQSTZK') \
109+
.find_element(By.XPATH, "//*[contains(text(), 'Good')]")
107110
condition_good.click()
108111
time.sleep(0.5)
109112

110113
# In Shanghai
111114
if config.users[user_index]['in_shanghai']:
112-
in_shanghai = browser.find_element_by_id('p1_Shanghai') \
113-
.find_element_by_class_name('f-field-body-cell') \
114-
.find_element_by_class_name('f-field-checkbox-switch')
115+
in_shanghai = browser.find_element(By.ID, 'p1_Shanghai') \
116+
.find_element(By.CLASS_NAME, 'f-field-body-cell') \
117+
.find_element(By.CLASS_NAME, 'f-field-checkbox-switch')
115118
in_shanghai.click()
116119
time.sleep(0.5)
117120

118121
# Submit
119-
submit_button = browser.find_element_by_id('p1_ctl00') \
120-
.find_element_by_id('p1_ctl00_btnSubmit')
122+
submit_button = browser.find_element(By.ID, 'p1_ctl00') \
123+
.find_element(By.ID, 'p1_ctl00_btnSubmit')
121124
submit_button.click()
122125
time.sleep(1)
123126

124127
# Waiting for submit result
125128
for i in range(int(config.timeout / 3)):
126129
try:
127-
browser.find_element_by_class_name('f-window') \
128-
.find_elements_by_xpath(
130+
browser.find_element(By.CLASS_NAME, 'f-window') \
131+
.find_elements(By.XPATH,
129132
"//*[contains(text(), 'Submit successfully')]")
130133
except exceptions.NoSuchElementException:
131134
time.sleep(3)
@@ -135,8 +138,8 @@ def report(user_index, try_times):
135138
break
136139

137140
try:
138-
browser.find_element_by_class_name('f-window') \
139-
.find_elements_by_xpath(
141+
browser.find_element(By.CLASS_NAME, 'f-window') \
142+
.find_elements(By.XPATH,
140143
"//*[contains(text(), 'Submit successfully')]")
141144
except exceptions.NoSuchElementException:
142145
print('[Error] Submit timeout')
@@ -150,8 +153,8 @@ def report(user_index, try_times):
150153
print('[Info] Reported successfully')
151154

152155
# Click done
153-
done_button = browser.find_element_by_class_name('f-window') \
154-
.find_element_by_class_name('f-btn')
156+
done_button = browser.find_element(By.CLASS_NAME, 'f-window') \
157+
.find_element(By.CLASS_NAME, 'f-btn')
155158
done_button.click()
156159
time.sleep(1)
157160

@@ -160,9 +163,9 @@ def report(user_index, try_times):
160163
time.sleep(1)
161164

162165
# Get rank data text
163-
txt = browser.find_element_by_id('Panel1_DataList1') \
164-
.find_element_by_class_name('f-datalist-list') \
165-
.find_elements_by_class_name('f-datalist-item-inner')[0].text
166+
txt = browser.find_element(By.ID, 'Panel1_DataList1') \
167+
.find_element(By.CLASS_NAME, 'f-datalist-list') \
168+
.find_elements(By.CLASS_NAME, 'f-datalist-item-inner')[0].text
166169

167170
# Check whether report is successful
168171
if txt.find(strings.msg['success_msg']) == -1:

0 commit comments

Comments
 (0)