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.
This commit is contained in:
kinoshitakenta 2025-05-28 15:32:23 +08:00
parent cfd1e5e61a
commit e32063e2ce
Signed by: kinoshitakenta
GPG Key ID: A811E8CA36EF425E
1 changed files with 14 additions and 8 deletions

View File

@ -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)