python selenium实现发送带附件的邮件代码实例

yipeiwu_com6年前Python基础

这篇文章主要介绍了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('ff_gaofeng@163.com')

    #输入邮件主题
    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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python使用pygame模块编写俄罗斯方块游戏的代码实例

Python使用pygame模块编写俄罗斯方块游戏的代码实例

文章先介绍了关于俄罗斯方块游戏的几个术语。 边框——由10*20个空格组成,方块就落在这里面。 盒子——组成方块的其中小方块,是组成方块的基本单元。 方块——从边框顶掉下的...

使用Python进行目录的对比方法

如果进行单个文件的比较,可以使用difflib模块。虽然filecmp模块也能够进行单个文件的对比,但是前者能够提供观感更好的报告。如果我们只是想看一下两个目录中的某个文件是否一致而不关...

简单了解OpenCV是个什么东西

OpenCV于1999年由Intel建立,如今由Willow Garage提供支持。OpenCV是一个基于BSD许可[1] (开源)发行的跨平台计算机视觉库,可以运行在Linux、Win...

python3.6下Numpy库下载与安装图文教程

python3.6下Numpy库下载与安装图文教程

今天在做Plotly的散点图时,需要Numpy 这个库的使用。 没有安装Numpy这个库的时候,报错一般是下图这样:ModuleNotFoundError: No module name...

python中seaborn包常用图形使用详解

python中seaborn包常用图形使用详解

seaborn包是对matplotlib的增强版,需要安装matplotlib后才能使用。 所有图形都用plt.show()来显示出来,也可以使用下面的创建画布 fig,ax=plt...