user7351652
user7351652

Reputation:

Logging into Microsoft account using Selenium

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?

Upvotes: 11

Views: 20796

Answers (3)

Carlos Ati
Carlos Ati

Reputation: 1

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()

Upvotes: 0

jitendra
jitendra

Reputation: 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();
        }

Upvotes: -1

Damian Jansen
Damian Jansen

Reputation: 236

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()

Upvotes: 16

Related Questions