操作富文本框

富文本框常见的技术用到了Frame标签,并且在Frame里面实现了一个完整的HTML网页结构。

方法一:

#!usr/bin/env python  
#-*- coding:utf-8 -*-  
""" 
@author:   sleeping_cat
@Contact : zwy24zwy@163.com 
"""
#操作富文本框

from selenium import webdriver
import unittest,traceback,time
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):
        self.driver = webdriver.Firefox()

    def test_SohuMailSendEMail(self):
        url = 'http://mail.sohu.com'
        self.driver.get(url)
        try:
            userName = self.driver.find_element_by_xpath\
                ('//input[@placeholder = "请输入您的邮箱:"]')
            userName.clear()
            userName.send_keys("xxxx")
            passWord = self.driver.find_element_by_xpath\
                ('//input[@placeholder = "请输入您的密码:"]')
            passWord.clear()
            passWord.send_keys("xxxx")
            login = self.driver.find_element_by_xpath('//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('//li[text()="写邮件"]').click()
            time.sleep(2)
            receiver = self.driver.find_element_by_xpath\
                ('//div[@arr = "mail.to_render"]//input')
            receiver.send_keys("xxxx")
            subject = self.driver.find_element_by_xpath\
                ('//input[@ng-model = "mail.subject"]')
            subject.send_keys("测试邮件")
            # 获取邮件正文编辑区域的iframe页面元素对象
            iframe = self.driver.find_element_by_xpath('//iframe[contains(@id,"ueditor_0")]')
            self.driver.switch_to.frame(iframe)#通过.switch_to.frame()方法切换进入富文本框中
            # 获取富文本框中编辑页面元素对象
            editBox = self.driver.find_element_by_xpath("/html/body")
            editBox.send_keys("邮件的正文内容")
            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("邮件发送成功")
        except TimeoutException:
            print("显示等待页面元素超时")
        except NoSuchElementException:
            print("寻找的页面元素不存在",traceback.print_exc())
        except Exception:
            print(traceback.print_exc())
    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
'''
优点:实现简单,只要调用WebDriver对页面元素对象提供的send_keys()方法,即可实现内容输入
缺点:必须能定位到要被操作元素,对脚本编写人员的定位能力要求比较高,同时不支持HTML格式的内容输入
'''

方法二:

#!usr/bin/env python  
#-*- coding:utf-8 -*-  
""" 
@author:   sleeping_cat
@Contact : zwy24zwy@163.com 
""" 
#操作富文本框

from selenium import webdriver
import unittest,traceback,time
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):
        self.driver = webdriver.Firefox()

    def test_SohuMailSendEMail(self):
        url = 'http://mail.sohu.com'
        self.driver.get(url)
        try:
            userName = self.driver.find_element_by_xpath\
                ('//input[@placeholder = "请输入您的邮箱:"]')
            userName.clear()
            userName.send_keys("xxxx")
            passWord = self.driver.find_element_by_xpath\
                ('//input[@placeholder = "请输入您的密码:"]')
            passWord.clear()
            passWord.send_keys("xxxx")
            login = self.driver.find_element_by_xpath('//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('//li[text()="写邮件"]').click()
            time.sleep(2)
            receiver = self.driver.find_element_by_xpath\
                ('//div[@arr = "mail.to_render"]//input')
            receiver.send_keys("xxxx")
            subject = self.driver.find_element_by_xpath\
                ('//input[@ng-model = "mail.subject"]')
            subject.send_keys("测试邮件")
            # 获取邮件正文编辑区域的iframe页面元素对象
            iframe = self.driver.find_element_by_xpath('//iframe[contains(@id,"ueditor_0")]')
            self.driver.switch_to.frame(iframe)  # 通过.switch_to.frame()方法切换进入富文本框中
            self.driver.execute_script\
                ("document.getElementsByTagName('body')[0].innerHTML='<b>邮件的正文内容<b>;'")
            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("邮件发送成功")
        except TimeoutException:
            print("显示等待页面元素超时")
        except NoSuchElementException:
            print("寻找的页面元素不存在", traceback.print_exc())
        except Exception:
            print(traceback.print_exc())

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()
'''
优点:可以支持HTML格式的文字内容作为富文本框的输入内容
缺点:由于各种网页中富文本框实现的机制可能不同,有可能造成定位到富文本框的文本编辑区对象比较困难,此时就需要熟练了解HTML代码含义以及Frame的进出方式,对脚本编写人员的能力要求就比较高
'''

方法三:

#!usr/bin/env python  
#-*- coding:utf-8 -*-  
""" 
@author:   sleeping_cat
@Contact : zwy24zwy@163.com 
""" 
#操作富文本框

from selenium import webdriver
import unittest,traceback,time
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
from selenium.webdriver.common.keys import Keys
import win32clipboard as w
import win32api,win32con

#用于设置剪贴板内容
def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
    w.CloseClipboard()

#键盘按键映射字典
VK_CODE = {
    'Ctrl':0x11,
    'V':0x56}

#键盘键按下
def keyDown(keyName):
    win32api.keybd_event(VK_CODE[keyName],0,0,0)

#键盘键抬起
def keyUp(keyName):
    win32api.keybd_event(VK_CODE[keyName],0,win32con.KEYEVENTF_KEYUP,0)

class TestDemo(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_SohuMailSendEMail(self):
        url = 'http://mail.sohu.com'
        self.driver.get(url)
        try:
            userName = self.driver.find_element_by_xpath\
                ('//input[@placeholder = "请输入您的邮箱:"]')
            userName.clear()
            userName.send_keys("xxxx")
            passWord = self.driver.find_element_by_xpath\
                ('//input[@placeholder = "请输入您的密码:"]')
            passWord.clear()
            passWord.send_keys("xxxx")
            login = self.driver.find_element_by_xpath('//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('//li[text()="写邮件"]').click()
            time.sleep(2)
            receiver = self.driver.find_element_by_xpath\
                ('//div[@arr = "mail.to_render"]//input')
            receiver.send_keys("xxxx")
            subject = self.driver.find_element_by_xpath\
                ('//input[@ng-model = "mail.subject"]')
            subject.send_keys("测试邮件")
            subject.send_keys(Keys.TAB)
            setText("邮件正文内容")
            keyDown('Ctrl')
            keyDown('V')
            keyUp('V')
            keyUp('Ctrl')
            self.driver.find_element_by_xpath('//span[.="发送"]').click()
            wait.until(EC.visibility_of_element_located((By.XPATH, '//span[.="发送成功"]')))
            print("邮件发送成功")
        except TimeoutException:
            print("显示等待页面元素超时")
        except NoSuchElementException:
            print("寻找的页面元素不存在", traceback.print_exc())
        except Exception:
            print(traceback.print_exc())

        def tearDown(self):
            self.driver.quit()

    if __name__ == '__main__':
        unittest.main()
'''
优点:不管何种类型的富文本框,只要找到它上面的紧邻元素,然后通过模拟按Tab键的方式均可进入到富文本框中,由此可以使用一种方法解决所有类型的富文本框定位问题
缺点:不能在富文本框编辑器中进行HTML格式的内容输入
'''
posted @ 2018-01-13 10:42  sleeping_cat  阅读(1057)  评论(0编辑  收藏  举报