How to scroll up using Protractor.NET?

796 Views Asked by At

My web page is little lengthy and the SAVE button is at the top right-hand corner. As I input the data through Protractor.NET, the webpage scrolls down which hides the SAVE button, thereby throwing a Element is not clickable at a point error. Now inorder to save the webpage, I need to scroll up and then find the SAVE button and click it.

I have an example in Protractor which uses window.scrollTo(0,0), but how do I implement the same in Protractor.NET

EDIT: Included code

public void Test()
{
     var saveBtn = NgWebDriver
                 .FindElement(By.ClassName("btnSave"))
                 .FindElement(By.ClassName("Save"));
    var btnSv = Scroller(saveBtn);
    btnSv.Click();
}


public IWebElement Scroller(IWebElement element)
{
    ((IJavaScriptExecutor)NgWebDriver).ExecuteScript("arguments[0].scrollIntoView();", element);
    return element;
}

So the exception occurs in Scroller method while casting the NgWebDriver to IJavaScriptExecutor type

How can I accomplish this?

3

There are 3 best solutions below

0
On BEST ANSWER

Finally got the solution to scrolling up to the top in Protractor.NET

Had referred the link and was able to solve my problem.

The below code worked for me.

IWebDriver driver = MyWebDriver.WrappedDriver;
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("scroll(0, -250);");
6
On

Why do you want to make it complicated?

If you want to scroll to an element you can use this simple method:

public IWebElement ScrollToElement(IWebElement webElement)
{
      ((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].scrollIntoView();", webElement);

      return webElement;
}
0
On

The below code helped me to achieve the same.

 IJavaScriptExecutor jse = (IJavaScriptExecutor)NgDriver;
 jse.ExecuteScript("arguments[0].scrollIntoView()", webElement);