javascriptjavaseleniumscrolljs-scrollintoview

scrollIntoView() not working for horizontal scroll (Selenium)


i want to scroll right to a column. This is what i tried:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

This works for vertical scrolling but not for horizontal.


Solution

  • Element.scrollIntoView()

    Element.scrollIntoView() method scrolls the element on which it's called into the Viewport of the browser window.


    Syntax


    Parameters

    The parameters for this method are:


    This usecase

    As per your line of code:

    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    

    the argument true refers to boolean value for alignToTop. Hence the issue.


    Solution

    To automate horizontal scrolling you need to pass the argument for the inline parameter either among the following:


    Alternative

    As an alternative you can use either of the following options


    Outro

    You can find a couple of relevant detailed discussion in: