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

yipeiwu_com7年前Python基础

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

基于selenium,使用chrome浏览器,完成qq邮箱登陆并发送发邮件功能,暂时未封装。qq邮箱和126邮箱登陆还是有一些区别的。

#encoding=utf-8
from selenium import webdriver
import time
 
#启动chrome浏览器
driver = webdriver.Chrome()
#进入qq邮箱登陆首页
driver.get("https://mail.qq.com/")
time.sleep(1)
 
#窗口最大化
driver.maximize_window()
#切换到登陆frame(!!!!!!!!必须先切换!!!!!!!!)
driver.switch_to.frame('login_frame')
driver.find_element_by_xpath("//*[@id='switcher_plogin']").click()
time.sleep(3)
#########登陆
#输入用户名
username = driver.find_element_by_xpath("//*[@id='u']")
username.clear()
#将xxxxxxxxxx换成qq邮箱账户
username.send_keys('xxxxxxxxxx')
#输入密码:将1111111111替换为自己的邮箱密码
driver.find_element_by_id('p').send_keys('1111111111')
#点击登陆
driver.find_element_by_id('login_button').click()
time.sleep(10)
#断言登陆成功
assert '退出' in driver.page_source
 
#########写信
#单击写信按钮
driver.find_element_by_link_text("写信").click()
time.sleep(2)
#切换到mainFrame
driver.switch_to.frame('mainFrame')
time.sleep(2)
#输入收件人
driver.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys('123456789@qq.com')
#输入主题
driver.find_element_by_id('subject').send_keys('test')
#输入正文
o=driver.find_elements_by_class_name("qmEditorIfrmEditArea")
o[0].click() #!!!!!!!must click!!!!!!!
o[0].send_keys("abc")
 
#点击发送按钮
driver.find_element_by_xpath("//*[@id='toolbar']/div/a[1]").click()
time.sleep(3)
##driver.find_element_by_xpath('//a[@name="sendbtn" and @tabindex="9"]').click()
time.sleep(3)
#断言发送成功
assert u"再写一封" in driver.page_source
#关闭浏览器
driver.quit()

小编再为大家分享一段代码:python+selenium自动登录QQ邮箱发送邮件

#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
url =("https://mail.qq.com/")
name=("874**5483")
key = ("Zly1****612X")
out_name=("65750**8@qq.com")
#
#打开浏览器
driver.get(url) 
driver.implicitly_wait(10)

#最大化窗口
driver.maximize_window()

#切换iframe
driver.switch_to.frame("login_frame")



# #定位至账号密码登录
# driver.find_element_by_xpath('//*[@id="switcher_plogin"]').click()

# #账号,密码输入
# driver.find_element_by_xpath('//*[@id="u"]').send_keys(name)
# driver.find_element_by_xpath('//*[@id="p"]').send_keys(key)

# #点击登录
# driver.find_element_by_xpath('//*[@id="login_button"]').click()

#点击头像登录
driver.find_element_by_xpath('//*[@id="img_out_874335483"]').click()
time.sleep(5)

#点击写信
driver.find_element_by_xpath('//*[@id="composebtn"]').click()
time.sleep(3)

#切换iframe至写信
driver.switch_to.frame("mainFrame")
#driver.switch_to.frame(driver.find_element_by_id('mainFrame'))
time.sleep(3)

#添加收件人
driver.find_element_by_xpath('//*[@id="toAreaCtrl"]/div[2]/input').send_keys(out_name)

#添加主题
driver.find_element_by_xpath('//*[@id="subject"]').send_keys("TestCase1")

#退出当前编辑Iframe
driver.switch_to.default_content()


#切换Iframe至编辑正文
driver.switch_to.frame("mainFrame")
#Body_frame=driver.find_element_by_xpath('//iframe[@scrolling="auto"]')
Body_frame=driver.find_element_by_class_name("qmEditorIfrmEditArea")
driver.switch_to.frame(Body_frame)

#添加正文
driver.find_element_by_xpath('/html/body').send_keys("I LOVE Python")
time.sleep(3)

#退回大Frame再点击发送
driver.switch_to.parent_frame()
driver.find_element_by_xpath('//*[@id="toolbar"]/div/a[1]').click()

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

相关文章

利用Anaconda完美解决Python 2与python 3的共存问题

前言 现在Python3 被越来越多的开发者所接受,同时让人尴尬的是很多遗留的老系统依旧运行在 Python2 的环境中,因此有时你不得不同时在两个版本中进行开发,调试。 如何在系统中同...

使用Python对Access读写操作

学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件。 需要下载安装pywi...

python opencv minAreaRect 生成最小外接矩形的方法

python opencv minAreaRect 生成最小外接矩形的方法

使用python opencv返回点集cnt的最小外接矩形,所用函数为 cv2.minAreaRect(cnt) ,cnt是点集数组或向量(里面存放的是点的坐标),并且这个点集不定个数。...

Python判断文本中消息重复次数的方法

本文实例讲述了Python判断文本中消息重复次数的方法。分享给大家供大家参考,具体如下: #coding:gbk ''' Created on 2012-2-3 从文件中读取文本,并...

在Python中使用Neo4j数据库的教程

在Python中使用Neo4j数据库的教程

 一个快速的REST例子 首先来看些基本知识。如果没有服务API,Neo4j就不能支持其他语言。该接口提供一组基于JSON消息格式的RESTful Web服务和一个全面的发现机...