Python Forum

Full Version: Microsoft text phone verifying account
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello folks,
I am not an expert, yet :), in python but have one question for you?
Until a few days ago everything worked well regarding my python script but now I do not know how to verify my (Microsoft) account via text message.
Username and password passed and then it asks me to verify the account via text message code?
Is there some solution for this?
Thank you in advance.
Below is the piece of my code, but I guess this should be refined for text message verification.

def login(driver):
    """
    Login to microsoft services in this session so we can
    navigate to other pages freely
    """
    # Open page to get authenticated
    driver.get('https://login.microsoftonline.com/')
    logging.info("Microsoft login page opened")
    time.sleep(15)
    # Find username/email input element
    username_input = driver.find_element_by_id("i0116")
    # Enter email and press enter
    username_input.send_keys(EMAIL)
    username_input.send_keys(Keys.ENTER)
    logging.info("Email entered to login form")
    time.sleep(15)
    # Get password field, enter pwd and press enter to log in
    password_input = driver.find_element_by_id("i0118")
    password_input.send_keys(PASSWORD)
    logging.info("Password entered to login form")
    password_input.send_keys(Keys.ENTER)
    logging.info("Form submitted")    
    time.sleep(15)
you didn't explain which package you are using I will assume selenium for this response:

instead of using sleep for a specified amount of time, use WebDriverWait and wait until presense of
something which changes after your submission.

Advantage is that the delay will only last as long as necessary, so you can make it larger.
If you do this, the longer delay will only occur if necessary

In the following example (see: https://selenium-python.readthedocs.io/waits.html ):

'delay' variable was set during class initialization to 30, usually only uses less than one second, and as mentioned the selenium driver will only use the necessary time, then continue

i am expecting the text to show up in element see 'Expected Conditions' on above webpage.
see: ( https://selenium-python.readthedocs.io/a...nt_located

i wait for an element using CSS_SELECTOR there are several choices.

Here it's waiting for display of page number text: 'Page 1 of 1, records 1 to 1 of 1' or similar.
Rarely fails.

Example:
    element = WebDriverWait(self.browser, delay).until( \
        EC.presence_of_element_located((By.CSS_SELECTOR, '#txtCommonPageNo')))
(Jul-21-2022, 11:35 AM)Larz60+ Wrote: [ -> ]you didn't explain which package you are using I will assume selenium for this response:

instead of using sleep for a specified amount of time, use WebDriverWait and wait until presense of
something which changes after your submission.

Advantage is that the delay will only last as long as necessary, so you can make it larger.
If you do this, the longer delay will only occur if necessary

In the following example (see: https://selenium-python.readthedocs.io/waits.html ):

'delay' variable was set during class initialization to 30, usually only uses less than one second, and as mentioned the selenium driver will only use the necessary time, then continue

i am expecting the text to show up in element see 'Expected Conditions' on above webpage.
see: ( https://selenium-python.readthedocs.io/a...nt_located

i wait for an element using CSS_SELECTOR there are several choices.

Here it's waiting for display of page number text: 'Page 1 of 1, records 1 to 1 of 1' or similar.
Rarely fails.

Example:
    element = WebDriverWait(self.browser, delay).until( \
        EC.presence_of_element_located((By.CSS_SELECTOR, '#txtCommonPageNo')))

Yes, you are correct, I am using selenium
import logging
import smtplib
import time
from datetime import datetime
from email.message import EmailMessage
from os import environ, path

from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.keys import Keys

# Constants
WEBDRIVER_PATH = "C:\\Tools\\msedgedriver.exe" 
# Get our email and password from Enviroment Variables
So, if I apply your solution from above I will resolve my problem with text message account verification since Edge every time (after closing it) asks me for text message verification.