Compare commits

..

No commits in common. "3c23ee2ce9bb83f2f5dcbe3b922966000424eb44" and "ef204f1aae0f15c7429971d2e23ef99de8794379" have entirely different histories.

2 changed files with 11 additions and 45 deletions

29
main.py
View File

@ -25,41 +25,18 @@ def get_usage() -> list[tuple]:
return cmd_list return cmd_list
def clear_screen(): def display_usage():
if os.name == 'nt': 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') os.system('cls')
else: else:
# Unix-like system os.system('clear')
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(): for cmd, msg in get_usage():
print(f"{cmd}: {msg}") print(f"{cmd}: {msg}")
def main(opt): def main(opt):
config_path = Path(opt.config_path) login_info = LoginInfo(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() driver = get_driver()
keep_login_status(driver, login_info) keep_login_status(driver, login_info)

View File

@ -11,25 +11,12 @@ from selenium import webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select 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(): class LoginInfo():
def __init__(self, config_path: WindowsPath): def __init__(self, config_path: WindowsPath):
with open(config_path, mode="rb") as f: with open(config_path, mode="rb") as f:
config = tomllib.load(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.lang = config["login_info"]["lang"]
self.login_ID = config["login_info"]["login_ID"] self.login_ID = config["login_info"]["login_ID"]
self.login_passwd = config["login_info"]["login_passwd"] self.login_passwd = config["login_info"]["login_passwd"]
@ -45,6 +32,8 @@ class LoginInfo():
def keep_login_status(driver: webdriver.Chrome, login_info: 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. # * Close all windows except the main window.
while len(driver.window_handles) > 1: while len(driver.window_handles) > 1:
driver.switch_to.window(driver.window_handles[1]) driver.switch_to.window(driver.window_handles[1])
@ -53,29 +42,29 @@ def keep_login_status(driver: webdriver.Chrome, login_info: LoginInfo):
driver.switch_to.window(driver.window_handles[0]) driver.switch_to.window(driver.window_handles[0])
top_page = driver.current_window_handle top_page = driver.current_window_handle
driver.get(EIP_URL) driver.get(EIP_url)
time.sleep(0.3) time.sleep(0.3)
if driver.title == "CHI MOTOR WEB ERP 登入": if driver.title == "CHI MOTOR WEB ERP 登入":
# * Fill in all login information. # * Fill in all login information.
dropdown_element = driver.find_element(By.ID, LANG_DROPDOWN_ID) dropdown_element = driver.find_element(By.ID, "ddlLang")
select = Select(dropdown_element) select = Select(dropdown_element)
select.select_by_value(login_info.lang) select.select_by_value(login_info.lang)
input_text_element = driver.find_element(By.ID, LOGIN_ID_INPUT_ID) input_text_element = driver.find_element(By.ID, "txtLoginID")
input_text_element.clear() input_text_element.clear()
input_text_element.send_keys(login_info.login_ID) input_text_element.send_keys(login_info.login_ID)
input_text_element = driver.find_element(By.ID, LOGIN_PWD_INPUT_ID) input_text_element = driver.find_element(By.ID, "txtLoginPwd")
input_text_element.clear() input_text_element.clear()
input_text_element.send_keys(login_info.login_passwd) input_text_element.send_keys(login_info.login_passwd)
dropdown_element = driver.find_element(By.ID, COMPANY_ID_DROPDOWN_ID) dropdown_element = driver.find_element(By.ID, "ddlCompanyID")
select = Select(dropdown_element) select = Select(dropdown_element)
select.select_by_value(login_info.company_ID) select.select_by_value(login_info.company_ID)
# * Press the submit button. # * Press the submit button.
submit_btn = driver.find_element(By.ID, LOGIN_BUTTON_ID) submit_btn = driver.find_element(By.ID, "btnLogin")
submit_btn.click() submit_btn.click()
time.sleep(3) time.sleep(3)