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设计】。

相关文章

Python3安装psycopy2以及遇到问题解决方法

Python3安装psycopy2以及遇到问题解决方法

事先在网上搜索了一大圈,头都大了,看到那么多文章写道在python里安装psycopg2的各种坑和各种麻烦,各种不成功。搜索了一下午,索性外出放松。晚饭后,又继续上psycopg2官网(...

使用python实现mqtt的发布和订阅

需要安装的python库 使用python编写程序进行测试MQTT的发布和订阅功能。首先要安装:pip install paho-mqtt 测试发布(pub) 我的MQTT部署在阿里云的...

Python缩进和冒号详解

Python缩进和冒号详解

对于Python而言代码缩进是一种语法,Python没有像其他语言一样采用{}或者begin...end分隔代码块,而是采用代码缩进和冒号来区分代码之间的层次。 缩进的空白数量是可变的,...

django 使用 request 获取浏览器发送的参数示例代码

获取数据(四种方式) 1. url: 需要正则去匹配     url(r'^index/(num)/$',view.index)   &...

Python的消息队列包SnakeMQ使用初探

一、关于snakemq的官方介绍 SnakeMQ的GitHub项目页:https://github.com/dsiroky/snakemq 1.纯python实现,跨平台 2.自动重连接...