【转】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!

2. 滚动条在最下方,点击submit用户,提示:Thanks!

以上如果是手动测试显然很简单,那么如何用selenium测试呢。
经过IDE录制,发现拖动滚动条的动作并没有录制下来!那么能想到的方法只有利用javascript来设置了。

Baidu后得到的知识是:
<body onload= "document.body.scrollTop=0 ">

也就是说如果scrollTop=0 时,滚动条就会默认在最上方
<body onload= "document.body.scrollTop=100000 ">

也就是说如果scrollTop=100000 时,滚动条就会默认在最下方
通过以上,以及学习的selenium调用javascript的知识:
在javascript中调用页面上的元素的方法
this.browserbot.getUserWindow().document.getElementById('text')
这样设置元素的属性就很简单了
this.browserbot.getUserWindow().document.getElementById('text').scrollTop=10000"
经过修改的IDE脚本

<!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来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候 ...
随机推荐
- cgpwn2-嫖来的wp
本想练习pwn的题目活跃下思维,但是接触后发现完全不懂,gg 然后就多方搜集,弄来了一些工具(IDA pro.pwntool)结果自己还是不会用,又是一番刷视频,结果看完又是一脸懵. 只记得一个快捷键 ...
- 「SP1716」GSS3 - Can you answer these queries III
传送门 Luogu 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: ...
- 关于Essay写作的Tips及整体讲解
Essay,相信在国外留学的小伙伴们都不会陌生,无论你是读本科还是硕士,甚至是读中学,都要经历Essay写作,也就是我们的小论文.那么,无论你是开始了英国学校的学习还是将要开始,你对Essay写作的了 ...
- Linux打印变量、环境配置、别名和文件删除操作
一.打印命令 1.echo打印命令 a.打印环境变量 echo $Path b.打印Path命令目录 which,比如:which ls表示打印的是Path目录中第一定义的全局变量的目录中命令. 二. ...
- python-python基础4
本章内容: 装饰器 生成器 迭代器 json & pickle 模块 软件目录结构规范 一.装饰器 装饰器 在不改动函数代码的基础上无限制扩展函数功能的一种机制,本质上讲,装饰器是一个返回函数 ...
- PE文件结构体-IMAGE_DATA_DIRECTORY
IMAGE_OPTIONAL_HEADER结构体最后一个成员是数组结构,大小为16,每个元素都是一个IMAGE_DATA_DIRECTORY结构体 typedef struct _IMAGE_DATA ...
- tools.lombok
@Slf4j @Data @Accessors @Builder
- Thymeleaf(一)---引入js/css文件
th:href="@{/static/css/style.css}" th:src="@{/static/js/thymeleaf.js}" index.htm ...
- 001-PHP静态变量
<?php function ADD1() { static $a = 10; //定义静态变量 $a += 1; echo "静态变量a的值为:" . $a . " ...
- login SMTP send mail error : Unable to read data from the transport connection: net_io_connectionclosed
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass); client.Credentials = new NetworkCr ...