使用滚动查找所有元素

ale*_*cxe 16 javascript selenium scroll selenium-webdriver protractor

找到页面上滚动的所有元素的最可靠和有效的方法是什么?


目前,我正在考虑使用element.all()filter()比较heightscrollHeight属性值:

element.all(by.xpath("//*")).filter(function (elm) {
    return protractor.promise.all([
        elm.getAttribute("height"),
        elm.getAttribute("scrollHeight")
    ]).then(function (heights) { 
        return heights[1] > heights[0];
    });
});
Run Code Online (Sandbox Code Playgroud)

但我不确定这种方法的正确性和性能.

And*_*ton 6

这适用于水平和垂直滚动条。诀窍是检测太宽/太短以及计算出的 CSS 是否允许您显示滚动条。

var ElementsWithScrolls = (function() {
    var getComputedStyle = document.body && document.body.currentStyle ? function(elem) {
        return elem.currentStyle;
    } : function(elem) {
        return document.defaultView.getComputedStyle(elem, null);
    };

    function getActualCss(elem, style) {
        return getComputedStyle(elem)[style];
    }

    function isXScrollable(elem) {
        return elem.offsetWidth < elem.scrollWidth &&
            autoOrScroll(getActualCss(elem, 'overflow-x'));
    }

    function isYScrollable(elem) {
        return elem.offsetHeight < elem.scrollHeight &&
            autoOrScroll(getActualCss(elem, 'overflow-y'));
    }

    function autoOrScroll(text) {
        return text == 'scroll' || text == 'auto';
    }

    function hasScroller(elem) {
        return isYScrollable(elem) || isXScrollable(elem);
    }
    return function ElemenetsWithScrolls() {
        return [].filter.call(document.querySelectorAll('*'), hasScroller);
    };
})();

ElementsWithScrolls();
Run Code Online (Sandbox Code Playgroud)