1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-18 07:23:03 +08:00

refactor(LibLogin): extract the captcha recognition and input to a separate function

This commit is contained in:
2025-11-08 11:54:57 +08:00
parent a4c5ee299e
commit d2cef258aa
+40 -11
View File
@@ -134,6 +134,41 @@ class LibLogin(LibOperator):
return False return False
def __solveCaptcha(
self,
auto_captcha: bool = True,
) -> str:
max_attempts = 5
for _ in range(max_attempts):
if auto_captcha:
captcha_text = self.__autoRecognizeCaptcha()
else:
self._showTrace(f"用户未配置自动识别验证码, 请手动输入验证码 !")
captcha_text = self.__manualRecognizeCaptcha()
if captcha_text:
return captcha_text
self._showTrace(f"验证码识别失败 {max_attempts} 次, 请检查验证码是否正确 !")
return ""
def __fillCaptchaElement(
self,
captcha_text: str,
) -> bool:
try:
captcha_element = self.__driver.find_element(By.NAME, "answer")
captcha_element.clear()
captcha_element.send_keys(captcha_text)
return True
except Exception as e:
self._showTrace(f"验证码填写失败 ! : {e}")
self.__refreshCaptcha()
return False
def login( def login(
self, self,
username: str, username: str,
@@ -153,17 +188,11 @@ class LibLogin(LibOperator):
password, password,
): ):
continue continue
while True: captcha_text = self.__solveCaptcha(auto_captcha)
if auto_captcha: if not captcha_text:
captcha_text = self.__autoRecognizeCaptcha() continue
else: if not self.__fillCaptchaElement(captcha_text):
self._showTrace(f"用户未配置自动识别验证码, 请手动输入验证码 !") continue
captcha_text = self.__manualRecognizeCaptcha()
if captcha_text:
break
captcha_element = self.__driver.find_element(By.NAME, "answer")
captcha_element.clear()
captcha_element.send_keys(captcha_text)
self._showTrace("尝试登录...") self._showTrace("尝试登录...")
try: try:
self.__driver.find_element( self.__driver.find_element(