Code:
#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 "Start browser ..."
Print "Start browser ..."
#创建Firefox浏览器实例
Driver=webdriver. Firefox (executable_path= "C:\\geckodriver")
#最大化浏览器窗口
Driver.maximize_window ()
#print U "Start browser successful ..."
Print "Start browser done ..."
#print U "Access 126 Mailbox Login ... "
Print "Access 126 Mail Login page ..."
Driver.get ("http://mail.126.com")
#暂停5秒钟, once the mailbox login page is loaded
Time.sleep (5)
Assert u "126 netease free mail-your professional electronic post office" 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 "NetEase mailbox" in Driver.title
Print "Login Done"
Address_book_link = Wait.until (Lambda x:x.find_element_by_xpath ("//div[text () = ' Address Book ']")
Address_book_link.click ()
Add_contact_button = Wait.until (Lambda x:x.find_element_by_xpath ("//span[text () = ' new Contact ']")
Add_contact_button.click ()
Contact_Name = Wait.until (Lambda x:x.find_element_by_xpath ("//a[@title = ' edit verbose name ']/preceding-sibling::d iv/input"))
Contact_name.send_keys (U "describes Chai")
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 () = ' Set as Star contact ']/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[.= ' OK ']")
Contact_save_button.click ()
Print U "go to homepage ... "
Time.sleep (3)
Mainpage=wait.until (ec.visibility_of_element_located ((By.xpath, "//div[.= ' home ']")) #也好用
#mainPage =wait.until (Lambda x:x.find_element (by= ' XPath ', value = "//div[.= ' home Page ')") #好用
#mainPage =driver.find_element_by_xpath ("//div[.= ' Home") #好用
Mainpage.click ()
Assert u "sent" in Driver.page_source
Print U "go to home success"
Print "Write Message ..."
Writemessage=wait.until (Lambda X:x.find_element_by_xpath ("//span[text () = ' letter ')")
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=" message subject input box, please enter the message subject ']/input '))
Theme.send_keys (U "Test Mail")
#添加附件
Attachment=wait.until (Lambda x:x.find_element_by_xpath ("//div[@title = ' Click Add Attachment ']/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 "send a letter to Xia Xiaoxu")
Driver.switch_to.default_content ()
#print U "write Letter Complete"
Print "Write Message Done"
Driver.find_element_by_xpath ("//header//span[text () = ' send ']"). Click ()
#print u "Start sending mail"
Print "Start to send email ..."
Time.sleep (3)
Assert u "send success" in Driver.page_source
#print u "Mail sent successfully"
Print "Send emial done"
Driver.quit ()
Results:
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
Go to homepage ...
Go to homepage success
Write Message ...
Write Message Done
Start to send email:
Send emial Done
Process finished with exit code 0
Python webdriver Firefox login 126 mailbox, add contacts First, and then go to the home page to send mail, with attachments.