Selenium 利用javascript 控制滚动条

简介:

 以下备注所用test.html 的代码(我也是在网上找的,简单的修改显示文字而已),供大家使用:

<!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>

 < 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脚本

  以下备注所用test.html 的代码(我也是在网上找的,简单的修改显示文字而已),供大家使用:

<!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>

   



最新内容请见作者的GitHub页:http://qaseven.github.io/

   

目录
相关文章
|
1月前
|
JavaScript 前端开发 测试技术
使用Selenium执行JavaScript脚本:探索Web自动化的新领域
本文介绍了如何在Selenium中使用JavaScript解决自动化测试中的复杂问题。Selenium的`execute_script`函数用于同步执行JS,例如滑动页面、操作时间控件等。在滑动操作示例中,通过JS将页面滚动到底部,点击下一页并获取页面信息。对于只读时间控件,利用JS去除readonly属性并设置新日期。使用JS扩展了Selenium的功能,提高了测试效率和精准度,适用于各种自动化测试场景。
46 1
|
8月前
|
JavaScript
selenium--操作JS弹框
selenium--操作JS弹框
|
3月前
|
移动开发 JavaScript 前端开发
分享33个JS滚动条特效&47个JS瀑布流特效,总有一款适合您
分享33个JS滚动条特效&47个JS瀑布流特效,总有一款适合您
27 0
|
4月前
|
JavaScript
【经典】原生JS实现“用鼠标拖拽(drag)内容DIV”,滚动条对应同步滚动
【经典】原生JS实现“用鼠标拖拽(drag)内容DIV”,滚动条对应同步滚动
|
5月前
|
Web App开发 JavaScript 前端开发
Python Selenium 执行 JavaScript
Python Selenium 执行 JavaScript
|
Web App开发 前端开发 JavaScript
Selenium 如何定位 JavaScript 动态生成的页面元素
Selenium 是一个自动化测试工具,可以用来模拟浏览器的操作,如点击、输入、滚动等。但是有时候,我们需要定位的页面元素并不是一开始就存在的,而是由 JavaScript 动态生成的。这时候,如果我们直接用 Selenium 的 find_element 方法去定位元素,可能会出现找不到元素的错误,因为页面还没有加载完成。为了解决这个问题,我们需要使用一些特定的定位技巧,让 Selenium 等待元素出现后再进行操作。
370 0
|
7月前
|
JavaScript
计算滚动条的宽度--js
计算滚动条的宽度--js
37 0
|
7月前
|
JavaScript
js判断滚动条是否到底部demo效果示例(整理)
js判断滚动条是否到底部demo效果示例(整理)
|
8月前
|
Web App开发 JavaScript
selenium--浏览器滚动条操作
selenium--浏览器滚动条操作
|
9月前
|
Web App开发 JavaScript 前端开发
如何使用Selenium自动化Chrome浏览器进行Javascript内容的数据挖掘和分析?
数据挖掘和分析是当今互联网时代的重要技能,它可以帮助我们从海量的信息中提取有价值的知识,为我们的决策和行动提供支持。但是,有些网站的内容是通过Javascript动态生成的,这就给数据挖掘和分析带来了一定的难度。如何才能有效地获取和处理这些Javascript内容呢?本文将介绍一种简单而强大的方法,就是使用Selenium自动化Chrome浏览器进行Javascript内容的数据挖掘和分析。
如何使用Selenium自动化Chrome浏览器进行Javascript内容的数据挖掘和分析?