【转】Selenium 利用javascript 控制滚动条
http://luyongxin88.blog.163.com/blog/static/92558072011101913013149/
< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
在工作中,遇到这样的问题,注册时的法律条文需要阅读,判断用户是否阅读的标准是:滚动条是否拉到最下方。以下是我模拟的2种情况:
1. 滚动条在上方时,点击submit用户,提示:please view top new terms!
![](http://img0.ph.126.net/TusvAABQDCr0RQ5ZCthFTQ==/3082995419928912660.jpg)
2. 滚动条在最下方,点击submit用户,提示:Thanks!
![](http://img6.ph.126.net/x93MG0WF_SmUOUMBbDy5Lg==/2529897090692472046.jpg)
以上如果是手动测试显然很简单,那么如何用selenium测试呢。
经过IDE录制,发现拖动滚动条的动作并没有录制下来!那么能想到的方法只有利用javascript来设置了。
![](http://img7.ph.126.net/A91ZMM6Gr6VU7FcmVfqgpg==/2542000514691029097.jpg)
Baidu后得到的知识是:
<body onload= "document.body.scrollTop=0 ">
![](http://img6.ph.126.net/HQuMruPNXVOPQA2yDnDl2A==/2655153455328708076.jpg)
也就是说如果scrollTop=0 时,滚动条就会默认在最上方
<body onload= "document.body.scrollTop=100000 ">
![](http://img6.ph.126.net/7f1DMFVX6WaVCNx7nyIvSw==/1580481994247426356.jpg)
也就是说如果scrollTop=100000 时,滚动条就会默认在最下方
通过以上,以及学习的selenium调用javascript的知识:
在javascript中调用页面上的元素的方法
this.browserbot.getUserWindow().document.getElementById('text')
这样设置元素的属性就很简单了
this.browserbot.getUserWindow().document.getElementById('text').scrollTop=10000"
经过修改的IDE脚本
![](http://img7.ph.126.net/05NeSLQlW7lRDtRIr8nejw==/2533274790412999417.jpg)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Selenium Study</title> <script language="JavaScript"> function check(){ var clientHeight = document.getElementById('text').clientHeight; var scrollTop = document.getElementById('text').scrollTop; var scrollHeight = document.getElementById('text').scrollHeight; if(clientHeight + scrollTop < scrollHeight){ alert("Please view top news terms !"); return false; }else{ alert("Thanks !"); } } function set() { document.getElementById('text').scrollTop=10000; } </script> </head> <body> <form id="form1" method="post" onsubmit="return check();"> <textarea id="text" name="text" cols="70" rows="14"> Selenium IDE is the a Firefox plugin that does record-and-playback of interactions with the browser. Use this to either create simple scripts, assist in exploratory testing. It can also export Remote Control or WebDriver scripts, though they tend to be somewhat brittle and should be overhauled into some sort of Page Object-y structure for any kind of resiliency. Selenium IDE is the a Firefox plugin that does record-and-playback of interactions with the browser. Use this to either create simple scripts, assist in exploratory testing. It can also export Remote Control or WebDriver scripts, though they tend to be somewhat brittle and should be overhauled into some sort of Page Object-y structure for any kind of resiliency. Selenium IDE is the a Firefox plugin that does record-and-playback of interactions with the browser. Use this to either create simple scripts, assist in exploratory testing. It can also export Remote Control or WebDriver scripts, though they tend to be somewhat brittle and should be overhauled into some sort of Page Object-y structure for any kind of resiliency. Selenium IDE is the a Firefox plugin that does record-and-playback of interactions with the browser. Use this to either create simple scripts, assist in exploratory testing. It can also export Remote Control or WebDriver scripts, though they tend to be somewhat brittle and should be overhauled into some sort of Page Object-y structure for any kind of resiliency. </textarea><br /><br /> <input type="submit" id="submit" name="submit" value="Submit" /> </form> </body> </html> |
【转】Selenium 利用javascript 控制滚动条的更多相关文章
- javascript控制滚动条的位置,获取控件的位置
一.如下是定位鼠标在视窗中的位置,先定位视窗和页面直接的距离. function getMousePoint() { var point = {x:0,y:0}; // 如果浏览器支持 pageYOf ...
- selenium执行JavaScript语句:控制滚动条 聚焦元素 改变下拉选项
1. 执行js脚本 控制滚动条 # http://www.cnblogs.com/yoyoketang/p/6128655.html In [347]: js = "window.scrol ...
- 使用selenium控制滚动条(非整屏body)
方法原理: (1)使用jQuery CSS 操作 - scrollTop() 方法,设置 <div> 元素中滚动条的垂直偏移,语法:$(selector).scrollTop(of ...
- Java&Selenium控制滚动条方法封装
Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...
- Java&Selenium 鼠标键盘及滚动条控制相关方法封装
一.摘要 本片博文主要展示在使用Selenium with java做web自动化时,一些不得不模拟鼠标操作.模拟键盘操作和控制滚动条的java代码 二.模拟鼠标操作 package util; im ...
- 【转】selenium技巧 - 通过js来控制滚动条,通过xpath定位最上层的div层
http://blog.csdn.net/iceryan/article/details/8162703 业务流程: 1.打开此网页 http://nanjing.xiaomishu.com/sh ...
- BDD测试之selenium控制滚动条
一.对于页面存在滚动条,可以通过插入JS控制滚动条(最常用的方法) (1)将滚动条移动到指定坐标位置处 ((JavascriptExecutor) driver).executeScript(&quo ...
- [TimLinux] JavaScript 代码控制滚动条移动到顶部/底部
1. scrollIntoView函数 这个函数控制滚动条顶部内容.还是底部内容呈现在视图窗口中,接收一个参数:boolean值. true: 顶部出现在视图窗口中 false: 底部存在在视图窗口中 ...
- 自动化测试-13.selenium执行JS处理滚动条
前言 selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候 ...
随机推荐
- 1_02_MSSQL课程_T_SQL语句_手动创建数据库和表
1. 基本的DML SQL脚本 ->简单的查询: Select 列名 From 表明 where 条件表达式 ->插入数据: Insert Into 表名(列名1,列名2,列名3..... ...
- PromQL操作符
PromQL操作符 使用PromQL除了能够方便的按照查询和过滤时间序列以外,PromQL还支持丰富的操作符,用户可以使用这些操作符对进一步的对事件序列进行二次加工.这些操作符包括:数学运算符,逻辑运 ...
- linux打包解压包(.tar .gz .tar.gz .zip)
01-.tar格式 解包:[*******]$ tar xvf FileName.tar 打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压 ...
- GoJS组织结构图2
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 4、mysql查询练习
1.创建四个表供查询 (1)学生表—Student 学号 姓名 性别 出生年月日 所在班级 [语句] > create table student( -> sno varchar(20) ...
- redis集群JedisCluster连接关闭问题
JedisCluster连接关闭问题 set方法为例 //伪代码 JedisCluster jedisCluster = new JedisCluster(); jedisCluster.set(&q ...
- Luogu P3263 [JLOI2015]有意义的字符串
Link 设\(e=\frac{b+\sqrt d}2,i=\frac{b-\sqrt d}2\). 显然\(f_n=e^n+i^n\)是一个整数,且\(f_n=(e+i)f_{n-1}+eif_{n ...
- PV 动态供给【转】
前面的例子中,我们提前创建了 PV,然后通过 PVC 申请 PV 并在 Pod 中使用,这种方式叫做静态供给(Static Provision). 与之对应的是动态供给(Dynamical Provi ...
- Ajax发送PUT/DELETE请求时出现错误的原因及解决方案
本文讲什么? 大家应该都知道.在HTTP中,规定了很多种请求方式,包括POST,PUT,GET,DELETE等.每一种方式都有这种方式的独特的用处,根据英文名称,我们能够很清楚的知道DELETE方法的 ...
- GoJS实例2
复制如下内容保存到空白的.html文件中,用浏览器打开即可查看效果 <!DOCTYPE html> <html> <head> <meta name=&quo ...