登錄163郵箱發郵件
阿新 • • 發佈:2018-09-05
editable tabindex word cond username use ont att web
#encoding=utf-8
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
#打開瀏覽器
driver = webdriver.Chrome(executable_path = "e:\\chromedriver")
url = "https://mail.163.com"
#訪問163郵箱
driver.get(url)
#暫停3秒,等待頁面出現,這個地方要加sleep,不然切入frame會出錯
time.sleep(3)
#定位登錄所在的frame
frame = driver.find_element_by_id("x-URS-iframe")
#切換進入frame
driver.switch_to.frame(frame)
time.sleep(2)
#獲取用戶名、密碼輸入框元素對象
username = driver.find_element_by_xpath(‘//input[@name="email"]‘)
password = driver.find_element_by_xpath(‘//input[@name="password"]‘)
#顯式等待用戶名輸入框出現,清空,並輸入用戶名
WebDriverWait(driver,10).until(EC.visibility_of(username))
username.clear()
username.send_keys("xxxxxx")
#顯式等待密碼輸入框出現,清空,並輸入密碼
WebDriverWait(driver,10).until(EC.visibility_of(password))
password.clear()
password.send_keys("xxxxxxx")
#顯式等待定位到登錄按鈕並點擊
WebDriverWait(driver,10).until(lambda x:x.find_element_by_id("dologin")).click()
assert
#發郵件
#暫停3秒,等待頁面出現
time.sleep(3)
#定位到寫信按鈕,顯式等待可點擊後,並點擊
writeButton = driver.find_element_by_xpath(‘//b[@class="nui-ico fn-bg ga0"]‘)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,‘//b[@class="nui-ico fn-bg ga0"]‘)))
writeButton.click()
#直接顯示等待元素出現後,並點擊
#WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath(‘//b[@class="nui-ico fn-bg ga0"]‘)).click()
#獲取收件人輸入框、主題輸入框元素對象
send_address = driver.find_element_by_xpath(‘//input[@class="nui-editableAddr-ipt"]‘)
subject = driver.find_element_by_xpath(‘//input[@class="nui-ipt-input" and @tabindex="1"]‘)
#等待收件人輸入框可用,並輸入收件人
WebDriverWait(driver,10,0.2).until(EC.visibility_of(send_address))
send_address.send_keys("[email protected]")
#等待主題輸入框可見,並輸入主題
WebDriverWait(driver,10).until(EC.visibility_of(subject))
subject.send_keys(u"測試163發郵件")
#定位到附件按鈕,並發送附件
attachment = driver.find_element_by_xpath(‘//input[@class="O0"]‘)
#用這個方法有問題
#WebDriverWait(driver,10).until(EC.visibility_of(attachment))
attachment.send_keys("e:\\1.jpg")
#WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath(‘//input[@class="O0"]‘)).send_keys("e:\\1.jpg")
#暫停幾秒,等待附件上傳
time.sleep(3)
#獲取到正文所在的frame,並切入
iframe = driver.find_element_by_xpath(‘//iframe[@class="APP-editor-iframe"]‘)
driver.switch_to.frame(iframe)
#獲取到正文輸入框對象元素,並輸入正文
content = driver.find_element_by_xpath(‘//body‘)
WebDriverWait(driver,10).until(EC.visibility_of(content))
content.send_keys(u"測試郵件發送")
#從正文frame切換出來,以便操作外面的發送按鈕
driver.switch_to.default_content()
time.sleep(3)
#定位到發送按鈕並點
sendButton = driver.find_element_by_xpath(‘//div[@class="frame-main-cont-body nui-scroll"]//div[@role="button"][1]//b‘)
sendButton.click()
#顯式等待方式定位發送按鈕,並點擊
#WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath(‘//div[@class="frame-main-cont-body nui-scroll"]//div[@role="button"][1]//b‘)).click()
time.sleep(5)
#退出驅動並關閉所有窗口
driver.quit()
登錄163郵箱發郵件