Rakesh
Rakesh

Reputation: 91

ScrollIntoView is not working properly if element is up - Selenium?

Scroll to element works fine if element is lower position ( scroll down ) , but in case of element is upper position ( scroll up ) it don't scroll to element in view rather it scroll one less to element ( count is not fixed may it 2 or 3 , varies ) .

I have to write explicit code for scroll up for element which is up , after ScrollIntoView , this is not consistent as scroll up count is not fixed .

I used this 2 codes one by one , but both gives same behavior

Code1 :

            // Scroll to view by Action 
            Actions actions = new Actions(driver);
            actions.MoveToElement(webElement);
            actions.Perform();

Code 2:

            // Scroll to view by JavaScript executer     
   ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", webElement);

Please help me in this case , please suggest what I have to with this .

Upvotes: 1

Views: 9689

Answers (2)

Abhinav
Abhinav

Reputation: 1027

Referrence: scrollIntoView vs moveToElement

You have more control on how the element is scrolled with scrollIntoView than with moveToElement.

Selenium is only interested in bringing the element into view so that the mouse can be placed on it. It does not give you any say in how it is going to do it. scrollIntoView however allows you, for instance, to specify whether you want the top or bottom of the element to be align with its scrollable ancestor.

Check this for more details: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

Upvotes: 1

Guy
Guy

Reputation: 50919

You can try double MoveToElement(). First scroll to the element parent and than to the element itself

actions.MoveToElement(parentElement).MoveToElement(webElement).Perform();

Upvotes: 0

Related Questions