首页 > 解决方案 > Selenium 通过 Outlook 邮件登录导航(无头驱动程序)

问题描述

我正在使用 Selenium 和 Python 3.9 浏览 Outlook 邮件登录。目标是成功登录后到达主收件箱页面。我正在使用 chrome 驱动程序,并且在运行下面的脚本以浏览 Outlook 登录时遇到错误。当我运行无头驱动程序或简单地最小化窗口而不进入无头时,无论我设置多长时间,登录页面似乎都会在单击密码页面上的“下一步”按钮之前冻结。有针对这个的解决方法吗?

在您指定 Outlook 登录用户名、密码和下载 Chrome 驱动程序的路径后,可以测试以下代码:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
import time

# -- Login Credentials
o_username = ' OUTLOOK LOGIN USERNAME '
o_password = ' OUTLOOK LOGIN USERNAME '

# -- Headless Driver (Ideal)
#o_options = Options()
#o_options.headless = True
#o_driver = webdriver.Chrome(options=o_options, executable_path=r' PATH TO DRIVER DOWNLOAD ')

# -- Minimized Window (Not ideal, but accepted)
o_driver = webdriver.Chrome(r' PATH TO DRIVER DOWNLOAD ')
o_driver.minimize_window()

o_driver.get('https://outlook.office.com/mail/inbox')

# -- Fill username and click next
WebDriverWait(o_driver, 10).until(EC.presence_of_element_located((By.ID, 'i0116'))).send_keys(o_username)
WebDriverWait(o_driver, 10,(NoSuchElementException,StaleElementReferenceException,)).until(EC.presence_of_element_located((By.ID, 'idSIButton9'))).click()

time.sleep(3)

# -- Fill password and click next
WebDriverWait(o_driver, 10).until(EC.presence_of_element_located((By.ID, 'i0118'))).send_keys(o_password)
WebDriverWait(o_driver, 10, (NoSuchElementException,StaleElementReferenceException,)).until(EC.presence_of_element_located((By.ID, 'idSIButton9'))).click()

try:
    # -- Detect HTML element only on main inbox page
    WebDriverWait(o_driver, 2).until(EC.presence_of_element_located((By.ID, 'Pivot29-Tab0')))
except:
    # -- Handle the 'Stay Logged In?' page and click 'Yes'
    WebDriverWait(o_driver, 5).until(EC.presence_of_element_located((By.ID, 'idSIButton9'))).click()
    time.sleep(3)

    # -- Detect the element on main inbox page
    # -- ERROR OCCURS HERE BY TIMEOUT BECAUSE PAGE IS STILL STUCK ON PASSWORD FILL-IN PAGE
    WebDriverWait(o_driver, 5).until(EC.presence_of_element_located((By.ID, 'Pivot29-Tab0')))

# -- Main Inbox Loaded

标签: pythonseleniumselenium-webdriver

解决方案


使用 GUI 来自动化电子邮件是出了名的缓慢且过于困难。许多邮件客户端不喜欢 GUI 自动化。

更好的方法是通过邮件协议本身。

有图书馆和很多指南。这可以让你开始:

import imaplib
import email
from email.header import decode_header


# account
username = "email@domain.com"
password = "password!"

imap = imaplib.IMAP4_SSL("outlook.office365.com")
imap.login(username, password)

status, messages = imap.select("INBOX")
messages = int(messages[0]) # this is how many mails you have
print ('# emails in inbox:', messages)

# count down as the highest number is latest email
# this loop is top 5 emails
for i in range(messages, messages-5, -1):
    res, msg = imap.fetch(str(i), "(RFC822)")
    for response in msg:
        if isinstance(response, tuple):
            # parse a bytes email into a message object
            msg = email.message_from_bytes(response[1])
            # decode the email subject
            subject, encoding = decode_header(msg["Subject"])[0]
            From, encoding = decode_header(msg.get("From"))[0]
            print("From: " + From + " -> Subject :" +subject)

imap.close()
imap.logout()

我不会分享我所有的 5 封电子邮件,但这是我为演示发送的测试邮件:

来自:Richard Edwards this.is.my.mail@removed.com -> 主题:Hello World

这就是我的邮件的样子: 电子邮件

您可以从您的网络邮件设置中获取您的 imap 地址。


推荐阅读