import time from enum import IntEnum from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import ElementClickInterceptedException from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select class ActionType(IntEnum): 登入 = 0 請假 = 1 補卡 = 2 訂便當 = 3 @classmethod def has_value(cls, value): return value in cls._value2member_map_ class Action(): def __init__(self, driver: webdriver.Chrome): self.action_list = {ActionType.登入: self.__登入, ActionType.請假: self.__請假, ActionType.補卡: self.__補卡, ActionType.訂便當: self.__訂便當 } self.driver = driver def run(self, action_type) -> None: self.action_list[action_type]() def __登入(self): pass def __請假(self): """開啟請假單頁面""" # self.driver.find_element(By.ID, "ddlMenuTypes_DIV").click() # time.sleep(0.05) # dropdown_element = self.driver.find_element(By.ID, "CopyOfComboBox") # select = Select(dropdown_element) # select.select_by_value("1") # 人力資源 # time.sleep(0.05) # self.driver.find_element(By.ID, "ddlMenuItems1_DIV").click() # time.sleep(0.05) # dropdown_element = self.driver.find_element(By.ID, "CopyOfComboBox") # # * 基於UI本身的邏輯,因下拉式選單沒有更換值,所以無法收回選單 # # // select = Select(dropdown_element) # # // select.select_by_value("1_0") # 考勤管理作業 # # * 以下列方式替代 # dropdown_element.find_element( # By.XPATH, ".//option[@value='1_0']").click() # 考勤管理作業 # time.sleep(0.05) # menu = self.driver.find_element(By.ID, "BtnMenu") # 選單 # btn_root = self.driver.find_element( # By.XPATH, ".//ul[@id='Stage1']") # btn_list = btn_root.find_elements( # By.XPATH, ".//li[@class='dropdown 1_0']") # btn = btn_list[0] # 請假單 # ActionChains(self.driver).move_to_element( # menu).move_to_element(btn).click(btn).perform() self.driver.execute_script( u"javascript:MenuItemClick('/MotorWeb/CHIPage/FuncTagPage.aspx?PageUrl=%7e%2fCHIHumrs%2fwHumrsExcuse.aspx&Form_FuncTag=CHIHumrs.Excuse', false, false, false)") time.sleep(0.05) def __補卡(self): """開啟刷卡補卡單頁面""" # self.driver.find_element(By.ID, "ddlMenuTypes_DIV").click() # time.sleep(0.05) # dropdown_element = self.driver.find_element(By.ID, "CopyOfComboBox") # select = Select(dropdown_element) # select.select_by_value("1") # 人力資源 # time.sleep(0.05) # self.driver.find_element(By.ID, "ddlMenuItems1_DIV").click() # time.sleep(0.05) # dropdown_element = self.driver.find_element(By.ID, "CopyOfComboBox") # dropdown_element.find_element( # By.XPATH, ".//option[@value='1_0']").click() # 考勤管理作業 # time.sleep(0.05) # menu = self.driver.find_element(By.ID, "BtnMenu") # 選單 # btn_root = self.driver.find_element( # By.XPATH, ".//ul[@id='Stage1']") # btn_list = btn_root.find_elements( # By.XPATH, ".//li[@class='dropdown 1_0']") # btn = btn_list[2] # 刷卡補卡單 # ActionChains(self.driver).move_to_element( # menu).move_to_element(btn).click(btn).perform() self.driver.execute_script( u"javascript:MenuItemClick('/MotorWeb/CHIPage/FuncTagPage.aspx?PageUrl=%7e%2fCHIHumrs%2fwHumrsAppendClk.aspx&Form_FuncTag=CHIHumrs.AppendClk', false, false, false)") time.sleep(0.05) def __訂便當(self): """開啟所有未訂購的便當訂購頁面""" self.driver.switch_to.frame(self.driver.find_element(By.ID, "main")) all_orders = self.driver.find_elements( By.XPATH, ".//table[@id='WPMyOrder_tbMyOrder']/tbody/tr") open_page_num = 0 for order in all_orders: a_tag = order.find_elements( By.XPATH, ".//td[@valign='baseline']/a") assert len(a_tag) == 3 order_title, img, order_status = a_tag if order_status.text != "【已訂購】": order_title.click() open_page_num += 1 time.sleep(0.05) if open_page_num > 0: print(f"已開啟 {open_page_num} 個訂購頁面") else: print("沒有尚未訂購的團購訂單") self.driver.switch_to.default_content()