python webdriver api-操作富文本框
操作富文本框-就是邮件正文部分,可以选字体啥的
第一种方式:
一般都是在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("fosterwu@sohu.com")
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
第二种方法:
切到iframe里,然后执行js的方式,找到这个元素,(三种方式:d,名字,标签名—)
#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.Firefox(executable_path="c:\\geckodriver")
self.driver = webdriver.Firefox(executable_path = "d:\\geckodriver")
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("fosterwu")
passWord = self.driver.find_element_by_xpath\
('//input[@placeholder="请输入您的密码"]')
passWord.clear()
passWord.send_keys("1111")
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("fosterwu@sohu.com")
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)
# 通过JavaScript代码向邮件正文编辑框中输入正文
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 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 49.789s
OK
第三种方法:键盘事件,用tab键切换到frame区域,推荐使前两种方法
#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
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):
# 启动Firefox浏览器
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\
('//input[@placeholder="请输入您的邮箱"]')
userName.clear()
userName.send_keys("fosterwu")
passWord = self.driver.find_element_by_xpath\
('//input[@placeholder="请输入您的密码"]')
passWord.clear()
passWord.send_keys("1111")
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("xiaxiaoxu1987@163.com")
subject = self.driver.find_element_by_xpath\
('//input[@ng-model="mail.subject"]')
# 输入邮件标题
subject.send_keys(u"一封测试邮件!")
# 输入完邮件标题后,按下Tab键可以将页面焦点切换到富文本框编辑区域
subject.send_keys(Keys.TAB)
# 设置剪切板内容,也就是将要输入的正文内容
setText(u"邮件正文内容")
# 模拟键盘的Ctrl + v组合键,将剪切板内容粘贴到富文本编辑区中
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 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 54.255s
OK
python webdriver api-操作富文本框的更多相关文章
- webdriver高级应用- 操作富文本框
富文本框的技术实现和普通的文本框的定位存在较大的区别,富文本框的常见技术用到了Frame标签,并且在Frame里面实现了一个完整的HTML网页结构,所以使用普通的定位模式将无法直接定位到富文本框对象. ...
- Selenium常用API用法示例集----下拉框、文本域及富文本框、弹窗、JS、frame、文件上传和下载
元素识别方法.一组元素定位.鼠标操作.多窗口处理.下拉框.文本域及富文本框.弹窗.JS.frame.文件上传和下载 元素识别方法: driver.find_element_by_id() driver ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- django(7)modelform操作及验证、ajax操作普通表单数据提交、文件上传、富文本框基本使用
一.modelForm操作及验证 1.获取数据库数据,界面展示数据并且获取前端提交的数据,并动态显示select框中的数据 views.py from django.shortcuts import ...
- vue集成CKEditor构建框架的使用,遇到富文本框不出现工具栏等操作
官方关于Vue集成CKEditor富文本框的文档:https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/framew ...
- Java + selenium 元素定位(6)之iframe切换(即对富文本框的操作)
在元素定位中,对富文本框的元素定位是特别的,当我们使用普通的元素定位方法对富文本框进行操作时,我们会发现不管我们之前介绍的八种方法中的任何方法,我们都不能成功定位到富文本框,并对其进行操作.那是因为富 ...
- selenium 富文本框处理
selenium 富文本框处理, 网上有用API的解决方法1:参见:http://blog.csdn.net/xc5683/article/details/8963621 群里1位群友的解决方法2:参 ...
- Android 富文本框实现 RichEditText
Android系统自带控件没有富文本框控件,如果想写一封带格式的邮件基本上不可能,EdtiText只有默认一种格式,显示不能滿足要求,!!正好项目需要研究了一下,开发了此控件,现将一些源代码开放一下, ...
- 第三百九十五节,Django+Xadmin打造上线标准的在线教育平台—Xadmin集成富文本框
第三百九十五节,Django+Xadmin打造上线标准的在线教育平台—Xadmin集成富文本框 首先安装DjangoUeditor3模块 Ueditor HTML编辑器是百度开源的HTML编辑器 下载 ...
随机推荐
- canvas二:绘制圆和其他曲线
1.绘制圆 绘制圆是canvas里面不可缺少的功课,而且绘制圆在canvas中的用处很多,好嘞,开扯 绘制圆需要用到arc这个方法: arc(X坐标,Y坐标,半径,起始弧度,结束弧度,旋转方向): 弧 ...
- ASP.NET 4.0尚未在Web服务器注册 解决
http://www.cnblogs.com/lvxiouzi/p/3511446.html 安装asp.net 4.0.30319.0版本 命令: %windir%\Microsoft.NET\Fr ...
- WPS Word查询某些内容的出现次数
1.CTRL+F 打开查找窗体
- xcode 4.6 破解及真机调试
从安卓到IOS,从 eclipse 到xcode跨度还是比较大的.在研究的过程中发现,许多时候不仅仅是C,C++,JAVA和OBJECT-C的区别,相对于编程语言来说,操作习惯和开发工具带来的困惑要 ...
- Django 1.5.4 专题二 urls 和 view 提高
一.修改article/urls.py内容如下 二.修改django_test/urls.py如下 三.修改article/views.py如下 四.修改templates/article.html的 ...
- Delphi的打开文件对话框-TOpenDialog
1.TOpenDialog组件的典型用法 “打开”对话框是用TOpenDialog组件实现的,TOpenDialog组件是非可视组件. Filter属性用于设置文件过滤器,让对话框只列出特定类型的文件 ...
- 设计模式之装饰模式(Java实现)
“怎么了,鱼哥?” “唉,别提了,网购了一件衣服,结果发现和商家描述的差太多了,有色差就算了,质量还不好,质量不好就算了,竟然大小也不行,说好的3个X,邮的却是一个X的,不说了,退货去.你先开讲吧,你 ...
- 如何偷懒地用 PHP 搭建一个班级网站
版权声明:本文由李宜东原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/116 来源:腾云阁 https://www.qclo ...
- getOwnPropertyNames() & keys()
1.getOwnPropertyNames方法可以获得对象的所有属性名,并储存于一个数组当中. keys方法只能获取可遍历的属性名并储存于数组. 2.在完成notepad模块模拟的过程中使用了getO ...
- postgresql数据库中对重复数据的处理
我们在使用postgresql数据库的时候,如果一张数据表在未做任何约束的情况下,很可能会出现几条完全一样的数据,即重复数据.如下图所示: 那么如果我们要删除其中的2条该怎么办呢?第一种我们可以清空表 ...