diff --git a/app.py b/app.py index cdacc57..d8a7719 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ """This python file will do the AutoClass job.""" +import time from selenium import webdriver from selenium.common import TimeoutException @@ -25,6 +26,10 @@ def driver_screenshot(locator, path): WebDriverWait(driver, 10).until(ec.presence_of_element_located(locator)).screenshot(path) +def driver_get_text(locator): + return WebDriverWait(driver, 10).until(ec.presence_of_element_located(locator)).text + + def login(): driver.get('https://course.fcu.edu.tw/') driver_send_keys((By.ID, "ctl00_Login1_UserName"), config.get("username")) @@ -44,13 +49,23 @@ def auto_class(class_ids): driver_click((By.ID, "ctl00_MainContent_TabContainer1_tabSelected_Label3")) for class_id in class_ids: driver_send_keys((By.ID, "ctl00_MainContent_TabContainer1_tabSelected_tbSubID"), class_id) - try: - WebDriverWait(driver, 0.5).until(ec.presence_of_element_located((By.ID, - "//*[@id='ctl00_MainContent_TabContainer1_tabSelected_gvToDel']/tbody/tr[2]/td[1]/input"))) - class_ids.remove(class_id) - except TimeoutException: + + # query remain position + driver_click((By.XPATH, + "//*[@id='ctl00_MainContent_TabContainer1_tabSelected_gvToAdd']/tbody/tr[2]/td[8]/input")) + time.sleep(0.5) + alert = driver.switch_to.alert + remain_pos = alert.text.strip('剩餘名額/開放名額:').split("/")[0] + alert.accept() + + if not remain_pos == '0': driver_click((By.XPATH, "//*[@id='ctl00_MainContent_TabContainer1_tabSelected_gvToAdd']/tbody/tr[2]/td[1]/input")) + if driver_get_text((By.XPATH, "//*[@id='ctl00_MainContent_TabContainer1_tabSelected_lblMsgBlock']/span")) == "加選成功": + print("成功加選課程:" + class_id) + class_ids.remove(class_id) + else: + pass auto_class(class_ids)