Python+Selenium+Webdriver+unittest 實現登錄126郵箱
第一版:登錄
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver import ActionChains from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.keys import Keys class setPageLoadTime(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_PageLoadTime(self): # 設定頁面加載限制時間為4秒 self.driver.set_page_load_timeout(4) #重點,限制超時時間 self.driver.maximize_window() try: startTime = time.time() self.driver.get("http://mail.126.com") except TimeoutException: print (u'頁面加載超過設定時間,超時') # 當頁面加載時間超過設定時間, # 通過執行Javascript來stop加載,然后繼續執行后續動作 self.driver.execute_script('window.stop()') end = time.time() - startTime print (end) # 切換進frame控件 self.driver.switch_to.frame(self.driver.find_element_by_xpath("//iframe[contains(@id,'x-URS-iframe')]")) # 獲取用戶名輸入框 time.sleep(5) userName = self.driver.find_element_by_xpath("//input[@data-placeholder='郵箱帳號或手機號碼']") # 輸入用戶名 userName.send_keys("******") # 獲取密碼輸入框 pwd = self.driver.find_element_by_xpath("//input[@data-placeholder='輸入密碼' and @name='password']") # 輸入密碼 pwd.send_keys("*******") # 發送一個回車鍵 pwd.send_keys(Keys.RETURN) time.sleep(5) assert u"退出" in self.driver.page_source def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
結果:

第二版:登錄+發送郵件
#encoding=utf-8 import unittest,time,traceback from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException,NoSuchElementException from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By class TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_SouhuMailSendMail(self): # 設定頁面加載限制時間為4秒 self.driver.set_page_load_timeout(4) self.driver.maximize_window() try: self.driver.get("http://mail.126.com") except TimeoutException: print (u'頁面加載超過設定時間,超時') self.driver.execute_script('window.stop()') try: # 切換進frame控件 self.driver.switch_to.frame(self.driver.find_element_by_xpath("//iframe[contains(@id,'x-URS-iframe')]")) #time.sleep(5) # 用戶名 userName = self.driver.find_element_by_xpath("//input[@data-placeholder='郵箱帳號或手機號碼']") userName.send_keys("****") # 密碼 pwd = self.driver.find_element_by_xpath("//input[@data-placeholder='輸入密碼' and @name='password']") pwd.send_keys("****") # 登錄按鈕,回車代替 pwd.send_keys(Keys.RETURN) # 顯式等待,確認頁面成功登錄并跳轉到登錄成功后的首頁 wait = WebDriverWait(self.driver,10) wait.until(EC.element_to_be_clickable((By.XPATH,'//span[text()="寫 信"]'))) time.sleep(2) self.driver.find_element_by_xpath('//span[text()="寫 信"]').click() time.sleep(2) # 輸入收件人 receiver = self.driver.find_element_by_xpath("//*[contains(@id,'_mail_emailinput')]//input") receiver.send_keys("****") # 輸入郵件標題 subject = self.driver.find_element_by_xpath('//*[contains(@id,"subjectInput")]') subject.send_keys(u"這是一封測試郵件") # 獲取郵件正文的富文本框 iframe = self.driver.find_element_by_xpath("//*[contains(@id,'editor')]//iframe") self.driver.switch_to.frame(iframe) editBox = self.driver.find_element_by_xpath("/html/body") editBox.send_keys(u"郵件正文內容") self.driver.switch_to.default_content() # 點擊發送按鈕 self.driver.find_element_by_xpath("//span[.='發送']").click() # 顯式等待發送成功元素是否出現在頁面上 wait.until(EC.visibility_of_element_located((By.XPATH,"//*[contains(@id,'succInfo')]"))) print(u"郵件發送成功") except TimeoutException: print(u"顯式等待頁面元素超時") except NoSuchElementException: print(u"尋找的頁面元素不存在",traceback.print_exc()) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
結果:

浙公網安備 33010602011771號