2fa验证重试中点击后再判断

This commit is contained in:
Your Name
2025-11-06 23:31:57 +08:00
parent 866e938468
commit 4bedbd67fc

View File

@@ -877,36 +877,30 @@ def login_with_2fa_retry(page, code_2fa, max_retries=3):
) )
code_input.fill(auth_code) code_input.fill(auth_code)
# 等待可能的验证结果 # 点击继续
page.wait_for_timeout(2000) # 等待2秒让系统处理 page.query_selector('div[role="button"][aria-label="Continue"]').click()
# 检查是否出现错误信息 # 检查是否出现错误信息
error_element = page.wait_for_selector( try:
'span:has-text("This code doesnt work. Check its correct or try a new one.")', timeout=10000) error_element = page.wait_for_selector(
'span:has-text("This code doesnt work. Check its correct or try a new one.")', timeout=3000)
if error_element and error_element.is_visible():
print("2FA验证码错误准备重试...")
retry_count += 1
if error_element and error_element.is_visible(): # 清空输入框重新输入
print("2FA验证码错误准备重试...") code_input.fill("")
retry_count += 1
# 清空输入框重新输入 # 如果不是最后一次重试,等待一段时间再重试
code_input.fill("") if retry_count < max_retries:
wait_time = 5 # 等待5秒
# 如果不是最后一次重试,等待一段时间再重试 print(f"等待 {wait_time} 秒后重试...")
if retry_count < max_retries: time.sleep(wait_time)
wait_time = 5 # 等待5秒 continue
print(f"等待 {wait_time} 秒后重试...") except TimeoutError:
time.sleep(wait_time)
continue
else:
logger.info("2FA验证成功") logger.info("2FA验证成功")
return True return True
except TimeoutError:
logger.info("查找输入框超时,可能是页面加载问题")
retry_count += 1
if retry_count < max_retries:
time.sleep(3)
continue
except Exception as e: except Exception as e:
logger.info(f"2FA验证发生未知错误: {e}") logger.info(f"2FA验证发生未知错误: {e}")
retry_count += 1 retry_count += 1
@@ -1009,8 +1003,6 @@ def playwright_m_login(username, password, code_2fa=None):
except Exception as e: except Exception as e:
logger.error(f"2FA验证失败: {e}", exc_info=True) logger.error(f"2FA验证失败: {e}", exc_info=True)
raise OperationFailed(f"2FA验证失败: {e}") raise OperationFailed(f"2FA验证失败: {e}")
# 点击继续
page.query_selector('div[role="button"][aria-label="Continue"]').click()
# 等待登录成功, 账号被检测自动化程序会卡在这里. 直接忽略报错 # 等待登录成功, 账号被检测自动化程序会卡在这里. 直接忽略报错
try: try:
@@ -1145,7 +1137,7 @@ if __name__ == '__main__':
# # # 视频链接的帖子点赞 # # # 视频链接的帖子点赞
# print(playwright_like(cookies, "https://www.facebook.com/groups/1070754870427928/permalink/1873461830157224/")) # print(playwright_like(cookies, "https://www.facebook.com/groups/1070754870427928/permalink/1873461830157224/"))
# #
cookies = playwright_m_login('61582494086321', '47a67lDCC8', '4AQBIGCZDKKZ4WKKDZQIOAAKJ2A4QACR') cookies = playwright_m_login('61583069571290', '47lfsryRmI', 'IJ6YM5MX6HEMRRIDSNUMSCEBJEIHVH3R')
print(cookies) print(cookies)
# pass # pass