欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

使用selenium控制滚动条(非整屏body)

发布时间:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的这篇文章主要介绍了 使用selenium控制滚动条(非整屏body) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

方法原理:

(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 sometext. This is some text. This is some text. This is some text. This issome 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 sometext. This is some text. This is some text. This is some text. This issome 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 sometext. This is some text. This is some text. This is some text. This issome 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> View Code

 

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;@Beforepublic void setUp(){//loginSystem.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>() {@Overridepublic 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();}}@Testpublic void testLogic(){}@Afterpublic void tearDown(){if(driver!=null){driver.quit();}}}

 

转载于:https://www.cnblogs.com/splvxh/p/4234043.html

总结

以上是生活随笔为你收集整理的使用selenium控制滚动条(非整屏body)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。