Logging into Microsoft account using Selenium
Asked Answered
N

3

11

I am trying to use selenium to log into my Microsoft account. The code below causes the site to return an error message saying something went wrong during login.

from selenium import webdriver
import time

browser = webdriver.Firefox()
browser.get('https://login.live.com')

#locating email field and entering my email
elem = browser.find_element_by_id("i0116")
elem.send_keys("myEmailAddress")

#locating password field and entering my password
elem2 = browser.find_element_by_id("i0118")
elem2.send_keys("myPassword")


elem.submit()

The password I am entering is definitely correct. Could it be that Microsoft simply does not want remote controlled browsing sessions to attempt login?

Neology answered 22/10, 2017 at 20:4 Comment(0)
P
16

I think you need to be waiting, as the fields don't show immediately. The following worked for me:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By

EMAILFIELD = (By.ID, "i0116")
PASSWORDFIELD = (By.ID, "i0118")
NEXTBUTTON = (By.ID, "idSIButton9")

browser = webdriver.Firefox()
browser.get('https://login.live.com')

# wait for email field and enter email
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(EMAILFIELD)).send_keys("myEmailAddress")

# Click Next
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(NEXTBUTTON)).click()

# wait for password field and enter password
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(PASSWORDFIELD)).send_keys("myPassword")

# Click Login - same id?
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(NEXTBUTTON)).click()
Portentous answered 23/10, 2017 at 4:7 Comment(4)
I was actually able to accomplish this by using the time.sleep() function. But this is likely a better solution.Neology
Definitely. Use sleep sparingly, it can have unpredictable outcomes.Portentous
Using selenium how would you add two-factor authentication support? The "Verify your identity" page is a form-group where you need to choose between mediums for the two-factor and the choices are presented in a form-group of tables?Prizewinner
I had this exact same problem this morning and a quickly learned for myself using a Thread.Sleep(3000); allows the system to have a moment to catch up, then login is smooth. So I agree, this answer is a better solution and will apply this to my code when I’m back in the office today.Frei
H
0

I use this code, it waits a second until the home button or any other element I need appears, so I don't depend on a static timeout.

driver.get("https://login.live.com")
valcon = True
while (valcon):
    try:
        if (driver.find_element(By.ID, 'idSIButton9') is None):
            time.sleep(1)
            valcon = True
        else:
            time.sleep(1)
            valcon = False
    except:
        valcon = True

u = "[email protected]"
usu = driver.find_element(By.ID, 'i0116')
usu.clear()
usu.send_keys(u)
ing = driver.find_element(By.ID, 'idSIButton9')
ing.click()
Hyperparathyroidism answered 13/11, 2023 at 17:14 Comment(0)
D
-1

use.. this.. work for me..better to wait until reach on desired URL

wait.until((Function) ExpectedConditions.urlContains("https://login.microsoftonline.com/common/SAS/ProcessAuth"));

        try
        {
            Thread.sleep(5000);
        }
        catch (Exception exception)
        {

        }
        WebElement confirmationButton = driver.findElement(By.xpath("//*[@id=\"idSIButton9\"]"));

        if (confirmationButton.isEnabled())
        {
            confirmationButton.click();
        }
Dylandylana answered 17/2, 2021 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.