From e32063e2ce37a48307e2127e9eacb5cc922a4e6c Mon Sep 17 00:00:00 2001 From: kinoshitakenta Date: Wed, 28 May 2025 15:32:23 +0800 Subject: [PATCH] refactor: uses constants for EIP login elements Refactors the code to use constants for EIP URL and HTML element IDs, improving code maintainability and readability. This change replaces hardcoded strings with named constants, making it easier to update element identifiers if they change on the EIP website. --- utils/utils.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) 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)