python+selenium实现发送一封带附件的邮件

163邮件登录首页

登录成功断言是否有退出按钮

点击退出退出登录

from selenium import webdriver
import unittest
import time

class VisitSogouByChrome(unittest.TestCase):

    def setUp(self):
        # 启动Chrome浏览器
        self.driver = webdriver.Chrome(executable_path = "e:\\chromedriver.exe")


    def test_sendEmail(self):
        # 访问163邮箱的首页
        self.driver.get("https://mail.163.com/")
        # 打印当前网页的网址
        self.driver.maximize_window()
        #点击密码登录
        self.pwd_link = self.driver.find_element_by_xpath("//a[text()=‘密码登录‘]")
        self.pwd_link.click()
        #找到登录框的iframe
        login_input_iframe = self.driver.find_element_by_xpath("//iframe[contains(@id,‘x-URS-iframe‘)]")
        # 切换进登录框的iframe
        self.driver.switch_to.frame(login_input_iframe)

        self.user_name = self.driver.find_element_by_xpath("//input[@name=‘email‘]")
        self.pass_wd = self.driver.find_element_by_xpath("//input[@name = ‘password‘]")
        self.login_button =self.driver.find_element_by_xpath("//a[@id =‘dologin‘]")

        #清空用户名
        self.user_name.clear()
        self.user_name.send_keys("ff_gaofeng")
        self.pass_wd.send_keys("XXX")
        self.login_button.click()
        time.sleep(5)

        #点击“写信”button
        self.writer_button = self.driver.find_element_by_xpath("//span[text()=‘写 信‘]")
        self.writer_button.click()
        time.sleep(2)

        #输入收件人的邮箱
        self.addressee = self.driver.find_element_by_xpath("//input[contains(@aria-label,‘收件人地址输入框‘)]")
        self.addressee.send_keys(‘‘)

        #输入邮件主题
        self.title = self.driver.find_element_by_xpath("//input[contains(@id,‘subjectInput‘)]")
        self.title.send_keys(‘发给自己的一封邮件‘)

        #上传文件
        self.uppload_file_link = self.driver.find_element_by_xpath("//input[@type = ‘file‘]")
        #self.uppload_file_link = self.driver.find_element_by_xpath("//a[text()=‘添加附件‘]")
        self.uppload_file_link.send_keys(r"D:\1.py")
        time.sleep(5)

        # 切换进入boby的iframe
        #boby_iframe = self.driver.find_element_by_xpath("//iframe[@class=‘APP-editor-iframe‘]")
        #self.driver.switch_to.frame(boby_iframe)
        self.driver.switch_to.frame(self.driver.find_element_by_xpath("//iframe[@class=‘APP-editor-iframe‘]"))

        # 输入邮件正文内容
        self.body = self.driver.find_element_by_xpath("html/body")
        self.body.send_keys("实现写邮件,上传附件的功能自动化用了。。。。。。。。")
        self.driver.switch_to.default_content()

        #点击“发送”按钮
        self.send_email = self.driver.find_element_by_xpath("//header//span[text()=‘发送‘]")
        self.send_email.click()



    def tearDown(self):
        # 退出IE浏览器
        self.driver.quit()

if __name__ == ‘__main__‘:
    unittest.main()

相关推荐