随笔:python+selenium+unittest用qq邮箱上传文件并发送邮件
import unittest
from selenium import webdriver
from time import sleep
class qqemail(unittest.TestCase):
def setUp(self):
print("调用qq邮箱发送邮件")
def testEmail(self):
self.option = webdriver.ChromeOptions()
self.option.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])
self.prefs = {}
self.prefs["credentials_enable_service"] = False
self.prefs["profile.password_manager_enabled"] = False
self.option.add_experimental_option("prefs", self.prefs)
self.driver = webdriver.Chrome(options=self.option)
self.driver.maximize_window()
sleep(5)
self.driver.get("https://mail.qq.com")
sleep(5)
self.driver.switch_to.frame('login_frame')
sleep(2)
self.driver.find_element_by_css_selector("a#switcher_plogin").click()
sleep(2)
self.driver.find_element_by_xpath("//input[@id='u']").send_keys("username")
self.driver.find_element_by_css_selector("input#p").send_keys("password")
self.driver.find_element_by_css_selector("input.btn").click()
sleep(5)
self.driver.find_element_by_xpath("//a[text()='写信']").click()
sleep(5)
self.driver.switch_to.frame('mainFrame')
print(self.driver.find_element_by_xpath("//a[@id='to_btn']").text)
self.driver.find_element_by_css_selector("div#toAreaCtrl>div>input.js_input").send_keys("收件人邮箱")
sleep(2)
self.driver.find_element_by_xpath("//div[@class='div_txt']//input[@id='subject']").send_keys("测试邮件发送")
self.driver.find_element_by_xpath("//div[@class='input_title']//input[@type='file']").send_keys(r'G:pythonjbcloudAItest estrunner est测试报告1.html')
sleep(10)
self.driver.find_element_by_css_selector("a[name='sendbtn']").click()
sleep(5)
t1 = self.driver.find_element_by_css_selector("b#sendinfomsg").text
print(t1)
sleep(1)
self.assertEqual("您的邮件已发送",t1)
sleep(2)
def tearDown(self):
print("邮件发送完成")
self.driver.quit()
if __name__ == '__main__':
unittest.main()