Compare commits

...

4 Commits

Author SHA1 Message Date
kinoshitakenta 3c23ee2ce9
feat: improves screen clearing functionality
Updates the screen clearing function to provide a more robust cross-platform solution. It now attempts to reset text attributes on Windows and uses `shutil.which` to locate the `clear` command on Unix-like systems, falling back to ANSI escape codes if necessary.

This change improves the screen-clearing function, ensuring its effectiveness regardless of the operating system or available commands.
2025-05-28 15:57:25 +08:00
kinoshitakenta 0671d49497
feat: validates config path before loading login info
Adds a check to ensure the provided configuration file path exists and is a valid file before attempting to load login information.
2025-05-28 15:45:01 +08:00
kinoshitakenta e32063e2ce
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.
2025-05-28 15:33:05 +08:00
kinoshitakenta cfd1e5e61a
feat: validates required fields in login configuration
Adds validation to ensure that all required fields ("lang", "login_ID", "login_passwd", "company_ID") are present in the login_info section of the configuration file.

Raises a ValueError if any required field is missing, improving error handling and preventing potential issues
due to incomplete configuration.
2025-05-28 15:33:03 +08:00
2 changed files with 45 additions and 11 deletions

29
main.py
View File

@ -25,18 +25,41 @@ def get_usage() -> list[tuple]:
return cmd_list
def display_usage():
def clear_screen():
if os.name == 'nt':
# Windows system
try:
# try to reset the text attributes
kernel32 = ctypes.windll.kernel32
handle = kernel32.GetStdHandle(-11) # STD_OUTPUT_HANDLE
kernel32.SetConsoleTextAttribute(handle, 7)
except Exception:
pass
os.system('cls')
else:
os.system('clear')
# Unix-like system
clear_cmd = shutil.which('clear')
if clear_cmd:
os.system(clear_cmd)
else:
# backup plan: Use ANSI escape codes (to clear and reset the screen).
print('\033c', end='')
def display_usage():
clear_screen()
for cmd, msg in get_usage():
print(f"{cmd}: {msg}")
def main(opt):
login_info = LoginInfo(Path(opt.config_path))
config_path = Path(opt.config_path)
if not config_path.exists() or not config_path.is_file():
print(f"Error: Config path '{config_path}' does not exist or is not a file.")
return
login_info = LoginInfo(config_path)
driver = get_driver()
keep_login_status(driver, login_info)

View File

@ -11,12 +11,25 @@ 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):
with open(config_path, mode="rb") as f:
config = tomllib.load(f)
required_fields = ["lang", "login_ID", "login_passwd", "company_ID"]
for field in required_fields:
if field not in config.get("login_info", {}):
raise ValueError(f"Missing required field: {field} in login_info section")
self.lang = config["login_info"]["lang"]
self.login_ID = config["login_info"]["login_ID"]
self.login_passwd = config["login_info"]["login_passwd"]
@ -32,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])
@ -42,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)