如何使用webdriver java中的jscroll窗格滚动

时间:2013-12-11 16:55:31

标签: java javascript selenium-webdriver

当窗格保持不变时,元素不会进入视图。我相信如果我向下滚动窗格,它可能会解决这个问题吗?

所以基本上我想将div向下滚动到一个元素,以便进入可见性。

我需要将哪些代码添加到我的webdriver java中?

我尝试了以下但没有成功:

WebDriver driver = null;
JavascriptExecutor js = (JavascriptExecutor) driver;    
js.executeScript("$('#fed-panel').data('jsp').scrollToBottom()");

1 个答案:

答案 0 :(得分:0)

我在我的代码中使用以下内容来处理一些只在向下滚动时出现的Ajax对象。你可以用ObjectVisible替换下面的'this.objectExists'函数(这是我实现的)。

    public void scrollDown(String identifier, String m){
            Boolean readyStateComplete = false;
            int i=0;

            while (!readyStateComplete) {
                    JavascriptExecutor js = (JavascriptExecutor)driver;
                    js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
                                                     "document.body.scrollHeight,document.documentElement.clientHeight));");
                    covlog.logIssue(CovLogger.DEBUG, "scrollDown","Scrolling for the " + ++i + " times");
                    String tmp = js.executeScript("return document.readyState").toString();

                readyStateComplete = tmp.contentEquals("complete");
                //In case the object doesn't exists, break out of the loop
                //the object might represent a spinner that shows that more
                // records are downloaded.
                if ((i%24)==0){ //24 is page size
                        if (!this.objectExists(identifier, m)){
                            readyStateComplete = true;
                        }
                    }
            }
    }