zoukankan      html  css  js  c++  java
  • python webdriver 登录163邮箱发邮件加附件, 外加数据和程序分离,配置文件的方式

    配置文件:UiObjectMapSendMap.ini用来存放配置信息

    GetOptionSendMail.py 用来读取配信息

    #encoding=utf-8
    from selenium.webdriver.support.ui import WebDriverWait
    import ConfigParser
    import os
    from selenium import webdriver

    class GetOption(object):
        def __init__(self):
            # 获取存放页面元素定位表达方式及定位表达式的配置文件所在绝对路径
            # os.path.abspath(__file__)表示获取当前文件所在路径目录
            self.uiObjMapPath = os.path.dirname(os.path.abspath(__file__))
            + "\UiObjectMapSendMail.ini"
            #print self.uiObjMapPath

        def getOption(self,sectionName, optionName):
            try:
                # 创建一个读取配置文件的实例
                cf = ConfigParser.ConfigParser()
                # 将配置文件内容加载到内存
                cf.read(self.uiObjMapPath)
                # 根据section和option获取配置文件中的配置信息
                section = cf.get(sectionName, optionName)
                #print "section:",section

            except Exception, e:
                raise e
            else:
                # 当页面元素被找到后,将该页面元素对象返回给调用者
                return section

    if __name__ == '__main__':
        getoption=GetOption()
        print getoption.getOption("mailaccount","username")

    test.py脚本文件,用webdriver进入邮箱发邮件的脚本

    #encoding=utf-8

    import unittest
    import time
    from GetOptionSendMail import GetOption
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException, NoSuchElementException
    import traceback

     
    class Visit163ByFirefox(unittest.TestCase):

        def setUp(self):
            #启动IE浏览器
            self.obj=GetOption()
            self.driver=webdriver.Firefox(executable_path='d:\geckodriver')
            #self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")


        def test_HandleIFrame(self):
            url = "https://mail.163.com/"
            # 访问自动以测试网页
            self.driver.get(url)
            try:
                username=self.obj.getOption("mailaccount","username")
                print "username:",username
                password=self.obj.getOption("mailaccount","password")
                print "password:",password
                attachname=self.obj.getOption("attachment","directory")
                print "attachname:",attachname
                mailtoaddress=self.obj.getOption("mailto","address")
                print "mailtoaddress:",mailtoaddress
                #显示等待
                wait=WebDriverWait(self.driver,15,0.2)
                #切换frame
                self.driver.switch_to.frame(self.driver.find_element_by_xpath("//*[@id='x-URS-iframe']"))
                #self.driver.switch_to.frame("x-URS-iframe")
                #显示等待获取用户名输入框元素
                name=wait.until(lambda x:x.find_element_by_xpath("//*[@class='j-inputtext dlemail']"))
                name.send_keys(username)
                #显示等待获取密码输入框元素
                passwd=wait.until(lambda x:x.find_element_by_xpath("//*[@class='j-inputtext dlpwd']"))
                passwd.send_keys(password)
                #登录
                login=wait.until(lambda x:x.find_element_by_id("dologin"))
                login.click()
                #切回到默认framae
                self.driver.switch_to.default_content()
                #获取写信按钮元素
                iwrite=wait.until(lambda x:x.find_element_by_xpath("//li[@id='_mail_component_74_74']/*[@class='oz0']"))
                iwrite.click()
                #获取收件人输入框元素
                receiver=wait.until(lambda x:x.find_element_by_xpath("//input[@class='nui-editableAddr-ipt'and @role='combobox']"))
                #receiver.click()
                receiver.send_keys(mailtoaddress)
                #获取主题输入框元素
                theme=wait.until(lambda x:x.find_element_by_xpath("//input[@class='nui-ipt-input' and @type='text' and @maxlength='256']"))
                #theme.click()
                theme.send_keys(u"这是夏晓旭的第一个自动化发邮件脚本!")
                #发送附件,找添加附件的元素,直接send_keys("目录")就可以,不用点击操作
                attach=wait.until(lambda x:x.find_element_by_xpath("//div[@class='by0']//input[@class='O0']"))
                attach.send_keys(attachname)
                #切到写信内容部分的frame
                self.driver.switch_to.frame(self.driver.find_element_by_xpath("//*[@class='APP-editor-iframe']"))
                #获取写信区域的元素
                editBox = wait.until(lambda x:x.find_element_by_xpath("/html/body"))
                editBox.click()
                editBox.send_keys(u'这是夏晓旭的第一个自动化发邮件脚本!')
                #切回到默认frame
                self.driver.switch_to.default_content()
                #获取发送按钮元素
                send=wait.until(lambda x:x.find_element_by_xpath("//*[@class='jp0']//*[@role='button']//*[.='发送']"))
                send.click()

            except TimeoutException, e:
                # 捕获TimeoutException异常
                print traceback.print_exc()
            except NoSuchElementException, e:
                # 捕获NoSuchElementException异常
                print traceback.print_exc()
            except Exception, e:
                # 捕获其他异常
                print traceback.print_exc()
     

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

    执行过程:找到各个元素,输入字符,添加附件

    发送成功

    总结一下:

    添加附件时,不是通过点击按钮,选择文件,而是找到添加附件按钮的元素,直接用send_keys("附件目录")这样来传就可以

    读取配置文件的目的是实现程序和数据的分离,作用有两个:

    一是方便维护所取的配置项,比如我可以把用户名和密码、邮箱地址在配置文件里随便改,程序不用动,直接去取你所配置的选项就可以了

    二是可以给不懂代码逻辑的人员进行操作,不必要研究代码是怎么实现的,实现了程序和数据的隔离,方便使用

  • 相关阅读:
    Levmar 配置
    函数前加static与不加static的区别
    关于形如--error LNK2005: xxx 已经在 msvcrtd.lib ( MSVCR90D.dll ) 中定义--的问题分析解决
    Latex 算法Algorithm
    matlab 曲线拟合
    生成eps图形
    libSVM在matlab下的使用安装
    spring 事务管理笔记
    集合框架
    小程序 滑动弹窗阻止主页面滑动
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9216363.html
Copyright © 2011-2022 走看看