selenium操作滚动条的几种方式

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);

 

posted on 2015-08-26 18:49  乔叶叶  阅读(1956)  评论(0编辑  收藏  举报

导航