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 关于supper 的 用法和原理

一、前言 Python 面向对象中有继承这个概念,初学时感觉很牛逼,里面也有个super类,经常见到,最近做一些题才算是理解了。特地记录分享给后来研究的小伙伴,毕竟现在小学生都开始学了(...

PyQt5固定窗口大小的方法

PyQt5固定窗口大小的方法

直接以数值固定大小 根据屏幕大小固定大小 禁止最大化按钮 MainWindow.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint...

Tensorflow的可视化工具Tensorboard的初步使用详解

Tensorflow的可视化工具Tensorboard的初步使用详解

当使用Tensorflow训练大量深层的神经网络时,我们希望去跟踪神经网络的整个训练过程中的信息,比如迭代的过程中每一层参数是如何变化与分布的,比如每次循环参数更新后模型在测试集与训练集...

matplotlib subplots 设置总图的标题方法

如下所示: matplotlib subplots 设置总图的标题 : fig.suptitle(dname,fontsize=16,x=0.53,y=1.05,) 以上这篇matplo...

Python调用graphviz绘制结构化图形网络示例

Python调用graphviz绘制结构化图形网络示例

首先要下载:Graphviz - Graph Visualization Software 安装完成后将安装目录的bin 路径加到系统路径中,有时候需要重启电脑。 然后: pip...