使用Selenium登录Microsoft帐户

时间:2017-10-22 20:04:26

标签: python selenium-webdriver

我正在尝试使用selenium登录我的Microsoft帐户。下面的代码会导致网站返回一条错误消息,说明登录时出现了问题。

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

我输入的密码绝对正确。是不是微软根本不希望远程控制的浏览会话尝试登录?

2 个答案:

答案 0 :(得分:2)

我认为您需要等待,因为字段不会立即显示。以下对我有用:

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

答案 1 :(得分:-1)

使用..这个..对我有用..最好等到到达所需的网址

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