title: c# break while and foreach
body: <p>I have a little difficulty solving a problem. I'm looking for all the elements that take a class called '_1VfKB'. Inside this element at a span with a 'data-icon' as an attribute. I am using the while loop so if the data-icon receives a value X it keeps running, otherwise it ends the loop (while). However, how can I break the while when my condition is triggered?</p>
  
  <pre><code>private void CheckIfMsgSent() {
    while (true) {
     IReadOnlyCollection &lt; IWebElement &gt; els = driver.FindElementsByClassName("_1VfKB");
     foreach(IWebElement el in els) {
       IWebElement span = el.FindElement(By.TagName("span"));
  
       if (span.GetAttribute("data-icon") == "status-time") {
        Thread.Sleep(1000);
        break;
       }
       /* If no element is found with the data-icon = "status-time", break while and break foreach*/
  
                  }
              }
          }
  </code></pre>