代码:#encoding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport 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()-time1assert u"网易邮箱" in driver.titleprint "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_sourceprint 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")#切入正文iframedriver.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
原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9343935.html
时间: 2024-08-28 18:11:13