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创建普通菜单示例【基于win32ui模块】

Python创建普通菜单示例【基于win32ui模块】

本文实例讲述了Python创建普通菜单的方法。分享给大家供大家参考,具体如下: 一、代码 # -*- coding:utf-8 -*- #! python3 import win32...

python去除扩展名的实例讲解

获取不带扩展名的文件的名称: import os printos.path.splitext("path_to_file")[0] from os.path import ba...

Django xadmin开启搜索功能的实现

应用目录下adminx.py class EmailVerifyRecordAdmin(object): search_fields = ['code','email','sen...

分享6个隐藏的python功能

小编在以前给大家介绍过python一些很少用到的功能,这次我们给大家分享了6个隐藏的python功能,学习下。 在python的设计哲学中,有这么一条内容:“Simple is bett...

Python字符串处理的8招秘籍(小结)

Python的字符串处理,在爬虫的数据解析、大数据的文本清洗,以及普通文件处理等方面应用非常广泛,而且Python对字符串的处理内置了很多高效的函数,功能非常强大、使用非常方便。今天我就...