python3+selenium实现126邮箱登陆并发送邮件功能

yipeiwu_com5年前Python基础

本文实例为大家分享了python3实现126邮箱登陆并发送邮件的具体代码,供大家参考,具体内容如下

基于selenium,使用chrome浏览器,完成126邮箱登陆并发送发邮件功能,暂时未封装。

from selenium import webdriver
# 导入显示等待类
from selenium.webdriver.support.ui import WebDriverWait
# 导入期望场景类
from selenium.webdriver.support import expected_conditions as EC
# 导入By类
from selenium.webdriver.common.by import By
import time
 
#浏览器驱动放在了c:\\Python36\\Scripts目录下,无需指定参数
driver= webdriver.Chrome()
driver.get("https://mail.126.com/")
time.sleep(3)
####登陆
driver.switch_to.frame("x-URS-iframe")
user_name = driver.find_element_by_xpath('//*[@name="email"]')
#将xxxxxxx替换为自己的用户名
user_name.send_keys('xxxxxxx')
pass_word = driver.find_element_by_xpath('//*[@name="password"]')
#将11111111111替换为自己的密码
pass_word.send_keys('11111111111')
button = driver.find_element_by_id("dologin")
button.click()
driver.switch_to.default_content()
time.sleep(3)
 
####写邮件
wait = WebDriverWait(driver,10,0.2)
##wait.until(EC.visibility_of_element_located((By.XPATH,"//span[text()='发送']")))
wait.until(EC.visibility_of_element_located((By.XPATH,"//a[contains(text(),'退出')]")))
driver.find_element_by_xpath('//span[text()="写 信"]').click()
driver.find_element_by_xpath('//input[@tabindex="1" and @role="combobox"]').\
                          send_keys("1234h@qq.com")
driver.find_element_by_xpath('//input[@tabindex="1" and @class="nui-ipt-input"]').\
                          send_keys("测试邮件")
driver.find_element_by_xpath('//input[@type="file"]').send_keys("f:\\b.txt")
time.sleep(5)
 
wait.until(EC.visibility_of_element_located((By.XPATH,"//span[text()='上传完成']")))
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@tabindex=1]'))
driver.execute_script("document.getElementsByTagName('body')[0].innerHTML='<b>邮件的正文内容<b>;'")
driver.switch_to.default_content()
 
##发送
driver.find_element_by_xpath('//span[text()="发送"]').click()
time.sleep(5)
assert '发送成功' in driver.page_source
logout_link=driver.find_element_by_xpath("//a[text()='退出']")
time.sleep(3)
assert u"登录" in driver.page_source
 
driver.quit()

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

相关文章

matplotlib绘制符合论文要求的图片实例(必看篇)

matplotlib绘制符合论文要求的图片实例(必看篇)

最近需要将实验数据画图出来,由于使用python进行实验,自然使用到了matplotlib来作图。 下面的代码可以作为画图的模板代码,代码中有详细注释,可根据需要进行更改。 # -*...

在Python的struct模块中进行数据格式转换的方法

在Python的struct模块中进行数据格式转换的方法

Python是一门非常简洁的语言,对于数据类型的表示,不像其他语言预定义了许多类型(如:在C#中,光整型就定义了8种),它只定义了六种基本类型:字符串,整数,浮点数,元组,列表,字典。通...

Python字符串的常见操作实例小结

本文实例讲述了Python字符串的常见操作。分享给大家供大家参考,具体如下: 如果我们想要查看以下功能:help(mystr .find) 1.find 例: mystr="hell...

Python pip替换为阿里源的方法步骤

Python pip替换为阿里源的方法步骤

背景 由于 python 自带的源下载速度非常慢,特别是安装一些库的时候,甚至有时会失败。 pip国内的一些镜像   阿里云 http://mirror...

解决python nohup linux 后台运行输出的问题

遇到问题 nohup python flush.py & 这样运行,生成了nohup.out文件,但是内容始终是空的,试了半天也不行。浪费了不少时间。 原因 python的输出又缓...