From 49dc53ed324f60424d7b6ed3491d37d874e92b8b Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 10 Apr 2024 11:04:54 +0200 Subject: [PATCH] Fix 2FA (language setting is not always working) --- V6.py | 37 +++++++++++++++++++++++-------------- modules/driver_tools.py | 4 ++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/V6.py b/V6.py index 9cbc108..73e7ee7 100755 --- a/V6.py +++ b/V6.py @@ -10,6 +10,7 @@ from modules.driver_tools import * from modules.error import * import os +from selenium.common.exceptions import NoSuchElementException # create a webdriver def create_driver(mobile=False): @@ -383,22 +384,30 @@ def login_part_1(): driver = config.WebDriver.driver driver.get("https://login.live.com") wait_until_visible(By.ID, "i0116", browser=driver) - mail_elem = driver.find_element(By.ID, "i0116") - send_keys_wait(mail_elem, config.UserCredentials.get_mail()) - mail_elem.send_keys(Keys.ENTER) + send_wait_and_confirm( + driver.find_element(By.ID, "i0116"), + config.UserCredentials.get_mail() + ) wait_until_visible(By.ID, "i0118", browser=driver) - pwd_elem = driver.find_element(By.ID, "i0118") - send_keys_wait(pwd_elem, config.UserCredentials.get_password()) - pwd_elem.send_keys(Keys.ENTER) - custom_sleep(2) + send_wait_and_confirm( + driver.find_element(By.ID, "i0118"), + config.UserCredentials.get_password() + ) # 2FA - if "Entrez le code de sécurité" in driver.page_source: - try: - a2f_elem = driver.find_element(By.ID, "idTxtBx_SAOTCC_OTC") - a2f_elem.send_keys(config.UserCredentials.get_tfa().now()) - a2f_elem.send_keys(Keys.ENTER) - except Exception as err: - log_error(err) + try: + wait_until_visible(By.ID, "idTxtBx_SAOTCC_OTC", browser=driver, timeout=5) + + a2f_code = config.UserCredentials.get_tfa().now() + info(f"Need 2FA, I have code: {a2f_code}") + send_wait_and_confirm( + driver.find_element(By.ID, "idTxtBx_SAOTCC_OTC"), + a2f_code + ) + except NoSuchElementException: + custom_sleep(2) + pass + except Exception as err: + log_error(err) # Accept all cookies question, and check if the account is locked diff --git a/modules/driver_tools.py b/modules/driver_tools.py index 6208f58..7147515 100644 --- a/modules/driver_tools.py +++ b/modules/driver_tools.py @@ -60,6 +60,10 @@ def send_keys_wait(element, keys: str) -> None: element.send_keys(i) sleep(uniform(0.1, 0.3)) +def send_wait_and_confirm(element, keys: str) -> None: + send_keys_wait(element, keys) + element.send_keys(Keys.ENTER) + # Wait for the presence of the element identifier or [timeout]s def wait_until_visible(search_by: str, identifier: str, timeout: int = 20, browser=None) -> bool: