根据页面的实现方式,滚动到动态页面的底部可能具有挑战性。
首先,您必须使用滚动条找到该容器,因为它可能与链接的容器不同
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 timesdriver.executeAsyncscript(scrollBottom, 3) .then(n => console.log(`scrolled ${n} time(s)`)); // scroll to the bottom until the enddriver.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]; var elm = document.elementFromPoint(window.innerWidth - 25, window.innerHeight / 2); for ( ;elm && (++elm.scrollTop, !elm.scrollTop); elm=elm.parentElement); elm = elm || document.documentElement; 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); }; })(); 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; var i = 0; var elm = document.elementFromPoint(window.innerWidth - 25, window.innerHeight / 2); for ( ;elm && (++elm.scrollTop, !elm.scrollTop); elm=elm.parentElement); elm = elm || document.documentElement; (function scroll(){ var endtime = Date.now() + timeout; var height = elm.scrollHeight; elm.scrollTop = 0x7fffffff; setTimeout(function check(){ if (Date.now() > endtime) callback(i); else if (elm.scrollHeight == height) setTimeout(check, 60); else if (++i === count) callback(i); else setTimeout(scroll, 60); }, 250); })();}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)