Search code examples
c#selenium-webdriverautomationspecflowscenarios

Passing Datatable item in xpath and getting error "javascript error: arguments[0].scrollIntoView is not a function"


Hello I am new to c# and specflow.

I have a scenario outline with 1 datatable

Examples:
    | Name    | ID           | Attendence| Date       |
    | Shan    | 1003 - Shan  | FT        | 10/22/2021 |
    | Sam     | ZYZ          | FT        | 10/22/2021 |

The above is my code (Page)

 IList<IWebElement> AllList = new List<IWebElement>(driver.FindElements(By.XPath("//*[@id='Myul']/li"))); //get all elements in the list

 var path = driver.FindElement(By.XPath("//span[contains(text(),'" + ID + "')]"));
 IJavaScriptExecutor je = (IJavaScriptExecutor)driver;

 je.ExecuteScript("arguments[0].scrollIntoView(false);", AllList.Contains(path));
 Thread.Sleep(2000);

 if (path.Displayed)
 {
     path.Click();
 }

I am trying to pass the ID I have set in the Feature file, into the xpath but I am getting the following error:

OpenQA.Selenium.WebDriverException: 'javascript error: arguments[0].scrollIntoView is not a function

enter image description here


Solution

  • AllList.Contains(path) returns a boolean value. This is the context of the call to arguments[0].scrollIntoView(false); which means JavaScript is attempting to execute scrollIntoView(false); on a true/false value instead of an HTMLElement object.

    The AllList.Contains(path) method just tests to see if path is one of the items in the AllList collection. It doesn't quite make sense with what you are trying to accomplish. You probably only need to pass path:

    je.ExecuteScript("arguments[0].scrollIntoView(false);", path);
    

    Since path is an IWebElement in C#, JavaScript will interpret that as the HTMLElement object pointing to the same HTML tag that your IWebElement refers to in C#. This is the context for the call to arguments[0].scrollIntoView(false); which executes HTMLElement.scrollIntoView(boolean). I believe this to be the JavaScript method you are trying to call.