0

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?

Karthik
  • 1,064
  • 2
  • 16
  • 33

3 Answers3

2

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);");
Community
  • 1
  • 1
Karthik
  • 1,064
  • 2
  • 16
  • 33
0

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;
}
Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49
  • I am getting an exception `Unable to cast object of type 'Protractor.NgWebDriver' to type 'OpenQA.Selenium.IJavaScriptExecutor'` – Karthik Aug 14 '15 at 05:57
  • I think you get this error when you do something like this: myProtactorElement = ScrollToElement(myProtactorElement).Click(); If so, then just change the type that function returns. – Denis Koreyba Aug 14 '15 at 06:49
  • The actual problem is that the NgWebDriver does not implement the interface `OpenQA.Selenium.IJavaScriptExecutor`. So it throws an error while casting it. Let me know if you need some code sample – Karthik Aug 14 '15 at 07:06
  • Yes, please five code where you use this method. And point to line where error occurs. – Denis Koreyba Aug 14 '15 at 07:56
  • Try this: IJavaScriptExecutor js = NgWebDriver as IJavaScriptExecutor; And then js.ExecuteScript(); – Denis Koreyba Aug 14 '15 at 09:32
  • No. It does not work. Protractor.NET uses NgWebDriver which does not implement `IJavaScriptExecutor`. So, casting always returns null. – Karthik Aug 14 '15 at 10:16
-1

The below code helped me to achieve the same.

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