title: Selenium webdriver System Invalid Cast Exception
body: <p>To collect some test data for a neural net in C# I want to use Selenium to scrape some dynamically generated data <a href="http://online.wsj.com/mdc/public/page/2_3022-govtbonds.html?mod=topnav_2_3004" rel="nofollow noreferrer">WSJ</a>. There is an example implementation on the Selenium site that seems to do exactly what I need <a href="http://docs.seleniumhq.org/docs/03_webdriver.jsp" rel="nofollow noreferrer">Finding all the input elements to the every label on a page</a>. The example searches on <code>TagName</code>, I search on <code>ClassName</code>, but other than that, I think it's identical.
  Yet, when I run this code, creating an <code>IList</code> with <code>IWebElements</code> works, but the following <code>IJavaScriptExecutor</code> throws an Invalid Cast exception: </p>
  
  <blockquote>
    <p>Unable to cast object of type
    System.Collections.ObjectModel.ReadOnlyCollection 1[System.Object]
    to type
    System.Collections.Generic.IList 1[OpenQA.Selenium.IWebElement]</p>
  </blockquote>
  
  <p>Here's a bit of the code, this is for "text", I do the same for "num":</p>
  
  <pre><code>    // Find elements by class name
      IList&lt;IWebElement&gt; labels = driver.FindElements(By.ClassName("text"));
  
      // get all input elements for every class label
      IList&lt;IWebElement&gt; labVals = (IList&lt;IWebElement&gt;)((IJavaScriptExecutor)driver).ExecuteScript(
          "var labels = arguments[0], labVals = []; for (var i=0; i &lt; labels.length; i++){" +
          "labVals.push(document.getElementById(labels[i].getAttribute('for'))); } return labVals;", labels);
  </code></pre>
  
  <p>I have looked at this question <a href="https://stackoverflow.com/questions/14146513/selenium-web-driver-c-sharp-invalidcastexception-for-list-of-webelements-after-j">Selenium Web Driver C# InvalidCastException</a> which may point to the same problem, but I don't see how the answers provided can help me.</p>
  
  <p>An option could be to break up the <code>IJavaScriptExecutor</code> statement into "discrete" code with a work around, but I would not know how to do that.</p>
  
  <p>Once I have the text labels and data values both in a <code>List</code> structure, I should be able to find the numbers I need.</p>