python - 使用 Selenium 登录 Microsoft 帐户

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

我输入的密码绝对正确。难道微软只是不想让远程控制的浏览 session 尝试登录?

最佳答案

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

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

关于python - 使用 Selenium 登录 Microsoft 帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46878621/

相关文章:

python - xpath 属性包含特殊字符

python - 属性错误: 'module' object has no attribute 'ver'

python - BeautifulSoup:根据前面标签的内容打印 div

selenium-webdriver - 如何为在 Ruby 中的 Jenkins 服务器上运行的 Cucumber 设置 ChromeDriver 的路径?

python - ElementClickInterceptedException : element click intercepted:

python - Scrapy:下一个按钮使用 javascript

python - 在第一个 pandas python 上排序时保持第二级多索引完整

java - 用于 Firefox 的基于 Java 的 Selenium WebDriver 项目的示例 Maven pom.xml

java - 如何使用 Selenium webdriver 执行 Control + Save 操作?

python - Pandas - 日期时间索引的前 X 小时的总和