title: How can I loop through list of WebElements and select one WebElement with a condition?
body: <p>I am using Selenium WebDriver with Java. I am trying to create a method that loops through a list of WebElements to return the first WebElement that contains the text "Ooga Booga", or return null if there are no elements in the list that contains this text.</p>
  
  <pre><code>public class myClass {
   private static WebElement element = null;
   private static List&lt;WebElement&gt; elements = null;
  
   public static WebElement returnOneElement (WebDriver driver){
      elements = driver.findElements(By.cssSelector("someSelector"));
      for (WebElement element : elements){
          String myText = element.findElement(By.cssSelector("classC")).getText();
          if (myText.contains("Ooga Booga")) {
              return element;
          }
      }
      return null;
  }
  </code></pre>
  
  <p>This all works fine until I run into an element that does not have this attribute:</p>
  
  <pre><code>String myText = element.findElement(By.cssSelector("classC")).getText();
  </code></pre>
  
  <p>How can I continue to loop through my List while ignoring any elements that do not have "someDiv" inside of it? </p>
  
  <pre><code>&lt;div class="someSelector"&gt;
      &lt;div class="classA"&gt;
      &lt;div class="classB"&gt;
  &lt;/div&gt;
  
  &lt;div class="someSelector"&gt;
      &lt;div class="classA"&gt;
      &lt;div class="classB"&gt;
      &lt;div class="classC"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I need to search for text inside div "classC".</p>