python+selenium实现QQ邮箱自动发送功能
作者:知否,知否 时间:2021-11-11 22:03:58
python实现从登录到自动发送qq邮件,供大家参考,具体内容如下
qq邮箱登录注意问题
账户密码登录框是写在一个iframe上的,所以我们要先进入iframe中才能定位到下面的两个输入框和登录按钮。从下图我们可以看到iframe=login_frame,我们可以是用driver.switch.to_frame(“login_frame”)进入iframe中。接下来的两个输入框和登录按钮都有id属性,我们只需要用driver.find_element_by_id(“xxx”)就可以定位到元素。
记得退出iframe,不然定位后面的所有元素都会失败(切记),退出iframe的话我们可以写driver.switch_to_default_content()来实现(个人发现to后面用“.”还是“_”符号,都可以将后面显示出来)。大家可以尝试下,把这句话省略,接着定位下面的元素。
主页面注意事项
主页面上左上角的“写信”按钮,你右击是不会显示查看元素的,第一种方法,我们首先在页面其他地方右击,接着选中查看元素,然后用查看器左边的那个鼠标按钮去点击“写信”按钮。就可以查看到。第二种方法,直接根据“写信”两个字去定位元素。我们可以这样driver.find_element_by_link_text(“写信”)
写信页面注意事项
写信页面都写在iframe里面,所以要进入iframe中,然后定位收件人的输入框,我们可以从下面的页面代码中看到class的值有很多地方都用到(不是唯一),所有我们相对路径的方法,driver.find_element_by_xpath("//*[@id=“toAreaCtrl”]//div[2]/input")。这样就可以解决了。
文下面的文本框注意四点,第一点,文本框在一个iframe中;第二点,文本框没有任何的属性值,所以我们可以使用HTML标签名定位,driver.find_element_by_tag_name(“body”);第三点文本框需要先点击(click),在编写(send_keys),如果没有先点击在编写,那么你send_keys(“xxxx”)内容会放在主题后面的文本框中。第四点,退出iframe,然后再进入iframe=“mainFrame”,因为退出的话就是所有的iframe都退出。
完整脚本
from selenium import webdriver
import unittest,time,traceback
from selenium.common.exceptions import TimeoutException,NoSuchElementException
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path='f:\\geckodriver')
def test_sendqqemail(self):
try:
url = 'https://mail.qq.com/'
self.driver.get(url)
self.driver.maximize_window()
self.driver.switch_to_frame("login_frame")
self.driver.find_element_by_id("u").send_keys("用户名")
self.driver.find_element_by_id("p").send_keys("密码")
self.driver.find_element_by_id("login_button").click()
time.sleep(5)
self.driver.switch_to.default_content() #退出iframe,如果没这句话后面的元素定位都会出错
self.driver.find_element_by_id('composebtn').click()
#写信模块都在iframe=mainFrame页面上
self.driver.switch_to_frame("mainFrame")
time.sleep(3)
self.driver.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys("收件人")
#定位主题
self.driver.find_element_by_xpath('//input[@id="subject"]').send_keys("主题")
#正文编写iframe中
self.driver.switch_to_frame(self.driver.find_element_by_class_name("qmEditorIfrmEditArea"))
#self.driver.find_element_by_xpath("/html/body").send_keys('你好,再见')
#定位正文
content=self.driver.find_element_by_tag_name("body")
#必须click(),不然会写到主题里面
content.click()
content.send_keys("正文内容")
time.sleep(3)
self.driver.switch_to.default_content()
self.driver.switch_to_frame("mainFrame")
#定位发送按钮
self.driver.find_element_by_xpath('//a[.="发送"]').click()
except TimeoutException:
print("显示等待页面元素超时")
except NoSuchElementException:
print("寻找页面元素不存在",traceback.print_exc())
except Exception:
print(traceback.print_exc())
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main()
来源:https://blog.csdn.net/weixin_43992799/article/details/86082389
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
分享Python 中的 7 种交叉验证方法
![](https://img.aspxhome.com/file/2023/1/81481_0s.png)
使用SQL Server2005扩展函数进行性能优化
![](https://img.aspxhome.com/file/UploadPic/20106/20106712295687s.jpg)
python远程登录代码
PHP程序员玩转Linux系列 使用supervisor实现守护进程
![](https://img.aspxhome.com/file/2023/4/65814_0s.jpg)
Python中表达式x += y和x = x+y 的区别详解
python try except 捕获所有异常的实例
Django 中使用流响应处理视频的方法
![](https://img.aspxhome.com/file/2023/7/65547_0s.png)
python爬虫之request模块深入讲解
![](https://img.aspxhome.com/file/2023/7/118397_0s.png)
Python利用逻辑回归分类实现模板
Python 实操显示数据图表并固定时间长度
![](https://img.aspxhome.com/file/2023/7/107557_0s.gif)
python中logging库的使用总结
python实现维吉尼亚算法
![](https://img.aspxhome.com/file/2023/8/59818_0s.jpg)
python 输入字符串生成所有有效的IP地址(LeetCode 93号题)
setInterval 和 setTimeout 会产生内存溢出
Python学习入门之区块链详解
![](https://img.aspxhome.com/file/2023/4/92204_0s.png)
如何利用python写GUI及生成.exe可执行文件
![](https://img.aspxhome.com/file/2023/4/59824_0s.png)
PHP中的traits实现代码复用使用实例
MSSQL数据库还原图解教程
![](https://img.aspxhome.com/file/UploadPic/20091/12/34_20071005121043_mq==-29s.jpg)