本文實例為大家分享了python3實現126郵箱登陸并發送郵件的具體代碼,供大家參考,具體內容如下
基于selenium,使用chrome瀏覽器,完成126郵箱登陸并發送發郵件功能,暫時未封裝。
from selenium import webdriver# 導入顯示等待類from selenium.webdriver.support.ui import WebDriverWait# 導入期望場景類from selenium.webdriver.support import expected_conditions as EC# 導入By類from selenium.webdriver.common.by import Byimport time #瀏覽器驅動放在了c://Python36//Scripts目錄下,無需指定參數driver= webdriver.Chrome()driver.get("https://mail.126.com/")time.sleep(3)####登陸driver.switch_to.frame("x-URS-iframe")user_name = driver.find_element_by_xpath('//*[@name="email"]')#將xxxxxxx替換為自己的用戶名user_name.send_keys('xxxxxxx')pass_word = driver.find_element_by_xpath('//*[@name="password"]')#將11111111111替換為自己的密碼pass_word.send_keys('11111111111')button = driver.find_element_by_id("dologin")button.click()driver.switch_to.default_content()time.sleep(3) ####寫郵件wait = WebDriverWait(driver,10,0.2)##wait.until(EC.visibility_of_element_located((By.XPATH,"http://span[text()='發送']")))wait.until(EC.visibility_of_element_located((By.XPATH,"http://a[contains(text(),'退出')]")))driver.find_element_by_xpath('//span[text()="寫 信"]').click()driver.find_element_by_xpath('//input[@tabindex="1" and @role="combobox"]')./ send_keys("1234h@qq.com")driver.find_element_by_xpath('//input[@tabindex="1" and @class="nui-ipt-input"]')./ send_keys("測試郵件")driver.find_element_by_xpath('//input[@type="file"]').send_keys("f://b.txt")time.sleep(5) wait.until(EC.visibility_of_element_located((By.XPATH,"http://span[text()='上傳完成']")))driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@tabindex=1]'))driver.execute_script("document.getElementsByTagName('body')[0].innerHTML='<b>郵件的正文內容<b>;'")driver.switch_to.default_content() ##發送driver.find_element_by_xpath('//span[text()="發送"]').click()time.sleep(5)assert '發送成功' in driver.page_sourcelogout_link=driver.find_element_by_xpath("http://a[text()='退出']")time.sleep(3)assert u"登錄" in driver.page_source driver.quit()以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答