Compare commits
4 Commits
ef204f1aae
...
3c23ee2ce9
Author | SHA1 | Date |
---|---|---|
|
3c23ee2ce9 | |
|
0671d49497 | |
|
e32063e2ce | |
|
cfd1e5e61a |
29
main.py
29
main.py
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue