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 Test_RichTextBox(unittest.TestCase):
“”"
测试富文本框
“”"
def setUp(self) -> None:
self.dr = webdriver.Chrome()
def tearDown(self) -> None:
self.dr.quit()
def test_rich_text_box(self):
url = 'https://mail.163.com/'
self.dr.get(url)
self.dr.maximize_window()
time.sleep(2)
try:
iframe = self.dr.find_element_by_xpath\
('/html/body/div[2]/div[3]/div/div[3]/div[4]/div[1]/div[1]/iframe')
self.dr.switch_to.frame(iframe)
self.dr.find_element_by_name('email').send_keys('yj_20_20')
e = self.dr.find_element_by_name('password')
e.send_keys('bm20200408')
self.dr.find_element_by_id('dologin').click()
# 显示等待,登陆成功后出现“写信”
wait = WebDriverWait(self.dr,10)
wait.until(EC.element_to_be_clickable
((By.XPATH,'//*[@id="_mail_component_137_137"]/span[2]')))
self.dr.find_element_by_xpath\
('//*[@id="_mail_component_137_137"]/span[2]').click()
time.sleep(2)
# 输入收件人
receiver = self.dr.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[1]/div[1]/div/div[2]/div/input')
receiver.clear()
receiver.send_keys('请输入邮箱地址')
# 输入主题
subject = self.dr.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[2]/div[1]/div/div/input')
subject.clear()
subject.send_keys('你好,美女!一封测试邮件,来自帅哥的问候')
# 获取正文编辑区iframe的页面元素
iframe = self.dr.find_element_by_xpath\
('/html/body/div[2]/div[1]/div[2]/div[1]/section/section/div/div[1]/div[1]/div[2]/iframe')
# 切换到富文本编辑框
self.dr.switch_to.frame(iframe)
# 获取富文本框中的编辑元素对象
editBox = self.dr.find_element_by_xpath('/html/body')
editBox.send_keys('\\t'+'如果你不那么美,就不用回信了。哈哈哈')
# 从富文本框切回到默认页面
self.dr.switch_to.default_content()
# 点击发送
self.dr.find_element_by_xpath\
('/html/body/div[2]/div[1]/div[2]/div[1]/section/footer/div[1]/span[1]/b').click()
# 显式等待发送成功
# wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="1595602595940_succInfo"]/text()')))
# print('邮件发送成功')
except TimeoutException:
print('显示等待页面元素超时')
except NoSuchElementException as e:
print('寻找的页面元素不存在:'+ str(e))
except Exception:
print(traceback.print_exc())
if name == ‘main’:
unittest.main()
from 自动化测web.书籍selenium.操作富文本框 import Test_RichTextBox
tester = Test_RichTextBox()
num=1
while num <=100:
tester.setUp()
tester.test_rich_text_box()
print('第%d次发送'%num)
tester.tearDown()
num+=1
有点无聊了!