使用selenium控制滚动条(非整屏body)
生活随笔
收集整理的這篇文章主要介紹了
使用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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Lisenter笔记
- 下一篇: [php] 高级教程