Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all elements with a scroll

What is the most reliable and efficient way to find all elements having a scroll on a page?


Currently, I'm thinking about using element.all() with filter() comparing the height and scrollHeight attribute values:

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];
    });
});

But I'm not sure about the correctness and performance of this approach.

like image 955
alecxe Avatar asked Dec 30 '15 15:12

alecxe


People also ask

How do you get scroll elements?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

How do you know which element is scrolled?

Show activity on this post. Then when you scroll down you should see one really tall box. Right click on it and select inspect element. That should give you the information you need.

What is scrollParent?

.scrollParent() : Get the closest ancestor element that is scrollable. This method does not accept any arguments. This method finds the nearest ancestor that allows scrolling. In other words, the . scrollParent() method finds the element that the currently selected element will scroll within.


1 Answers

This works with both horizontal and vertical scrollbars. The trick is detecting BOTH the too-wide/too-short AND if the computed CSS is going to allow you to display a scrollbar.

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();
like image 70
Andrew Templeton Avatar answered Oct 09 '22 05:10

Andrew Templeton