我正在尝试使用此代码完全滚动页面:
JavascriptExecutor js = (JavascriptExecutor) Browser; js.executeScript("javascript:window.onload=toBottom();"+ "function toBottom(){" +"window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +"document.body.scrollHeight,document.documentElement.clientHeight));" +"}"); js.executeScript("window.status = 'fail';"); //Attach the Ajax call back method js.executeScript( "$(document).ajaxComplete(function() {" + "status = 'success';});"); js.executeScript("window.status = 'fail';"); //Attach the Ajax call back method js.executeScript( "$(document).ajaxComplete(function() {" +"status = 'success';});");
这段代码工作正常,第一次尝试滚动页面,但当页面向下滚动时,新的数据出现在页面,这段代码无法再次滚动。 所以我需要的是有人会帮我滚动页面直到结束,直到滚动完成。
我为此使用任何循环?
帮助/build议/回应将不胜感激!
我有一个功能相似的页面,以及我之前回答的另一个问题。 我不熟悉任何通用的方法来知道页面上没有任何其他元素加载。 在我的情况下,页面被设计为在每个滚动中加载40/80(忘记确切的计数)元素。 因为,大多数情况下,我知道滚动的估计数量(因为我正在使用一个测试公司,我知道在db中有多少元素),我可以估计滚动的数量,并做了以下处理该页面。
public void ScrollPage(int counter) { const string script = @"window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));"; int count = 0; while (count != counter) { IJavaScriptExecutor js = _driver as IJavaScriptExecutor; js.ExecuteScript(script); Thread.Sleep(500); count++; } }
在这里看到我的其他答案
Java等同代码
public void ScrollPage(int counter) throws InterruptedException { String script = "window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));"; int count = 0; while (count != counter) { ((JavascriptExecutor)driver).executeScript(script); Thread.sleep(500); count++; } }
使用
ScrollPage(10);
在哪里滚动是必要的
所以,我会做这样的事情:
bool pageEnded = false; while (!pageEnded) { JavascriptExecutor js = (JavascriptExecutor) Browser; js.executeScript("window.scrollTo(0, document.body.offsetHeight);"); pageEnded = (String)js.executeScript("return document.readyState;") ? true; }