溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在python中使用selenium發(fā)送帶附件的郵件

發(fā)布時(shí)間:2021-04-17 16:07:24 來源:億速云 閱讀:280 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在python中使用selenium發(fā)送帶附件的郵件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

from selenium import webdriver
import unittest
import time

class VisitSogouByChrome(unittest.TestCase):

  def setUp(self):
    # 啟動(dòng)Chrome瀏覽器
    self.driver = webdriver.Chrome(executable_path = "e:\\chromedriver.exe")


  def test_sendEmail(self):
    # 訪問163郵箱的首頁(yè)
    self.driver.get("https://mail.163.com/")
    # 打印當(dāng)前網(wǎng)頁(yè)的網(wǎng)址
    self.driver.maximize_window()
    #點(diǎn)擊密碼登錄
    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')]")
    # 切換進(jìn)登錄框的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)

    #點(diǎn)擊“寫信”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('ff_gaofeng@163.com')

    #輸入郵件主題
    self.title = self.driver.find_element_by_xpath("//input[contains(@id,'subjectInput')]")
    self.title.send_keys('發(fā)給自己的一封郵件')

    #上傳文件
    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)

    # 切換進(jìn)入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']"))

    # 輸入郵件正文內(nèi)容
    self.body = self.driver.find_element_by_xpath("html/body")
    self.body.send_keys("實(shí)現(xiàn)寫郵件,上傳附件的功能自動(dòng)化用了。。。。。。。。")
    self.driver.switch_to.default_content()

    #點(diǎn)擊“發(fā)送”按鈕
    self.send_email = self.driver.find_element_by_xpath("//header//span[text()='發(fā)送']")
    self.send_email.click()



  def tearDown(self):
    # 退出IE瀏覽器
    self.driver.quit()

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

關(guān)于怎么在python中使用selenium發(fā)送帶附件的郵件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI