diff --git a/utils/utils.py b/utils/utils.py index c3c4a01..fbd0016 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -11,6 +11,14 @@ from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select +# Constants for configuration and element IDs +EIP_URL = "https://eip.techmation.com.tw/MotorWeb/MotorFaceDefaultNew.aspx" +LANG_DROPDOWN_ID = "ddlLang" +LOGIN_ID_INPUT_ID = "txtLoginID" +LOGIN_PWD_INPUT_ID = "txtLoginPwd" +COMPANY_ID_DROPDOWN_ID = "ddlCompanyID" +LOGIN_BUTTON_ID = "btnLogin" + class LoginInfo(): def __init__(self, config_path: WindowsPath): @@ -37,8 +45,6 @@ class LoginInfo(): def keep_login_status(driver: webdriver.Chrome, login_info: LoginInfo): - EIP_url = "https://eip.techmation.com.tw/MotorWeb/MotorFaceDefaultNew.aspx" - # * Close all windows except the main window. while len(driver.window_handles) > 1: driver.switch_to.window(driver.window_handles[1]) @@ -47,29 +53,29 @@ def keep_login_status(driver: webdriver.Chrome, login_info: LoginInfo): driver.switch_to.window(driver.window_handles[0]) top_page = driver.current_window_handle - driver.get(EIP_url) + driver.get(EIP_URL) time.sleep(0.3) if driver.title == "CHI MOTOR WEB ERP 登入": # * Fill in all login information. - dropdown_element = driver.find_element(By.ID, "ddlLang") + dropdown_element = driver.find_element(By.ID, LANG_DROPDOWN_ID) select = Select(dropdown_element) select.select_by_value(login_info.lang) - input_text_element = driver.find_element(By.ID, "txtLoginID") + input_text_element = driver.find_element(By.ID, LOGIN_ID_INPUT_ID) input_text_element.clear() input_text_element.send_keys(login_info.login_ID) - input_text_element = driver.find_element(By.ID, "txtLoginPwd") + input_text_element = driver.find_element(By.ID, LOGIN_PWD_INPUT_ID) input_text_element.clear() input_text_element.send_keys(login_info.login_passwd) - dropdown_element = driver.find_element(By.ID, "ddlCompanyID") + dropdown_element = driver.find_element(By.ID, COMPANY_ID_DROPDOWN_ID) select = Select(dropdown_element) select.select_by_value(login_info.company_ID) # * Press the submit button. - submit_btn = driver.find_element(By.ID, "btnLogin") + submit_btn = driver.find_element(By.ID, LOGIN_BUTTON_ID) submit_btn.click() time.sleep(3)