美文网首页
selenium-163邮箱自动发送一封包含主题、附件的邮件.m

selenium-163邮箱自动发送一封包含主题、附件的邮件.m

作者: 回忆的时间沙漏 | 来源:发表于2019-06-03 14:25 被阅读0次
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Created by iFantastic on 2019-05-08
    
    from selenium import webdriver
    import time
    
    
    driver_path = "{chromedriver存放路径}"
    base_url = "https://mail.163.com/"
    
    # chrome去除浏览器正在收到自动测试软件控制
    option = webdriver.ChromeOptions()
    option.add_argument("disable-infobars")
    
    driver = webdriver.Chrome(executable_path=driver_path, options=option)
    driver.get(base_url)
    driver.maximize_window()
    driver.save_screenshot("{登录页面截图存放路径}")
    
    
    # 定位选择"密码登录"
    ele = driver.find_element_by_link_text("密码登录")
    print(ele)
    ele.click()
    
    
    # 定位iframe框架
    iframe = driver.find_element_by_xpath("//div[@id='loginDiv']/iframe")
    
    # 因为switch_to_frame(),这句话被划上了删除线,原因是这个方法已经out了,之后很有可能会不支持,建议的写法是switch_to.frame()
    # 进入iframe框架
    driver.switch_to.frame(iframe)
    # driver.switch_to.default_content()
    
    # 定位用户名输入框并输入用户名
    
    time.sleep(2)
    element_user = driver.find_element_by_xpath("//input[@name='email']")
    element_user.clear()
    element_user.send_keys("{发件人邮箱地址}")
    
    # 定位密码输入框并输入密码
    element_passwd = driver.find_element_by_name("password")
    element_passwd.clear()
    element_passwd.send_keys("{发件人邮箱密码}")
    
    # 定位登录按钮
    login_button = driver.find_element_by_xpath("//*[@id='dologin']")
    login_button.click()
    # 退出iframe
    driver.switch_to.default_content()
    
    time.sleep(3)
    driver.save_screenshot("{登录页面截图存放路径}")
    
    # 定位写信按钮
    driver.find_element_by_xpath('//span[text()="写 信"]').click()
    
    # 定位收件人输入框
    driver.find_element_by_xpath('//input[@class="nui-editableAddr-ipt"]')
    driver.find_element_by_xpath('//input[@class="nui-editableAddr-ipt"]').send_keys("{收件人邮箱地址}")
    
    time.sleep(1)
    
    # 定位主题输入框
    driver.find_element_by_css_selector("[maxlength='256']")
    driver.find_element_by_css_selector("[maxlength='256']").send_keys("selenium auto test测试邮件")
    
    # 定位"上传附件"
    time.sleep(4)
    driver.find_element_by_xpath("//input[@type='file' and @class='O0']").send_keys("{附件存放地址}")
    time.sleep(4)
    
    
    # 定位邮件正文
    driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@class='APP-editor-iframe']"))
    driver.find_element_by_class_name("nui-scroll").clear()
    driver.find_element_by_class_name("nui-scroll").send_keys("Python3 + selenium自动化测试学习")
    time.sleep(1)
    driver.save_screenshot("{邮件页面截图存放路径}")
    driver.switch_to.default_content()
    time.sleep(2)
    
    # 定位"发送"按钮
    driver.find_element_by_xpath("//span[text()='发送']").click()
    
    time.sleep(3)
    driver.close()
    
    

    相关文章

      网友评论

          本文标题:selenium-163邮箱自动发送一封包含主题、附件的邮件.m

          本文链接:https://www.haomeiwen.com/subject/epscxctx.html