Created
December 16, 2020 12:55
-
-
Save km4sh/74d820d2d8c33ce15989117c9d271147 to your computer and use it in GitHub Desktop.
Auto report complete using `selenium` with *headless Firefox*
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.firefox.options import Options | |
import time | |
from random import random | |
from datetime import datetime | |
def open_safari(): | |
driver = webdriver.safari() | |
return driver | |
def open_firefox(): | |
options = Options() | |
options.headless = True | |
driver = webdriver.Firefox(options=options) | |
return driver | |
def login(driver, id_code, pass_code): | |
url = "https://selfreport.shu.edu.cn" | |
driver.get(url) | |
username = wait_until_find(driver, "username", "id") | |
username.send_keys(id_code) | |
time.sleep(1) | |
password = wait_until_find(driver, "password", "id") | |
password.send_keys(pass_code) | |
driver.execute_script("arguments[0].click()", driver.find_element_by_id("submit")) | |
return driver.current_url | |
def submit_today(driver, time): | |
today = wait_until_find(driver, "lnkReport", "id") | |
driver.execute_script("arguments[0].click()", today) | |
if time == "morning": | |
morning = wait_until_find(driver, "p1_Button1", "id") | |
driver.execute_script("arguments[0].click()", morning) | |
elif time == "evening": | |
evening = wait_until_find(driver, "p1_Button2", "id") | |
driver.execute_script("arguments[0].click()", evening) | |
submit_form(driver) | |
def submit_form(driver): | |
driver.execute_script( | |
"arguments[0].click()", | |
wait_until_find(driver, "p1_ChengNuo-inputEl-icon", "id"), | |
) | |
driver.execute_script("arguments[0].click()", driver.find_element_by_id("fineui_0")) | |
temp = driver.find_element_by_id("p1_TiWen-inputEl") | |
temp.clear() | |
temp.send_keys(u"36") | |
# Baoshan campus | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_6-inputEl-icon") | |
) | |
# No stay in mid-high risk zone | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_11-inputEl-icon") | |
) | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_13-inputEl-icon") | |
) | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_15-inputEl-icon") | |
) | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_21-inputEl-icon") | |
) | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_23-inputEl-icon") | |
) | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_26-inputEl-icon") | |
) | |
driver.execute_script( | |
"arguments[0].click()", driver.find_element_by_id("fineui_27-inputEl-icon") | |
) | |
temp = driver.find_element_by_id("p1_XiangXDZ-inputEl") | |
temp.clear() | |
temp.send_keys("校内") | |
submit = driver.find_element_by_id("p1_ctl00_btnSubmit") | |
driver.execute_script("arguments[0].click()", submit) | |
sure = wait_until_find(driver, "确定", "button") | |
driver.execute_script("arguments[0].click()", sure) | |
sure = wait_until_find(driver, "确定", "button") | |
time.sleep(3 * random() + 1) | |
driver.execute_script("arguments[0].click()", sure) | |
return "OK!" | |
def wait_until_find(driver, element_id, method): | |
if method == "id": | |
element = WebDriverWait(driver, 5).until( | |
EC.presence_of_element_located((By.ID, element_id)) | |
) | |
return element | |
elif method == "text": | |
element = WebDriverWait(driver, 5).until( | |
EC.presence_of_all_elements_located((By.PARTIAL_LINK_TEXT, element_id)) | |
) | |
return element | |
elif method == "button": | |
element = WebDriverWait(driver, 5).until( | |
EC.presence_of_element_located((By.LINK_TEXT, element_id)) | |
) | |
return element | |
def clear_history(driver): | |
history = wait_until_find(driver, "lnkReportHistory", "id") | |
driver.execute_script("arguments[0].click()", history) | |
try: | |
undone = wait_until_find(driver, "未填报", "text") | |
while len(undone) > 1: | |
driver.execute_script("arguments[0].click()", undone[1]) | |
submit_form(driver) | |
history = wait_until_find(driver, "lnkReportHistory", "id") | |
driver.execute_script("arguments[0].click()", history) | |
undone = wait_until_find(driver, "未填报", "text") | |
time.sleep(5 * random() + 2) | |
except: | |
print( | |
datetime.now(), | |
" --> Found no report needs to be cleared while checking history.", | |
) | |
if __name__ == "__main__": | |
import getpass | |
from datetime import datetime | |
from datetime import time as set_time | |
now = datetime.now | |
id = input("Username:") | |
pwd = getpass.getpass("Password:") | |
def epoch(): | |
browser = open_firefox() | |
login(browser, id, pwd) | |
print(datetime.now(), " --> Login as", id, "successful. ") | |
clear_history(browser) | |
print(datetime.now(), " --> History cleared. (Including morning report.)") | |
browser.get("https://selfreport.shu.edu.cn") | |
try: | |
submit_today(browser, "evening") | |
print(datetime.now(), " --> Evening report cleared.") | |
except: | |
print(datetime.now(), " --> Too early for the evening report.") | |
browser.quit() | |
print(datetime.now(), " --> Browser quited.") | |
# return morningCleared, eveningCleared | |
epoch() | |
morningPeriod = [set_time(6, 30), set_time(7, 00)] | |
eveningPeroid = [set_time(7, 30), set_time(8, 00)] | |
resetPeroid = [set_time(3, 30), set_time(4, 00)] | |
morningCleared = True | |
eveningCleared = False | |
while True: | |
current = now().time() | |
if current > morningPeriod[0] and current < morningPeriod[1]: | |
if not morningCleared: | |
epoch() | |
morningCleared = True | |
if current > eveningPeroid[0] and current < eveningPeroid[1]: | |
if not eveningCleared: | |
epoch() | |
eveningCleared = True | |
if current > resetPeroid[0] and current < resetPeroid[1]: | |
morningCleared = False | |
eveningCleared = False | |
time.sleep(1800) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment