1. 程式人生 > >自動登錄火狐郵箱發送攜帶附件的郵件

自動登錄火狐郵箱發送攜帶附件的郵件

def clas pri ext cas ren web ring str

# encoding=utf-8 from selenium import webdriver import unittest, time, traceback from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException, NoSuchElementException import win32clipboard as w import win32con import win32api from selenium.webdriver.common.keys import Keys #設置剪貼板內容 def setText(aString): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT,aString) w.CloseClipboard() # 鍵盤按鍵映射字典 VK_CODE = { ‘enter‘:0x0D, ‘ctrl‘:0x11, ‘v‘:0x56} # 鍵盤鍵按下 def keyDown(keyName): win32api.keybd_event(VK_CODE[keyName], 0, 0, 0) # 鍵盤鍵擡起 def keyUp(keyName): win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0) class MyClass(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox(executable_path="d:\\geckodriver") #self.driver = webdriver.Chrome(executable_path="d:\\chromedriver") def test_multext(self): url = "http://mail.sohu.com" self.driver.get(url) # 定義WebDriverWait對象 wait = WebDriverWait(self.driver, 10, 0.2) try: # 顯式等待用戶名輸入框可見,並獲取輸入框元素對象 #username = wait.until( #lambda x: x.find_element_by_xpath( #‘//input[@placeholder="請輸入您的郵箱"]‘)) username = self.driver.find_element_by_xpath( ‘//input[@placeholder="請輸入您的郵箱"]‘) wait.until(EC.visibility_of(username)) # username = wait.until(EC.visibility_of_element_located( # ("xpath",‘//input[@placeholder="請輸入您的郵箱"]‘))) username.clear() username.send_keys("[email protected]") # 顯式等待密碼輸入框可見,並獲取輸入框元素對象 passwd = wait.until(lambda x: x.find_element( By.XPATH, ‘//input[@placeholder="請輸入您的密碼"]‘)) passwd.send_keys("xxxxxx") # 獲取登錄按鈕元素對象 login_button = self.driver.find_element_by_xpath(‘//input[@value="登 錄"]‘) login_button.click() # 顯式等待“寫郵件”按鈕出現,並獲取“寫郵件”按鈕元素對象(頁面有跳轉) writeMail_b = wait.until(EC.element_to_be_clickable( (By.XPATH, ‘//li[text()="寫郵件"]‘))) # writeMail_b = wait.until( # lambda x : x.find_element_by_xpath(‘//li[text()="寫郵件"]‘)) writeMail_b.click() # 顯式等待收件人輸入框可見,並獲取輸入框元素對象(頁面有跳轉) #receiver = wait.until(lambda x: x.find_element_by_xpath( #‘//div[@arr="mail.to_render"]//input[@ng-model="addrInput"]‘)) receiver = wait.until(EC.visibility_of_element_located( ("xpath",‘//div[@arr="mail.to_render"]//input[@ng-model="addrInput"]‘))) receiver.send_keys("[email protected]") time.sleep(3) # 直接獲取主題輸入框元素對象(可不用顯式等待,此時頁面已加載) subject = self.driver.find_element_by_xpath(‘//input[@ng-model="mail.subject"]‘) subject.send_keys("測試搜狐郵箱發郵件") # 獲取上傳附件元素對象 attachment = self.driver.find_element_by_xpath(‘//span[.="添加附件(50M)"]‘) attachment.click() #attachment.send_keys("e:\\1.gif") time.sleep(3) #設置剪貼板的內容 setText("e:\\1.gif") #粘貼內容到文件輸入框 time.sleep(3) keyDown("ctrl") keyDown("v") keyUp("v") keyUp("ctrl") time.sleep(2)#防止執行太快沒有粘貼上 #點擊彈出框的“打開”按鈕 keyDown("enter") keyUp("enter") time.sleep(3)#等待上傳完成 time.sleep(3)#切入frame前最好暫停下,否則可能定位不到frame # 切入正文所在的frame self.driver.switch_to.frame(self.driver.find_element_by_xpath( ‘//iframe[contains(@id,"ueditor")]‘)) # 獲取正文輸入框 contentBox = self.driver.find_element_by_xpath(‘/html/body‘) contentBox.send_keys("來自外太空的一封神秘郵件!") # 從正文frame切出 self.driver.switch_to.default_content() # 點擊發送按鈕 self.driver.find_element_by_xpath(‘//span[.="發送"]‘).click() # 顯式等待"發送成功" 出現 wait.until(EC.visibility_of_element_located((‘xpath‘, ‘//span[.="發送成功"]‘))) 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()) time.sleep(5) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()

自動登錄火狐郵箱發送攜帶附件的郵件