selenium操作滚动条的几种方式

时间:2021-12-07 21:12:45

1.操作滚动条到当前可见视图的元素位置

    WebElement element = dr.findElement(By.id("4"));
((JavascriptExecutor)dr).executeScript("arguments[0].scrollIntoView(true);", element);

或者

    Actions a = new Actions(dr);
a.moveToElement(dr.findElement(By.id("4"))).perform();

2.操作滚动条到底部或者顶部

        Actions a = new Actions(dr);
//a.sendKeys(Keys.PAGE_DOWN).perform();
     a.sendKeys(Keys.END).perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//a.sendKeys(Keys.PAGE_UP).perform();
     a.sendKeys(Keys.HOME).perform();

或者

        String js = "document.documentElement.scrollTop=10000";
((JavascriptExecutor)dr).executeScript(js);
String js1 = "document.documentElement.scrollTop=0";
((JavascriptExecutor)dr).executeScript(js1);

或者

        String js = "document.getElementById(\"su\").scrollTop=10000";
((JavascriptExecutor)dr).executeScript(js);
String js1 = "document.getElementById(\"su\").scrollTop=0";
((JavascriptExecutor)dr).executeScript(js1);