selenium:滚动到页面结尾

selenium

我是webdriverJS的新手。 我已经在Java中尝试了这个评价

Long repaeted = 0l, scrollHeight = 0l, returnHeight = 0l; while(true){ if (repaeted == 0) { returnHeight = (Long) jse.executeScript("var scroll =document.documentElement.scrollHeight;window.scrollTo(0, scroll); return scroll;"); System.out.println("Height : "+scrollHeight +"\t Chnage : "+returnHeight+ "\t Repeated : "+repaeted); scrollHeight = returnHeight; }else { returnHeight = (Long) jse.executeScript("var scroll = document.documentElement.scrollHeight;window.scrollTo(0, scroll); return scroll;"); System.out.println("Height : "+scrollHeight +"\t Chnage : "+returnHeight+ "\t Repeated : "+repaeted); if (scrollHeight.intValue() == returnHeight.intValue()) { System.out.println("Break.."+ returnHeight); break; } else { scrollHeight = returnHeight; } } repaeted++; } 

但是我在迭代循环时遇到了webdriverjs的问题

 var webdriver = require('..'), By = webdriver.By, until = webdriver.until; // make sure chromedriver can be found on your system PATH var driver = new webdriver.Builder() .forBrowser('chrome') .withCapabilities(webdriver.Capabilities.chrome()) .build(); driver.get('https://in.yahoo.com/').then(function(){ var window = new webdriver.WebDriver.Window(driver); window.maximize(); driver.manage().timeouts().implicitlyWait(1000 * 3); }) .then(function(){ console.log('Entered'); var check = 0, count = 0 for(var i = 0; i< 50; i++){ //driver.sleep(1000 * 2); driver.executeScript('var dynamicscroll = document.documentElement.scrollHeight;window.scrollTo(0, dynamicscroll);return dynamicscroll;').then(function(height){ console.log('Check : '+check+' Height : '+height +' Repeated : '+(count++)); if(check === 0 || check !== height){console.log('continue'); check = height; } else { console.log('break'); i = 100; } }); } }) .then(null, function(err) { console.error("An error was thrown! By Promise..." + err); }); driver.quit(); 

在我的代码中, 我已经硬编码循环迭代到50次,我想退出/打破循环滚动高度达到结束 。 在这种方法中,我想删除像java代码的硬代码,因为我不知道有多less次迭代其他应用程序的滚动不断增加dynamic 。 例如:Facebook应用程序,雅虎新闻….

滚动到dynamic页面的底部可能是具有挑战性的,取决于页面如何实现。

首先你必须find带有滚动条的容器,因为它可以和链接到window.scrollTo

然后通过增加scrollTop滚动容器,直到scrollHeight变得稳定而没有挂起的请求。 要检查是否有挂起的请求,要么评估jQuery.active如果页面有JQuery,或者挂接XMLHttpRequest来监视send的调用。

这是一个使用通用函数滚动到页面底部多次或直到结束的例子:

 var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder().forBrowser('chrome').build(); driver.get('https://groups.google.com/forum/#!search/webdriverjs'); // scroll to the bottom 3 times driver.executeAsyncScript(scrollBottom, 3) .then(n => console.log(`scrolled ${n} time(s)`)); // scroll to the bottom until the end driver.executeAsyncScript(scrollBottom) .then(n => console.log(`scrolled ${n} time(s)`)); 
 function scrollBottom(){ var count = arguments[arguments.length - 2] || 0x7fffffff; var callback = arguments[arguments.length - 1]; /* get the scrollable container */ var elm = document.elementFromPoint(window.innerWidth - 25, window.innerHeight / 2); for ( ;elm && (++elm.scrollTop, !elm.scrollTop); elm=elm.parentElement); elm = elm || document.documentElement; /* hook XMLHttpRequest to monitor Ajax requests */ if (!('idle' in XMLHttpRequest)) (function(){ var n = 0, t = Date.now(), send = XMLHttpRequest.prototype.send; var dispose = function(){ --n; t = Date.now(); }; var loadend = function(){ setTimeout(dispose, 1) }; XMLHttpRequest.idle = function() { return n > 0 ? 0 : Date.now() - t; }; XMLHttpRequest.prototype.send = function(){ ++n; this.addEventListener('loadend', loadend); send.apply(this, arguments); }; })(); /* scroll until steady scrollHeight or count of scroll and no pending request */ var i = 0, scrollHeight = -1, scrollTop = -1; (function scroll(){ if ((scrollHeight === elm.scrollHeight || i === count) && XMLHttpRequest.idle() > 60) return callback(i); scrollTop = elm.scrollTop; scrollHeight = elm.scrollHeight; if (i < count) i += (elm.scrollTop = 0x7fffffff, scrollTop !== elm.scrollTop); setTimeout(scroll, 100); })(); } 

或者通过滚动直到高度在特定时间(此处为5秒)不再增加:

 function scrollBottom(){ var count = arguments[arguments.length - 2] || 0x7fffffff; var callback = arguments[arguments.length - 1]; var timeout = 5000; /* 5 seconds timeout */ var i = 0; /* get the scrollable container */ var elm = document.elementFromPoint(window.innerWidth - 25, window.innerHeight / 2); for ( ;elm && (++elm.scrollTop, !elm.scrollTop); elm=elm.parentElement); elm = elm || document.documentElement; /* scroll while the height is increasing or until timeout */ (function scroll(){ var endtime = Date.now() + timeout; var height = elm.scrollHeight; elm.scrollTop = 0x7fffffff; /* scroll */ setTimeout(function check(){ if (Date.now() > endtime) /* returns if waited more than 5 sec */ callback(i); else if (elm.scrollHeight == height) /* wait again if same height */ setTimeout(check, 60); else if (++i === count) /* returns if scrolled the expected count */ callback(i); else /* scroll again */ setTimeout(scroll, 60); }, 250); })(); } 

纯Javascript

在JavaScript中,我们可以使用setTimeout ()函数。 这将在您确定的时间延迟之后recursion地调用指定的函数。

我已经testing了谷歌组的应用程序,其div标签垂直滚动dynamic增加。 要加载我使用的时间延迟5000的内容,您可以在浏览器控制台中使用以下urltesting此代码: https://groups.google.com/forum/#!search/webdrierjshttps://groups.google.com/forum/#!search/webdrierjs search/ https://groups.google.com/forum/#!search/webdrierjs

 var i = 0, height = 0, check = 0, t = null; flow(); function run(arg){ var objDiv = document.querySelector('div.IVILX2C-b-F'); objDiv.scrollTop = objDiv.scrollHeight; return objDiv.scrollHeight; } function flow() { i++; switch(i){ case 0: height = run(i); sleep(5000); break; case -1: run(i); clearTimeout(t); //stops flow break; default: check = run(i); console.log('Return Height : '+check +' Count : '+i); if(check === height){ i = -2; console.log('Break message : '+i); }else { console.log('Changed...'); height = check; } sleep(5000); break; } } function sleep(delay) { t=setTimeout("flow()",delay);} //starts flow control again after time specified. //function sleep(delay) { var start = new Date().getTime(); while (new Date().getTime() < start + delay); flow(); } // stops execution and then continues. 

但即使我不能使用Webdriver / webdriverJS运行此脚本,因为它不会在时间延迟上调用recursion函数。

根据经验,滚动到页面末尾的最快方法是查找页脚元素并移动,通常是#footer.footer或者只是footerselect器就可以完成。 例如:

 footer = driver.findElement({id: "footer"}); driver.executeScript("arguments[0].scrollIntoView(false);", footer); 

在Facebook,Twitter等“无尽”stream的情况下,当达到极限时,它们可能会阻止您,因此可以将最大迭代次数与window.scrollTo(0, 300); recursion地等待每个滚动后几秒钟。

试试这个代码 – 它在Python中的工作(只是在你的情况下翻译):

  # Get scroll height. last_height = self.driver.execute_script("return document.body.scrollHeight") while True: # Scroll down to the bottom. self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Wait to load a page. time.sleep(2) # Calculate new scroll height and compare with last scroll height. new_height = self.driver.execute_script("return document.body.scrollHeight") if new_height == last_height: break last_height = new_height 

顺便说一句:这里是我从Python运行的JavaScript。