Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  63] [ 3]  / answers: 1 / hits: 22882  / 5 Years ago, thu, october 24, 2019, 12:00:00

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.


More From » java

 Answers
28

Element.scrollIntoView()


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




Syntax



  • element.scrollIntoView()

  • element.scrollIntoView(alignToTop) // Boolean parameter

  • element.scrollIntoView(scrollIntoViewOptions) // Object parameter




Parameters


The parameters for this method are:



  • alignToTop (Optional): Is a Boolean value, if true, the top of the element will be aligned to the top of the visible area of the scrollable ancestor. This is the default value. If false, the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor. Corresponds to scrollIntoViewOptions: {block: "end", inline: "nearest"}.

  • scrollIntoViewOptions (Optional): Is an Object with the following properties:

  • behavior (Optional): Defines the transition animation. One of "auto" or "smooth". Defaults to "auto".

  • block (Optional): Defines vertical alignment. One of "start", "center", "end", or "nearest". Defaults to "start".

  • inline (Optional): Defines horizontal alignment. One of "start", "center", "end", or "nearest". Defaults to "nearest".




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:



  • start

  • center

  • end

  • nearest




Alternative


As an alternative you can use either of the following options



  • scrollLeft(): Element.scrollLeft() property gets or sets the number of pixels that an element's content is scrolled from its left edge. If the element's direction is rtl (right-to-left), then scrollLeft is 0 when the scrollbar is at its rightmost position (at the start of the scrolled content), and then increasingly negative as you scroll towards the end of the content.

  • scrollWidth(): Element.scrollWidth() read-only property is a measurement of the width of an element's content, including content not visible on the screen due to overflow.




Outro


You can find a couple of relevant detailed discussion in:



[#51545] Monday, October 14, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
sidneyh questions
Tue, Jun 7, 22, 00:00, 2 Years ago
Wed, Apr 13, 22, 00:00, 2 Years ago
Wed, Aug 12, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Fri, Apr 24, 20, 00:00, 4 Years ago
;