1. 程式人生 > >webdriver實現用126郵箱給你自己發一個帶有附件、標題、正文的郵件

webdriver實現用126郵箱給你自己發一個帶有附件、標題、正文的郵件

info 驅動 需要 密碼 editor ucc add 郵箱 content

from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException, NoSuchElementException import time, traceback driver = None wait = None #登錄函數 def login_126(): global driver global wait url = "http://www.126.com" driver = webdriver.Chrome(executable_path="d:\\chromedriver") driver.get(url) # 定義顯式等待對象 wait = WebDriverWait(driver, 10, 0.2) try: # 切入登錄框所在的iframe wait.until(EC.frame_to_be_available_and_switch_to_it(( By.XPATH, ‘//iframe[contains(@id,"x-URS-iframe")]‘))) time.sleep(3) # driver.switch_to.frame(driver.find_element_by_xpath( # ‘//iframe[contains(@id,"x-URS-iframe")]‘)) # 顯式等待用戶名輸入框可見且獲取輸入框元素對象 username = wait.until(lambda x: x.find_element_by_xpath( ‘//input[@placeholder="郵箱帳號或手機號"]‘)) username.send_keys("xxxx") # 直接獲取密碼輸入框 passwd = driver.find_element_by_xpath(‘//input[@name="password"]‘) passwd.send_keys("xxxxx") # 獲取登錄按鈕並點擊 driver.find_element(By.ID, "dologin").click() #且出到正文 driver.switch_to.default_content() # 顯式等待登錄成功後“退出”按鈕出現在頁面上 if wait.until(EC.visibility_of_element_located((‘xpath‘, ‘//a[.="退出"]‘))): print("登錄成功!") else: print("登錄失敗!") except NoSuchElementException as e: print(traceback.print_exc()) except TimeoutException as e: print(traceback.print_exc()) except Exception as e: print(traceback.print_exc()) #退出驅動並關閉所有瀏覽器窗口 def close_driver(): global driver driver.quit() def send_mail(): global driver global wait try: #顯式等待寫信按鈕可點擊並獲取寫信按鈕元素對象 write_button = wait.until(EC.element_to_be_clickable((‘xpath‘,‘//span[.="寫 信"]‘))) write_button.click() #顯式等待收件人輸入框可見 receiver = wait.until(lambda x : x.find_element_by_xpath( ‘//input[@class="nui-editableAddr-ipt"]‘)) #receiver = wait.until(EC.visibility_of_element_located((‘xpath‘,‘//span[.="寫 信"]‘))) receiver.send_keys("[email protected]") #獲取主題輸入框頁面元素對象 subject = driver.find_element_by_xpath(‘//input[contains(@id,"subjectInput")]‘) subject.send_keys("2019你要怎麽做?") #獲取添加附件元素對象(用send_keys()需要定位到input元素) attachment = driver.find_element_by_xpath(‘//input[@class="O0"]‘) attachment.send_keys("e:\\python\\111.txt") #等待附件上傳完成 wait.until(EC.visibility_of_element_located((‘xpath‘,‘//span[.="上傳完成"]‘))) print("附件上傳完成!") time.sleep(3) #切入正文frame wait.until(EC.frame_to_be_available_and_switch_to_it( (‘xpath‘,‘//iframe[@class="APP-editor-iframe"]‘))) contentBox = driver.find_element_by_xpath(‘/html/body‘) contentBox.send_keys("學習 理財 看書 如何轉更多錢錢,不為錢而工作") #切出frame,切到正文 driver.switch_to.default_content() #發送 driver.find_element_by_xpath( ‘//footer[@class="jp0"]//span[text()="發送"]//preceding-sibling::span//b‘).click() #顯式等待發送成功的//h1[contains(@id,"succInfo")] //h1[text()="發送成功"] if wait.until(EC.visibility_of_element_located((‘xpath‘,‘//h1[text()="發送成功"]‘))): print("郵件發送成功!") else: print("郵件發送失敗!") time.sleep(5) except NoSuchElementException as e: print(traceback.print_exc()) except TimeoutException as e: print(traceback.print_exc()) except Exception as e: print(traceback.print_exc()) def main(): login_126() send_mail() close_driver() if __name__ == "__main__": main()

webdriver實現用126郵箱給你自己發一個帶有附件、標題、正文的郵件