方法原理:
(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(); } } }
只有当知道了全部,你的选择才有意义。