python3? ?selenium實(shí)現(xiàn)發(fā)送qq郵箱自動(dòng)化在岂,我看了下網(wǎng)頁發(fā)布的代碼比較復(fù)雜幅恋,說的不夠詳細(xì),不適合初學(xué)者厢岂。歡迎加入我群,溝通交流學(xué)習(xí)
以下是代碼:
from selenium import webdriver? ? ?#導(dǎo)入webdriver包
from time import sleep? ? ? ? ? ? ? ? ?#導(dǎo)入sleep包
from selenium.webdriver.support import expected_conditionsas EC? ? ?#導(dǎo)入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()
#定位用戶輸入界面阳距,輸入賬號(hào)
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)
#點(diǎn)擊登錄
dr.find_element_by_id('login_button').click()
sleep(3)
#退出iframe
dr.switch_to.default_content()
#驗(yàn)證郵箱是否登錄成功
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("http://*[@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()
#再進(jìn)入正文的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('http://www.reibang.com/writer#/notebooks/28485264/notes/32223172 ')
#退回到大的frame框架中再點(diǎn)擊發(fā)送郵件
dr.switch_to.parent_frame()
sleep(2)
dr.find_element_by_name('sendbtn').click()? ? ? #點(diǎn)擊發(fā)送按鈕