標籤:content case ace 通過 comm path wait serve strong
操作富文字框-就是郵件內文部分,可以選字型啥的第一種方式:一般都是在iframe裡,要切進去,一般是”html/body”,編輯之後,再切出來,然後再send_keys就完事兒
#encoding=utf-8
from selenium import webdriver
import unittest, time, traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
class TestDemo(unittest.TestCase):
def setUp(self):
# 啟動Firefox瀏覽器
#self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
self.driver = webdriver.Firefox(executable_path="c:\\geckodriver")
def test_SohuMailSendEMail(self):
url = "http://mail.sohu.com"
# 訪問搜狐郵箱登入頁
self.driver.get(url)
try:
userName = self.driver.find_element_by_xpath\
(u‘//input[@placeholder="請輸入您的郵箱"]‘)
userName.clear()
userName.send_keys("fosterwu")
passWord = self.driver.find_element_by_xpath\
(u‘//input[@placeholder="請輸入您的密碼"]‘)
passWord.clear()
passWord.send_keys("1111")
login = self.driver.find_element_by_xpath(u‘//input[@value="登 錄"]‘)
login.click()
wait = WebDriverWait(self.driver, 10)
# 顯示等待,確定頁面是否成功登入並跳轉到登入成功後的首頁
wait.until(EC.element_to_be_clickable\
((By.XPATH, ‘//li[text()="寫郵件"]‘)))
self.driver.find_element_by_xpath(u‘//li[text()="寫郵件"]‘).click()
time.sleep(2)
receiver = self.driver.find_element_by_xpath\
(‘//div[@arr="mail.to_render"]//input‘)
# 輸入收件者
receiver.send_keys("[email protected]")
subject = self.driver.find_element_by_xpath\
(‘//input[@ng-model="mail.subject"]‘)
# 輸入郵件標題
subject.send_keys(u"一封測試郵件!")
# 擷取郵件內文編輯地區的iframe頁面元素對象
iframe = self.driver.find_element_by_xpath\
(‘//iframe[contains(@id, "ueditor")]‘)
# 通過switch_to.frame()方法切換進入富文字框中
self.driver.switch_to.frame(iframe)
# 擷取富文字框中編輯頁面元素對象
editBox = self.driver.find_element_by_xpath("/html/body")
# 輸入郵件內文
editBox.send_keys(u"郵件的本文內容")
# 從富文字框中切換出,回到預設頁面
self.driver.switch_to.default_content()
# 找到頁面上的“發送”按鈕,並單擊它
self.driver.find_element_by_xpath(‘//span[.="發送"]‘).click()
# 顯示都等待含有關鍵字串“發送成功”的頁面元素出現在頁面中
wait.until(EC.visibility_of_element_located\
((By.XPATH, ‘//span[.="發送成功"]‘)))
print u"郵件發送成功"
except TimeoutException:
print u"顯示等待頁面元素逾時"
except NoSuchElementException:
print u"尋找的頁面元素不存在", traceback.print_exc()
except Exception:
# 列印其他異常堆棧資訊
print traceback.print_exc()
def tearDown(self):
# 退出IE瀏覽器
self.driver.quit()
if __name__ == ‘__main__‘:
unittest.main()
D:\test>python test.py
郵件發送成功
.
----------------------------------------------------------------------
Ran 1 test in 58.226s
OK
python webdriver api-操作富文字框-待續