title: Cannot loop/iterate through a list of WebElements in Selenium using C#
body: <p>I am having an issue looping through a list of WebElements. It works the first iteration through but then fails at the second iteration. </p>
  
  <h2><strong>Workflow</strong></h2>
  
  <p>Loop 1 Executes properly and creates a list of web elements. I did this because I thought Loop 2 was failing because navigation switching (e.g. clicking on the web element)</p>
  
  <p>Durning Loop 2 execution</p>
  
  <ol>
  <li>The <code>ListofLinks</code> is populated</li>
  <li>On the first iteration, it works fine, clicks on the web element and finishes the loop</li>
  <li>On the 2nd iteration <code>Item.Text</code> fails. It looks like that item does not exist. </li>
  </ol>
  
  <p>Any thoughts? </p>
  
  <pre><code>    var ListOflinks = new List&lt;IWebElement&gt;();
  
      //Loop 1
                  foreach (var item in driver.FindElements(By.TagName("a")))
                  {
                      if (item.Text != "")
                      {
                          ListOflinks.Add(item);
                      }
                  }
  
  // Loop 2
   foreach (var item in ListOflinks)
              {
                  if (item.Text != "")   // &lt;&lt;&lt;&lt;---- Fails Here
                  {
                      // Clicks on Current Link
                      item.Click();
                  }
               }
  </code></pre>
  
  <p>Note: Before the I tried the 2 loop solution. I was expeiencing the same results with. It would work for the first web element then fail on the 2nd iteration. </p>
  
  <pre><code>foreach (var item in driver.FindElements(By.TagName("a")))
              {
                  if (item.Text != "")
                  {
                      item.Click();
                  }
              }
  </code></pre>