Compare commits
9 Commits
3c23ee2ce9
...
86bd790f1c
Author | SHA1 | Date |
---|---|---|
|
86bd790f1c | |
|
ccd5dee807 | |
|
bee7927ef9 | |
|
8579b135cf | |
|
07e01554d0 | |
|
953b6ab909 | |
|
77054f1d84 | |
|
6523fecb5a | |
|
73f0e62301 |
|
@ -6,3 +6,4 @@ __pycache__/
|
||||||
|
|
||||||
# config file
|
# config file
|
||||||
config.toml
|
config.toml
|
||||||
|
uv.lock
|
||||||
|
|
|
@ -1 +1,7 @@
|
||||||
# auto_login_EIP
|
# auto_login_EIP
|
||||||
|
|
||||||
|
- 執行
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# uv run python main.py --config_path .\config.toml.example
|
||||||
|
```
|
||||||
|
|
4
main.py
4
main.py
|
@ -92,8 +92,8 @@ def main(opt):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# * run command
|
# * run command
|
||||||
keep_login_status(driver, login_info)
|
if keep_login_status(driver, login_info):
|
||||||
action_agent.run(action_code)
|
action_agent.run(action_code)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
[project]
|
||||||
|
name = "auto-login-eip"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
authors = [{ name = "Kinoshita Kenta", email = "ybs0306748@gmail.com" }]
|
||||||
|
requires-python = ">=3.9"
|
||||||
|
readme = "README.md"
|
||||||
|
dependencies = [
|
||||||
|
"selenium (>=4.27.1,<5.0.0)",
|
||||||
|
"tomli (>=2.2.1,<3.0.0)",
|
||||||
|
"webdriver-manager (>=4.0.2,<5.0.0)",
|
||||||
|
"rich (>=13.9.4,<15.0.0)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||||
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
||||||
|
[tool.uv]
|
||||||
|
package = false
|
|
@ -1,9 +0,0 @@
|
||||||
# for parsing toml file
|
|
||||||
tomli
|
|
||||||
|
|
||||||
# `webdriver_manager` manages webdriver versions
|
|
||||||
webdriver_manager
|
|
||||||
selenium
|
|
||||||
|
|
||||||
# better output
|
|
||||||
rich
|
|
|
@ -14,6 +14,7 @@ class ActionType(IntEnum):
|
||||||
請假 = 1
|
請假 = 1
|
||||||
補卡 = 2
|
補卡 = 2
|
||||||
訂便當 = 3
|
訂便當 = 3
|
||||||
|
訂會議室 = 4
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def has_value(cls, value):
|
def has_value(cls, value):
|
||||||
|
@ -25,7 +26,8 @@ class Action():
|
||||||
self.action_list = {ActionType.登入: self.__登入,
|
self.action_list = {ActionType.登入: self.__登入,
|
||||||
ActionType.請假: self.__請假,
|
ActionType.請假: self.__請假,
|
||||||
ActionType.補卡: self.__補卡,
|
ActionType.補卡: self.__補卡,
|
||||||
ActionType.訂便當: self.__訂便當
|
ActionType.訂便當: self.__訂便當,
|
||||||
|
ActionType.訂會議室: self.__訂會議室
|
||||||
}
|
}
|
||||||
self.driver = driver
|
self.driver = driver
|
||||||
|
|
||||||
|
@ -125,9 +127,17 @@ class Action():
|
||||||
|
|
||||||
order_title, img, order_status = a_tag
|
order_title, img, order_status = a_tag
|
||||||
if order_status.text != "【已訂購】":
|
if order_status.text != "【已訂購】":
|
||||||
order_title.click()
|
for attempt in range(5):
|
||||||
open_page_num += 1
|
try:
|
||||||
time.sleep(0.05)
|
order_title.click()
|
||||||
|
open_page_num += 1
|
||||||
|
time.sleep(0.05)
|
||||||
|
break # 點成功就跳出 retry
|
||||||
|
except ElementClickInterceptedException:
|
||||||
|
print("點擊被遮蔽,重試中...")
|
||||||
|
time.sleep(0.2) # 等一下再重試
|
||||||
|
else:
|
||||||
|
print("點擊失敗:可能被遮蔽或其他原因")
|
||||||
|
|
||||||
if open_page_num > 0:
|
if open_page_num > 0:
|
||||||
print(f"已開啟 {open_page_num} 個訂購頁面")
|
print(f"已開啟 {open_page_num} 個訂購頁面")
|
||||||
|
@ -135,3 +145,16 @@ class Action():
|
||||||
print("沒有尚未訂購的團購訂單")
|
print("沒有尚未訂購的團購訂單")
|
||||||
|
|
||||||
self.driver.switch_to.default_content()
|
self.driver.switch_to.default_content()
|
||||||
|
|
||||||
|
def __訂會議室(self):
|
||||||
|
"""訂會議室"""
|
||||||
|
|
||||||
|
self.driver.switch_to.frame(self.driver.find_element(By.ID, "main"))
|
||||||
|
all_meeting_room_tag = self.driver.find_element(By.ID, "WPPublicResource_TreeTagt0") # 會議室
|
||||||
|
ActionChains(self.driver).move_to_element(all_meeting_room_tag).move_to_element(all_meeting_room_tag).click(all_meeting_room_tag).perform()
|
||||||
|
|
||||||
|
Zhubei_tag = self.driver.find_element(By.ID, "WPPublicResource_TreeTagt1") # 竹北
|
||||||
|
ActionChains(self.driver).move_to_element(Zhubei_tag).move_to_element(Zhubei_tag).click(Zhubei_tag).perform()
|
||||||
|
|
||||||
|
meeting_room_500_tag = self.driver.find_element(By.ID, "WPPublicResource_TreeTagt2") # 500會議室
|
||||||
|
ActionChains(self.driver).move_to_element(meeting_room_500_tag).move_to_element(meeting_room_500_tag).click(meeting_room_500_tag).perform()
|
||||||
|
|
|
@ -8,6 +8,7 @@ if sys.version_info >= (3, 11):
|
||||||
else:
|
else:
|
||||||
import tomli as tomllib
|
import tomli as tomllib
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
|
from selenium.common.exceptions import NoAlertPresentException, UnexpectedAlertPresentException
|
||||||
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
|
||||||
|
|
||||||
|
@ -44,7 +45,22 @@ class LoginInfo():
|
||||||
"please enter your passwd: ")
|
"please enter your passwd: ")
|
||||||
|
|
||||||
|
|
||||||
def keep_login_status(driver: webdriver.Chrome, login_info: LoginInfo):
|
def keep_login_status(driver: webdriver.Chrome, login_info: LoginInfo) -> bool:
|
||||||
|
"""
|
||||||
|
Attempt to log into the CHI MotorWeb ERP system using the provided login information.
|
||||||
|
|
||||||
|
This function navigates to the login page, fills in the login form with the user’s credentials,
|
||||||
|
and attempts to log in. It handles unexpected alert pop-ups that indicate login failure,
|
||||||
|
and manages browser windows to ensure only the relevant page remains open.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
driver (webdriver.Chrome): An instance of Selenium WebDriver controlling a Chrome browser.
|
||||||
|
login_info (LoginInfo): A data object containing login credentials and preferences.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if login is successful or already logged in; False if login failed (e.g., due to incorrect credentials).
|
||||||
|
"""
|
||||||
|
|
||||||
# * 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])
|
||||||
|
@ -77,33 +93,55 @@ def keep_login_status(driver: webdriver.Chrome, login_info: LoginInfo):
|
||||||
# * 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, LOGIN_BUTTON_ID)
|
||||||
submit_btn.click()
|
submit_btn.click()
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
time.sleep(3)
|
# * Check if login failed (alert popup)
|
||||||
|
try:
|
||||||
|
alert = driver.switch_to.alert
|
||||||
|
print(f"Login error message: {alert.text}")
|
||||||
|
alert.accept()
|
||||||
|
return False # Skip remaining logic, login failed
|
||||||
|
except NoAlertPresentException:
|
||||||
|
pass # No alert, proceed
|
||||||
|
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
# * If login has pop a new window, switch main window to the new one.
|
|
||||||
login_page_handle = ""
|
login_page_handle = ""
|
||||||
main_page_handle = ""
|
main_page_handle = ""
|
||||||
|
|
||||||
for handle in driver.window_handles:
|
for handle in driver.window_handles:
|
||||||
driver.switch_to.window(handle)
|
driver.switch_to.window(handle)
|
||||||
if "CHI MotorWeb - " in driver.title:
|
try:
|
||||||
|
title = driver.title
|
||||||
|
except UnexpectedAlertPresentException:
|
||||||
|
try:
|
||||||
|
alert = driver.switch_to.alert
|
||||||
|
print(f"Unexpected alert: {alert.text}")
|
||||||
|
alert.accept()
|
||||||
|
continue
|
||||||
|
except NoAlertPresentException:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if "CHI MotorWeb - " in title:
|
||||||
main_page_handle = handle
|
main_page_handle = handle
|
||||||
elif "CHI MOTOR WEB ERP 登入" in driver.title:
|
elif "CHI MOTOR WEB ERP 登入" in title:
|
||||||
login_page_handle = handle
|
login_page_handle = handle
|
||||||
|
|
||||||
# * get the page handle that should be stay
|
stay_page_handle = main_page_handle or login_page_handle or top_page
|
||||||
if main_page_handle:
|
|
||||||
stay_page_handle = main_page_handle
|
|
||||||
elif login_page_handle:
|
|
||||||
stay_page_handle = login_page_handle
|
|
||||||
else:
|
|
||||||
stay_page_handle = top_page
|
|
||||||
|
|
||||||
# * close unnecessary pages
|
# * close unnecessary pages
|
||||||
for handle in driver.window_handles:
|
for handle in driver.window_handles:
|
||||||
if handle != stay_page_handle:
|
if handle != stay_page_handle:
|
||||||
driver.switch_to.window(handle)
|
try:
|
||||||
driver.close()
|
driver.switch_to.window(handle)
|
||||||
|
driver.close()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error closing window {handle}: {e}")
|
||||||
|
|
||||||
driver.switch_to.window(stay_page_handle)
|
driver.switch_to.window(stay_page_handle)
|
||||||
driver.maximize_window()
|
driver.maximize_window()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
elif "CHI MotorWeb - " in driver.title:
|
||||||
|
return True
|
||||||
|
|
Loading…
Reference in New Issue