from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time, traceback
driver = None
wait = None
#登錄函數(shù)
def login_126():
global driver
global wait
url = "http://www.126.com"
driver = webdriver.Chrome(executable_path="d:\\chromedriver")
driver.get(url)
# 定義顯式等待對象
wait = WebDriverWait(driver, 10, 0.2)
try:
# 切入登錄框所在的iframe
wait.until(EC.frame_to_be_available_and_switch_to_it(( \
By.XPATH, '//iframe[contains(@id,"x-URS-iframe")]')))
time.sleep(3)
# driver.switch_to.frame(driver.find_element_by_xpath(\
# '//iframe[contains(@id,"x-URS-iframe")]'))
# 顯式等待用戶名輸入框可見且獲取輸入框元素對象
username = wait.until(lambda x: x.find_element_by_xpath( \
'//input[@placeholder="郵箱帳號或手機(jī)號"]'))
username.send_keys("xxxx")
# 直接獲取密碼輸入框
passwd = driver.find_element_by_xpath('//input[@name="password"]')
passwd.send_keys("xxxxx")
# 獲取登錄按鈕并點擊
driver.find_element(By.ID, "dologin").click()
#且出到正文
driver.switch_to.default_content()
# 顯式等待登錄成功后“退出”按鈕出現(xiàn)在頁面上
if wait.until(EC.visibility_of_element_located(('xpath', '//a[.="退出"]'))):
print("登錄成功!")
else:
print("登錄失??!")
except NoSuchElementException as e:
print(traceback.print_exc())
except TimeoutException as e:
print(traceback.print_exc())
except Exception as e:
print(traceback.print_exc())
#退出驅(qū)動并關(guān)閉所有瀏覽器窗口
def close_driver():
global driver
driver.quit()
def send_mail():
global driver
global wait
try:
#顯式等待寫信按鈕可點擊并獲取寫信按鈕元素對象
write_button = wait.until(EC.element_to_be_clickable(('xpath','//span[.="寫 信"]')))
write_button.click()
#顯式等待收件人輸入框可見
receiver = wait.until(lambda x : x.find_element_by_xpath(\
'//input[@class="nui-editableAddr-ipt"]'))
#receiver = wait.until(EC.visibility_of_element_located(('xpath','//span[.="寫 信"]')))
receiver.send_keys("hhq8286@126.com")
#獲取主題輸入框頁面元素對象
subject = driver.find_element_by_xpath('//input[contains(@id,"subjectInput")]')
subject.send_keys("2019你要怎么做?")
#獲取添加附件元素對象(用send_keys()需要定位到input元素)
attachment = driver.find_element_by_xpath('//input[@class="O0"]')
attachment.send_keys("e:\\python\\111.txt")
#等待附件上傳完成
wait.until(EC.visibility_of_element_located(('xpath','//span[.="上傳完成"]')))
print("附件上傳完成!")
time.sleep(3)
#切入正文frame
wait.until(EC.frame_to_be_available_and_switch_to_it(\
('xpath','//iframe[@class="APP-editor-iframe"]')))
contentBox = driver.find_element_by_xpath('/html/body')
contentBox.send_keys("學(xué)習(xí) 理財 看書 如何轉(zhuǎn)更多錢錢,不為錢而工作")
#切出frame,切到正文
driver.switch_to.default_content()
#發(fā)送
driver.find_element_by_xpath(\
'//footer[@class="jp0"]//span[text()="發(fā)送"]//preceding-sibling::span//b').click()
#顯式等待發(fā)送成功的//h2[contains(@id,"succInfo")] //h2[text()="發(fā)送成功"]
if wait.until(EC.visibility_of_element_located(('xpath','//h2[text()="發(fā)送成功"]'))):
print("郵件發(fā)送成功!")
else:
print("郵件發(fā)送失??!")
time.sleep(5)
except NoSuchElementException as e:
print(traceback.print_exc())
except TimeoutException as e:
print(traceback.print_exc())
except Exception as e:
print(traceback.print_exc())
def main():
login_126()
send_mail()
close_driver()
if __name__ == "__main__":
main()
文章題目:webdriver實現(xiàn)用126郵箱給你自己發(fā)一個帶有附件、標(biāo)題、正文的郵件
本文地址:http://chinadenli.net/article8/joscop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計、軟件開發(fā)、做網(wǎng)站、服務(wù)器托管、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)