方法原理:

    (1)使用jQuery CSS 操作 - scrollTop() 方法,设置 <div> 元素中滚动条的垂直偏移,语法:$(selector).scrollTop(offset);
  (2)若要控制滚动条水平偏移,请使用方法scrollLeft(),语法:$(selector).scrollLeft(offset);
其中selector表示选择器,offset表示偏移量。
  
样例页面MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
$("#test div").scrollTop(100);
});
$(".btn2").click(function(){
alert($("div").scrollTop()+" px");
});
});
</script>
</head> <body>
<div id="test">
<div
style="border:1px solid black;width:200px;height:200px;overflow:auto">
This is some text. This is some text. This is some text. This is some
text. This is some text. This is some text. This is some text. This is
some text. This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text. This is some
text. This is some text. This is some text. This is some text. This is
some text. This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text. This is some
text. This is some text. This is some text. This is some text. This is
some text. This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.</div>
</div>
<button class="btn1">把 scroll top offset 设置为 100px</button>
<br />
<button class="btn2">获得 scroll top offset</button> </body>
</html>

selenium代码:

本例中仅控制滚动条垂直移动

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; public class TestScroll {
private WebDriver driver;
@Before
public void setUp(){
//login
System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
String url = "http://localhost:8080/test/MyJsp.jsp";
     //l打开测试页面
driver.get(url);
WebDriverWait wait = new WebDriverWait(driver, 5);
try {
WebElement webElement = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver webDriver) {
return webDriver.findElement(By.id("test"));
}
});
if(webElement==null){
System.out.println("页面还未加。。。。");
}else{
System.out.println("页面加载完成~~~~~~~~~~~");
JavascriptExecutor js = (JavascriptExecutor)driver;
          //执行js脚本,控制滚动条滚动
js.executeScript("$('#test div').scrollTop(100);");
Thread.sleep(10000);
}
} catch (TimeoutException e) {
System.out.println("打不开链接。。。。。");
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Test
public void testLogic(){
} @After
public void tearDown(){
if(driver!=null){
driver.quit();
}
} }

使用selenium控制滚动条(非整屏body)的更多相关文章

  1. Java&Selenium控制滚动条方法封装

    Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...

  2. BDD测试之selenium控制滚动条

    一.对于页面存在滚动条,可以通过插入JS控制滚动条(最常用的方法) (1)将滚动条移动到指定坐标位置处 ((JavascriptExecutor) driver).executeScript(&quo ...

  3. selenium执行JavaScript语句:控制滚动条 聚焦元素 改变下拉选项

    1. 执行js脚本 控制滚动条 # http://www.cnblogs.com/yoyoketang/p/6128655.html In [347]: js = "window.scrol ...

  4. 通过selenium控制浏览器滚动条

    目的:通过selenium控制浏览器滚动条 原理:通过 driver.execute_script()执行js代码,达到目的 driver.execute_script("window.sc ...

  5. 【转】selenium技巧 - 通过js来控制滚动条,通过xpath定位最上层的div层

    http://blog.csdn.net/iceryan/article/details/8162703 业务流程:   1.打开此网页 http://nanjing.xiaomishu.com/sh ...

  6. Selenium系列(22) - 通过selenium控制浏览器滚动条的几种方式

    如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...

  7. [Python爬虫] 之五:Selenium 处理滚动条

     selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的.这时候需要借助滚动条来拖 ...

  8. CSS 控制滚动条样式

    /*作为IT界最前端的技术达人,页面上的每一个元素的样式我们都必须较真,就是滚动条我们也不会忽略.下面我给大家分享一下如何通过CSS来控制滚动条的样式,代码如下:*/ 1 /*定义滚动条轨道*/ #s ...

  9. delphi 发送消息控制滚动条

    1.Perform 函数 DBGrid1.Perform(WM_VSCROLL,SB_PAGEDOWN,0);  //控制滚动条,向后翻页 DBGrid1.Perform(WM_VSCROLL,SB_ ...

随机推荐

  1. opendove中的odgw所需要的内核模块

    最近组里要做opendove相关的东西,需要odgw的一个kernel-module. 以前安装过,但备份不见了,在此做个链接备忘 : https://git.opendaylight.org/ger ...

  2. js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法

    var  questionId = []; var anSwerIdValue = []; ////javascript数组扩展indexOf()方法 Array.prototype.indexOf ...

  3. angularjs 遇到的问题汇总

    angular2 学习资源集锦:https://github.com/timjacobi/angular2-education 在学习angular开发项目过程遇到的问题:

  4. react在jsx语法中实现for循环

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  5. tomcat 7/8 启动非常慢的解决方法

    在日志中发现启动慢的地方: 2016-11-14 09:31:30.522 [localhost-startStop-1] INFO o.s.c.s.DefaultLifecycleProcessor ...

  6. C# DataGridView显示行号的三种方法

    方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: private void dgGrid_RowPostPaint( obj ...

  7. MySQL 第七天(核心优化一)

    一.Mysql核心优化 1. 优化的方面 ① 存储层:数据表"存储引擎"选取.字段选取.逆范式(3范式) ② 设计层:索引.分区/分表 ③ 架构层:分布式部署(主从模式/共享) ④ ...

  8. java 分布式锁方案

    第一步,自身的业务场景: 在我日常做的项目中,目前涉及了以下这些业务场景: 场景一: 比如分配任务场景.在这个场景中,由于是公司的业务后台系统,主要是用于审核人员的审核工作,并发量并不是很高,而且任务 ...

  9. iOS原型模式

    原型模式:大部分重复,只有一小部分不同的情况下,为了代码清晰和避免麻烦,用原型模式,会更方便一点 // 学生1 StudentModel *stu1 = [[StudentModel alloc] i ...

  10. ubuntu和win7 64双系统 安装

    我是thinkpad机器 主要参考到是http://www.weste.net/2012/4-29/82173.html这个地址到教程,表示感谢 这边说下安装好以后到环境设置 首先要 sudo apt ...