python webdriver firefox 登入126郵箱,先新增連絡人...,然後進入首頁發送郵件,帶附件。

來源:互聯網
上載者:User

標籤:span   login   encoding   source   iad   rom   window   box   exp   

代碼:
#encoding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

#print u"啟動瀏覽器..."
print "start browser..."
#建立Firefox瀏覽器執行個體
driver=webdriver.Firefox(executable_path="c:\\geckodriver")
#最大化瀏覽器視窗
driver.maximize_window()
#print u"啟動瀏覽器成功..."
print "start browser done..."
#print u"訪問126郵箱登頁。。。"
print "access 126 mail login page..."
driver.get("http://mail.126.com")
#暫停5秒鐘,一遍郵箱登入頁面載入完成
time.sleep(5)
assert u"126網易免費郵--你的專業電子郵局" in driver.title

print "access 126 mail login page done"

wait=WebDriverWait(driver,30)

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"x-URS-iframe")))

username=driver.find_element_by_xpath("//input[@name=‘email‘]")

username.send_keys("xiaxiaoxu1987")

pwd=driver.find_element_by_xpath("//input[@name=‘password‘]")

pwd.send_keys("gloryroad")

pwd.send_keys(Keys.RETURN)

print "user login..."

time.sleep(5)
time1=time.time()
driver.switch_to.default_content()
print "total time:",time.time()-time1
assert u"網易郵箱" in driver.title
print "login done"
address_book_link = wait.until(lambda x: x.find_element_by_xpath("//div[text()=‘通訊錄‘]"))
address_book_link.click()

add_contact_button = wait.until(lambda x: x.find_element_by_xpath("//span[text()=‘建立連絡人‘]"))
add_contact_button.click()

contact_name = wait.until(lambda x: x.find_element_by_xpath("//a[@title=‘編輯詳細姓名‘]/preceding-sibling::div/input"))
contact_name.send_keys(u"徐鳳釵")
contact_email = wait.until(lambda x: x.find_element_by_xpath("//*[@id=‘iaddress_MAIL_wrap‘]//input"))
contact_email.send_keys("[email protected]")
contact_is_star = wait.until(lambda x: x.find_element_by_xpath("//span[text()=‘設為星標連絡人‘]/preceding-sibling::span/b"))
contact_is_star.click()
contact_mobile = wait.until(lambda x: x.find_element_by_xpath("//*[@id=‘iaddress_TEL_wrap‘]//dd//input"))
contact_mobile.send_keys(‘18141134488‘)
contact_other_info = wait.until(lambda x: x.find_element_by_xpath("//textarea"))
contact_other_info.send_keys(‘my wife‘)
contact_save_button = wait.until(lambda x: x.find_element_by_xpath("//span[.=‘確 定‘]"))
contact_save_button.click()

print u"進入首頁。。。"
time.sleep(3)
mainPage=wait.until(EC.visibility_of_element_located((By.XPATH,"//div[.=‘首頁‘]")))#也好用
#mainPage=wait.until(lambda x: x.find_element(by=‘xpath‘, value = "//div[.=‘首頁‘]"))#好用
#mainPage=driver.find_element_by_xpath("//div[.=‘首頁‘]")#好用
mainPage.click()
assert u"已發送" in driver.page_source
print u"進入首頁成功"
print "write message..."
writeMessage=wait.until(lambda x:x.find_element_by_xpath("//span[text()=‘寫 信‘]"))
writeMessage.click()
#收件者
receiver=wait.until(lambda x:x.find_element_by_xpath("//div[contains(@id,‘_mail_emailinput‘)]/input"))
receiver.send_keys("[email protected]")
#主題
theme=wait.until(lambda x:x.find_element_by_xpath("//div[@aria-label=‘郵件主題輸入框,請輸入郵件主題‘]/input"))
theme.send_keys(u"測試郵件")
#添加附件
attachment=wait.until(lambda x:x.find_element_by_xpath("//div[@title=‘點擊添加附件‘]/input[@size=‘1‘ and @type=‘file‘]"))
attachment.send_keys("d:\\test.txt")
#切入本文iframe
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@tabindex=1]"))

editBox=driver.find_element_by_xpath(‘/html/body‘)
editBox.send_keys(u"發給夏曉旭的一封信")
driver.switch_to.default_content()
#print u"寫信完成"
print "write message done"
driver.find_element_by_xpath("//header//span[text()=‘發送‘]").click()
#print u"開始發送郵件"
print "start to send email.."
time.sleep(3)
assert u"發送成功" in driver.page_source
#print u"郵件發送成功"
print "send emial done"
driver.quit()

結果:

C:\Python27\python.exe D:/test/hybrid_version2/TestScripts/TestScript.py
start browser...
start browser done...
access 126 mail login page...
access 126 mail login page done
user login...
total time: 0.00800013542175
login done
進入首頁。。。
進入首頁成功
write message...
write message done
start to send email..
send emial done

Process finished with exit code 0



python webdriver firefox 登入126郵箱,先新增連絡人...,然後進入首頁發送郵件,帶附件。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.