【基础】练习册46-Python3_selenium_发送邮件
代码如下:
#coding=utf-8
from selenium import webdriver
import unittest,time
from public import login #导入login文件
class TestSendEmail(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(10)
self.base_url = "https://mail.163.com/"
self.verificationErrors = []
#只输入收件人发送
def test_sendmail1(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,'alyna_cai','QQqq1122369')
time.sleep(2)
#写信
driver.find_element_by_xpath('/html/body/div[1]/nav/div[1]/ul/li[2]/span[2]').click()
driver.find_element_by_xpath("//*[@class='bz0']/div[2]/div/input").send_keys('1755897460@qq.com')#输入收件地址
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/header/div/div[1]/div/span[1]/b').click()#发送
driver.switch_to_active_element().click()#切换至激活状态控件
time.sleep(2)
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text.startswith("发送成功"),True) #验证部分文本框
time.sleep(2)
login.logout(self)
#输入收件人和主题发送
def test_sendmail2(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,'alyna_cai','QQqq1122369')
time.sleep(2)
#写信
driver.find_element_by_xpath('/html/body/div[1]/nav/div[1]/ul/li[2]/span[2]').click()
driver.find_element_by_xpath("//*[@class='bz0']/div[2]/div/input").send_keys('1755897460@qq.com')#输入收件地址
driver.find_element_by_xpath("//*[@class='kZ0 fu0']/div/div/div/input").send_keys('假如生活欺骗了你') #输入主题
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/header/div/div[1]/div/span[1]/b').click()#发送
time.sleep(2)
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text.startswith("发送成功"),True) #验证部分文本框
time.sleep(2)
login.logout(self)
#输入收件人、主题和内容
def test_sendmail3(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,'alyna_cai','QQqq1122369')
time.sleep(2)
#写信
driver.find_element_by_xpath('/html/body/div[1]/nav/div[1]/ul/li[2]/span[2]').click()
driver.find_element_by_xpath("//*[@class='bz0']/div[2]/div/input").send_keys('1755897460@qq.com')#输入收件地址
driver.find_element_by_xpath("//*[@class='kZ0 fu0']/div/div/div/input").send_keys('假如生活欺骗了你') #输入主题
class_name = driver.find_element_by_class_name("APP-editor-iframe" )#iframe定位
driver.switch_to_frame(class_name)
driver.find_element_by_tag_name('body').send_keys('知世故,不世故') #编写正文
driver.switch_to.default_content()#退出iframe
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/header/div/div[1]/div/span[1]/b').click()#发送
time.sleep(2)
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text.startswith("发送成功"),True) #验证部分文本框
time.sleep(2)
login.logout(self)
#输入收件人、主题和附件发送
def test_sendmail4(self):
driver = self.driver
driver.get(self.base_url)
#登录
login.login(self,'alyna_cai','QQqq1122369')
time.sleep(2)
#写信
driver.find_element_by_xpath('/html/body/div[1]/nav/div[1]/ul/li[2]/span[2]').click()
driver.find_element_by_xpath("//*[@class='bz0']/div[2]/div/input").send_keys('1755897460@qq.com')#输入收件地址
driver.find_element_by_xpath("//*[@class='kZ0 fu0']/div/div/div/input").send_keys('给豆豆-txt') #输入主题
time.sleep(2)
driver.find_element_by_class_name('O0').send_keys('D:\\text.txt') #上传文件
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/header/div/div[1]/div/span[1]/b').click()#发送
time.sleep(2)
#断言发送结果
text = driver.find_element_by_class_name('tK1').text
self.assertEqual(text.startswith("发送成功"),True) #验证部分文本框
time.sleep(2)
login.logout(self)
def tearDown(self):
self.driver.quit()
self.assertEqual([],self.verificationErrors)
if __name__ == "__main__":
unittest.main()