python3 selenium实现发送qq邮箱自动化,我看了下网页发布的代码比较复杂,说的不够详细,不适合初学者。欢迎加入我群,沟通交流学习
以下是代码:
from selenium import webdriver #导入webdriver包
from time import sleep #导入sleep包
from selenium.webdriver.support import expected_conditionsas EC #导入EC包
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.byimport By
dr=webdriver.Chrome()
#登录qq 邮箱
dr.get('https://mail.qq.com/')
#窗口最大化
dr.maximize_window()
#切换到iframe
name=dr.find_element_by_xpath('//*[@name="login_frame"]')
dr.switch_to.frame(name)
element1=WebDriverWait(dr,3,0.5).until(EC.presence_of_element_located((By.ID,'switcher_plogin')))
element1.click()
#定位用户输入界面,输入账号
element=WebDriverWait(dr,3,0.5).until(EC.presence_of_element_located((By.ID,'u')))
element.send_keys('xxxxx@qq.com')
#定位用密码输入界面,输入密码
dr.find_element_by_id('p').send_keys('password')
sleep(1)
#点击登录
dr.find_element_by_id('login_button').click()
sleep(3)
#退出iframe
dr.switch_to.default_content()
#验证邮箱是否登录成功
text=dr.find_element_by_id('useraddr').text
if text=='xxxxxx@qq.com':
print('登录成功:{}'.format(text))
else:
print('登录失败')
dr.find_element_by_id('composebtn').click()
sleep(5)
dr.switch_to.frame('mainFrame')
#在收件箱输入邮箱
#输入收件人
dr.find_element_by_xpath("//*[@id='toAreaCtrl']/div[2]/input").send_keys('xxxx@qq.com')
sleep(2)
#输入主题
dr.find_element_by_id('subject').send_keys('xxxxx')
sleep(2)
#退出iframe
dr.switch_to.default_content()
#再进入正文的iframe,先切入到大的frame,再切入到嵌套的frame中,分两步走
dr.switch_to.frame('mainFrame')
frame_boby=dr.find_element_by_xpath('//iframe[@scrolling="auto"]')
dr.switch_to.frame(frame_boby)
sleep(2)
dr.find_element_by_xpath('/html/body').send_keys('https://www.jianshu.com/writer#/notebooks/28485264/notes/32223172 ')
#退回到大的frame框架中再点击发送邮件
dr.switch_to.parent_frame()
sleep(2)
dr.find_element_by_name('sendbtn').click() #点击发送按钮
网友评论